Android页面下拉刷新

Posted 第厘

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
    }
}

 

以上是关于Android页面下拉刷新的主要内容,如果未能解决你的问题,请参考以下文章

android开发游记:SpringView 下拉刷新的高效解决方案,定制你自己风格的拖拽页面

在 NavigationUI、Android Studio 中停止页面刷新

自定义下拉刷新不适用于 iOS

怎么自定义android 下拉刷新动画

自动刷新android片段,直到满足条件

android webview怎么实现下拉刷新