二.PullToRefresh 主方法里面

来源:互联网 发布:mongodb是什么数据库 编辑:程序博客网 时间:2024/05/22 13:15
public class Table_beijing extends Fragment {    private View view;    private PullToRefreshGridView pull_ref;    //网址地址    private String path = "http://www.meirixue.com/api.php?c=index&a=index";    //页数    private int pageIndex = 1;    //操作的类型 1:下拉刷新  2:上拉加载    private int type = 1;    //    给Bean包设置一个集合    private List<Table_Beijing.DataBean.HotcategoryBean> list = new ArrayList<>();    private Table_Beijing myBean;    private Table_Beijing_Baseadapter adapter;    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.table_beijing, container, false);        initView(view);// 设置Mode   Mode设置为Mode.BOTH后,下拉和上拉都会执行onRefresh()中的方法了       /* pull_ref.setMode(PullToRefreshBase.Mode.BOTH);       然后重写两个方法        pull_ref.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {        1.向下            @Override            public void onPullDownToRefresh(PullToRefreshBase<GridView> pullToRefreshBase) {            }        2.向上            @Override            public void onPullUpToRefresh(PullToRefreshBase<GridView> pullToRefreshBase) {            }        });*/// 设置Mode   Mode设置为Mode.BOTH后,下拉和上拉都会执行onRefresh()中的方法了        pull_ref.setMode(PullToRefreshBase.Mode.BOTH);// 点击事件        pull_ref.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {            @Override            public void onPullDownToRefresh(PullToRefreshBase<GridView> pullToRefreshBase) {                type = 1;   //重新请求第一页的数据 10条数据                String path = "http://www.meirixue.com/api.php?c=index&a=index" ;                new asyncTask().execute(path);            }            @Override            public void onPullUpToRefresh(PullToRefreshBase<GridView> pullToRefreshBase) {                type = 2;//                pageIndex++;   //进行地址的拼接                String path = "http://www.meirixue.com/api.php?c=index&a=index";   //执行异步请求                new asyncTask().execute(path);            }        });        new asyncTask().execute(path);        return view;    }    class asyncTask extends AsyncTask<String,Void,String>{        @Override        protected String doInBackground(String... params) {            try {//    获取网络                URL url = new URL(path);                HttpURLConnection connection = (HttpURLConnection) url.openConnection();                connection.setRequestMethod("GET");//   设置延时                connection.setReadTimeout(5000);                connection.setConnectTimeout(5000);                int responseCode = connection.getResponseCode();//  进行判断是否是 200                if (responseCode == 200){                    InputStream inputStream = connection.getInputStream();//  进行解析                    String s = streamToString(inputStream, "utf-8");                    return s;                }            } catch (Exception e) {                e.printStackTrace();            }            return null;        }        //相当于主线程        @Override        protected void onPostExecute(String s) {            super.onPostExecute(s);//获得利用 gson            Gson gson = new Gson();            myBean = gson.fromJson(s, Table_Beijing.class);//代表刷新            if (type == 1) {//如果是刷新的话                list.clear();//清除原有的数据            }            list.addAll(myBean.getData().getHotcategory());//添加新的数据            setAdapter();            pull_ref.onRefreshComplete();        }    }// 定义一个适配器    public void setAdapter() {        if (adapter == null) {            Log.d("SSSSSSSSSSSSSS", "+++++++listtttttt++++++++" + list);//            adapter = new List_Baseadapter(getActivity(), list);            adapter = new Table_Beijing_Baseadapter(getActivity(),list);            pull_ref.setAdapter(adapter);        } else {            //否则刷新适配器            adapter.notifyDataSetChanged();        }    }    //解析数据    private String streamToString(InputStream inputStream, String s) {//        进行trycatch        try {            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);            String a = "";            StringBuffer stringBuffer = new StringBuffer();            while ((a = bufferedReader.readLine()) != null) {                stringBuffer.append(a);            }            bufferedReader.close();            return stringBuffer.toString();        } catch (Exception e) {            e.printStackTrace();        }        return "";    }//获得控件    private void initView(View view) {        pull_ref = (PullToRefreshGridView) view.findViewById(R.id.beijing_pull);    }
原创粉丝点击