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);
    }
}

猜你喜欢

转载自blog.csdn.net/gylgww/article/details/81981677