PullToFreshListView

加依赖

视图:

<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;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值