加依赖
视图:
<com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/pull_refresh_list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:cacheColorHint="#00000000" android:divider="#19000000" android:dividerHeight="4dp" android:fadingEdge="none" android:fastScrollEnabled="false" android:footerDividersEnabled="false" android:headerDividersEnabled="false" android:smoothScrollbar="true" />
代码:
public class MainActivity extends AppCompatActivity { PullToRefreshListView ptrshlv; List<List<String>> result; LinkedList<List<String>> arr; MyAdpt myadpt; String path = "https://suggest.taobao.com/sug?code=utf-8&q=%E6%98%BE%E7%A4%BA%E5%99%A8"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ptrshlv = (PullToRefreshListView) findViewById(R.id.pull_refresh_list); ptrshlv.setMode(PullToRefreshBase.Mode.BOTH); arr = new LinkedList<List<String>>(); myadpt = new MyAdpt(); ptrshlv.setAdapter(myadpt); new MyAsyncTask().execute(path); ptrshlv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { arr.clear(); new MyAsyncTask().execute(path); } @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { new MyAsyncTask().execute(path); } }); } //添加 class MyAsyncTask extends AsyncTask<String,Void,String>{ @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson = new Gson(); Bean bean = gson.fromJson(s, Bean.class); result = bean.getResult(); arr.addAll(result); myadpt.notifyDataSetChanged(); ptrshlv.onRefreshComplete(); } @Override protected void onProgressUpdate(Void... values) { super.onProgressUpdate(values); } @Override protected String doInBackground(String... strings) { try { URL url = new URL(strings[0]); HttpURLConnection huc = (HttpURLConnection) url.openConnection(); if(huc.getResponseCode()==200){ InputStream inputStream = huc.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder sb = new StringBuilder(); String st = ""; while((st=br.readLine())!=null){ sb.append(st); } String ss = sb.toString(); return ss; } } catch (Exception e) { e.printStackTrace(); } return null; } } class MyAdpt extends BaseAdapter{ @Override public int getCount() { return arr.size(); } @Override public Object getItem(int i) { return arr.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { Adpt ad; List<String> liststr = arr.get(i); if(view==null){ view = View.inflate(MainActivity.this,android.R.layout.simple_expandable_list_item_1,null); ad = new Adpt(); ad.tv1 = view.findViewById(android.R.id.text1); view.setTag(ad); }else{ ad = (Adpt) view.getTag(); } ad.tv1.setText(liststr.get(0)); return view; } class Adpt{ TextView tv1; } } }