1. 程式人生 > >PullListView 和側滑 如何正確結合運用.....

PullListView 和側滑 如何正確結合運用.....

<佈局>

<android.support.v4.widget.DrawerLayout 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=".activity.ShowActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.handmark.pulltorefresh.library.PullToRefreshListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/pull_list"></com.handmark.pulltorefresh.library.PullToRefreshListView>
    </LinearLayout>

    <!--側滑-->
    <RelativeLayout
        android:layout_width="320dp"
        android:layout_gravity="start"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary"></RelativeLayout>

</android.support.v4.widget.DrawerLayout>

 

<主介面>

public class ShowActivity extends BaseActivity implements NetCallback, AdapterView.OnItemClickListener {

    private PullToRefreshListView pullListView;
    //介面
    private String api = "http://172.17.8.100/mobile/exam/findNewList";
    private boolean isPull = false;
    private MainAdapter adapter;
    //定義資料集
    private List<MainBean.ResultBean.DataBean> dataBeanList = new ArrayList<>();

    @Override
    protected void findView() {
       pullListView = findViewById(R.id.pull_list);
    }

    @Override
    protected void setListener() {
        pullListView.setOnItemClickListener(this);

    }

    @Override
    protected void initFinish() {
        //設定支援上拉下拉
        pullListView.setMode(PullToRefreshBase.Mode.BOTH);
        //設定監聽
        pullListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
                isPull = true;
                loadData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
                loadData();
            }
        });
        //設定介面卡
        adapter = new MainAdapter(this,dataBeanList);
        pullListView.setAdapter(adapter);
        loadData();
    }

    //載入資料
    private void loadData() {
        utils.getDataFromService(api,this);
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_show;
    }

    @Override
    public void onSuccess(String result) {
        //解析資料
        Gson gson = new Gson();
        MainBean mainBean = gson.fromJson(result,MainBean.class);

        //下拉
        if (isPull){
            isPull =! isPull;
            dataBeanList.clear();
        }
        //新增資料
        dataBeanList.addAll(mainBean.getResult().getData());

        //更新資料
        adapter.notifyDataSetChanged();

        //停止重新整理
        pullListView.onRefreshComplete();
    }

    @Override
    public void onError(String errorMsg) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this,DataActivity.class);
        MainBean.ResultBean.DataBean item = adapter.getItem(position);
        intent.putExtra("url",item.getUrl());
        startActivity(intent);
    }
}