1. 程式人生 > >Android--PullToRefreshListView 上拉下拉重新整理(設定上拉下拉重新整理時的樣式)

Android--PullToRefreshListView 上拉下拉重新整理(設定上拉下拉重新整理時的樣式)

一:在使用PullToRefreshListView時,一定要先導包

然後點選Import Module

將library包匯入進去(這裡因為我已經導過了,所以使用library1)

現在將已經導完的包新增到wome我們的Module裡面

點選倒數第三個,

點選第三個,把library匯入進去,這樣我們的library包就已經導好了!

二:構造佈局

我們在佈局時可以發現,我們在佈局檔案裡面,PullToRefreshListView是不提示的,所以我們可以在Activity裡面先打一個PullToRefreshListView,然後複製它的包名放到xml檔案中。

activity.xml檔案:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</android.support.constraint.ConstraintLayout>

三:使用PullToRefreshListView

注意:PullToRefreshListView和ListView使用方法非常相似,都需要資料來源。

下面我們就來模擬一下資料來源:

程式碼:

public class MainActivity extends AppCompatActivity {
private PullToRefreshListView listView;
private List<String> list = new ArrayList<>();
private Handler handler = new Handler();
private LoadingLayoutProxy layoutProxy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = findViewById(R.id.listView);
        listView.setMode(PullToRefreshBase.Mode.BOTH);
        for (int i = 0;i<50;i++){
            list.add("item"+i);//資料來源
        }
        listView.setAdapter(new MyAdapter());//設定介面卡將資料新增到PullToRefreshListView裡面
        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
//設定PullToRefreshListView的上拉下拉監聽事件
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                Log.i("下拉重新整理","重新整理");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
//睡眠1s
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
//停止重新整理
                                listView.onRefreshComplete();
                            }
                        });
                    }
                }).start();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                Log.i("上拉載入更多","載入更多");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                listView.onRefreshComplete();
                            }
                        });
                    }
                }).start();
            }
        });
        listView.setPullToRefreshOverScrollEnabled(true);
        layoutProxy = (LoadingLayoutProxy) listView.getLoadingLayoutProxy();
        layoutProxy.setLastUpdatedLabel("重新整理時的文字");
        layoutProxy.setPullLabel("載入完畢");
        layoutProxy.setLoadingDrawable(getResources().getDrawable(R.mipmap.ic_launcher));


    }
    private class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(MainActivity.this,R.layout.item,null);
            TextView tv = view.findViewById(R.id.tv);
            tv.setText(list.get(position));
            return view;
        }
    }
}

因為我們是模擬的資料來源,所以上拉下拉時候獲取不到資料,重新整理不出來,就會一直重新整理,所以我們就需要模擬一下重新整理完成的方法:

new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        handler.post(new Runnable() {
            @Override
            public void run() {
                listView.onRefreshComplete();
            }
        });
    }
}).start();

效果圖:

這樣,我們就做完了!