• 2021.5.10 Android页面下拉刷新


    一、今日学习内容

      关于页面下拉刷新

    public class Frag_01 extends Fragment implements XListView.IXListViewListener{
    
        private String path = "http://www.xieast.com/api/news/news.php?page=";
        private XListView xlistview;
        private int num = 1;
        private List<Beans.DataBean> data = new ArrayList<>();
        private MyAdapter myAdapter;
        private Handler handler;
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.frag_01,container,false);
            //控件
            xlistview = view.findViewById(R.id.xlistview);
            //方法
            getData(1);
    
            handler = new Handler();
            xlistview.setPullRefreshEnable(true);
            xlistview.setPullLoadEnable(true);
            xlistview.setXListViewListener(this);
            return view;
        }
        //时间
        private void getTime() {
            xlistview.stopRefresh();//停止刷新
            xlistview.stopLoadMore();//停止加载更多
            SimpleDateFormat dateFormat = new SimpleDateFormat("mm:ss");//设置日期显示格式
            Date date = new Date(System.currentTimeMillis()); //获取当前时间
            String format = dateFormat.format(date);// 将时间装换为设置好的格式
            xlistview.setRefreshTime(format);//设置时间*/
        }
    
        private void getData(int num) {
    
            new AsyncTask<String, Integer, List<Beans.DataBean>>() {
                @Override
                protected List<Beans.DataBean> doInBackground(String... strings) {
    
                    try {
                        URL url = new URL(strings[0]);
                        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                        urlConnection.setConnectTimeout(5000);
                        urlConnection.setRequestMethod("GET");
                        byte[] bytes = new byte[1024];
                        int len = 0;
                        if(urlConnection.getResponseCode()==200){
                            InputStream inputStream = urlConnection.getInputStream();
                            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                            while ((len=inputStream.read(bytes))!=-1){
                                outputStream.write(bytes,0,len);
                            }
                            String json = outputStream.toString();
                            inputStream.close();
                            outputStream.close();
    
                            //解析数据
                            Gson gson = new Gson();
                            Beans beans = gson.fromJson(json, Beans.class);
    
                            return beans.getData();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
    
                    return null;
                }
    
                @Override
                protected void onPostExecute(List<Beans.DataBean> dataBeans) {
                    super.onPostExecute(dataBeans);
                    data.addAll(dataBeans);
    
                    //适配器
                    myAdapter = new MyAdapter(getActivity(), (List<Beans.DataBean>) data);
                    xlistview.setAdapter(myAdapter);
                    //适配器
                    myAdapter.notifyDataSetChanged();
                }
            }.execute(path+num);
    
        }
    
        @Override
        public void onRefresh() {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    num = 1;
                    data.clear();
                    getData(1);
                    getTime();
                }
            },3000);
        }
    
        @Override
        public void onLoadMore() {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    num++;
                    getData(num);
                    getTime();
                }
            },3000);
        }
    }
  • 相关阅读:
    ubuntu分屏终端
    Xcode-5.1.1更改文件盯作者
    Swift——(两)Swift访问元组
    Android Loader使用,屏幕解锁,重复荷载
    医疗信息季节:第二十三届中国国际医疗仪器设备展览会暨研讨会 思考
    UIBarButtonItem 小记边
    L轻松学习inux教程5 知识与学习bash
    Android 教你打造炫酷的ViewPagerIndicator 不仅仅是高仿MIUI
    Android Context 上下文 你必须知道的一切
    Android 自定义控件 优雅实现元素间的分割线 (支持3.0以下)
  • 原文地址:https://www.cnblogs.com/wmdww/p/14905836.html
Copyright © 2020-2023  润新知