标签:
<com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent"></com.handmark.pulltorefresh.library.PullToRefreshListView>
public class OKHttpUtils { private static OkHttpClient client = new OkHttpClient(); public static void getData(String urlStr, Callback callback) { Request request = new Request.Builder().url(urlStr).build(); client.newCall(request).enqueue(callback); } }
public class MainActivity extends AppCompatActivity { private List<String> list; private PullToRefreshListView refreshListView; private String urlStr = "http://m2.qiushibaike.com/article/list/text?page=%d"; private int pageCount = 1; private ArrayAdapter<String> adapter; private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { //设置刷新完毕,取消刷新动画 refreshListView.onRefreshComplete(); adapter.notifyDataSetChanged(); } }; private Callback callback; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list = new ArrayList<>(); refreshListView = (PullToRefreshListView) findViewById(R.id.lv); TextView noDataTv = (TextView) findViewById(R.id.no_data_tv); //当ListView中的数据为空时显示的控件 refreshListView.setEmptyView(noDataTv); // initData(); initNetData(); //设置既有下拉刷新,又有上拉加载,不设置,默认只有下拉刷新 refreshListView.setMode(PullToRefreshBase.Mode.BOTH); refreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { //下拉刷新回调的方法 @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { new Thread(new Runnable() { @Override public void run() { SystemClock.sleep(3000); mHandler.sendEmptyMessage(0); } }).start(); } //上拉加载回调的方法 @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { OKHttpUtils.getData(String.format(urlStr, ++pageCount), callback); } }); refreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { //position从1开始 @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, "position:" + position, Toast.LENGTH_SHORT).show(); } }); ILoadingLayout loadingLayoutProxy = refreshListView.getLoadingLayoutProxy(); //提示用户松手刷新时的文本 loadingLayoutProxy.setReleaseLabel("释放时显示的文本"); //正在加载数据时显示的文本 loadingLayoutProxy.setRefreshingLabel("正在刷新时显示的文本"); //下拉时显示的文本 loadingLayoutProxy.setPullLabel("下拉时显示的文本"); //显示上次刷新时的文本,可以显示上次刷新时间 loadingLayoutProxy.setLastUpdatedLabel("显示上次刷新时的文本"); //获取一个bitmap对象 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); //设置刷新动画中的图片 loadingLayoutProxy.setLoadingDrawable(new BitmapDrawable(getResources(), bitmap)); //设置字体 loadingLayoutProxy.setTextTypeface(Typeface.createFromAsset(getAssets(), "mycustom.ttf")); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list); refreshListView.setAdapter(adapter); } /** * 初始化网络数据 */ private void initNetData() { callback = new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()) { parseJson(response.body().string()); mHandler.sendEmptyMessage(0); } } }; OKHttpUtils.getData(String.format(urlStr, pageCount), callback); } /** * json解析 * * @param string */ private void parseJson(String string) { try { JSONObject jo = new JSONObject(string); JSONArray items = jo.getJSONArray("items"); for (int i = 0; i < items.length(); i++) { String content = items.getJSONObject(i).getString("content"); list.add(content); } } catch (JSONException e) { e.printStackTrace(); } } private void initData() { list = new ArrayList<>(); for (int i = 0; i < 30; i++) { list.add("lisi:" + i); } } }
标签:
原文地址:http://www.cnblogs.com/anni-qianqian/p/5454231.html