程序员

RecyclerView应用复习

2020-07-14  本文已影响0人  世外大帝

导包

    implementation 'androidx.recyclerview:recyclerview:1.1.0'  //recyclerview
    implementation 'com.zhy:base-rvadapter:3.0.3' // adapter 框架
    implementation 'com.github.orangegangsters:swipy:1.2.3@aar' // 上拉刷新下拉加载

应用

GridList

private RecyclerView mRecyclerView;
private List<QueryBean> mDatas = new ArrayList<>();
private RecyclerView.Adapter adapter;

private void initRecycler() {
    adapter = new CommonAdapter<QueryBean>(getActivity(), R.layout.recycler_item_grid, mDatas) {
        @Override
        protected void convert(ViewHolder holder, QueryBean queryBean, int position) {
            holder.setImageResource(R.id.item_image, queryBean.getImgId());
            holder.setText(R.id.item_content, queryBean.getName());
        }
    };

    GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 3);
    mRecyclerView.setLayoutManager(layoutManager);
    //设置分隔线
    mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            //outRect.set()中的参数分别对应左、上、右、下的间隔
            outRect.set(10, 10, 10, 10);
        }
    });
    //设置Adapter
    mRecyclerView.setAdapter(adapter);
}


private void loadData() {
    mDatas.addAll(mViewModel.getList());
    adapter.notifyDataSetChanged();
}


ListView

private RecyclerView mRecyclerView;
private List<String> mDatas = new ArrayList<>();
private RecyclerView.Adapter adapter;

private void initRecycler() {
    adapter = new CommonAdapter<String>(THIS, R.layout.recycler_item, mDatas) {
        @Override
        protected void convert(ViewHolder holder, String s, int position) {
            holder.setTextColor(R.id.item_text, Color.RED);
            holder.setText(R.id.item_text1, s);
        }
    };

    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    //设置为垂直布局,这也是默认的
    layoutManager.setOrientation(RecyclerView.VERTICAL);
    //设置布局管理器
    mRecyclerView.setLayoutManager(layoutManager);

    //设置分隔线
    mRecyclerView.addItemDecoration(new DividerItemDecoration(this, RecyclerView.VERTICAL));
    //设置Adapter
    mRecyclerView.setAdapter(adapter);

}

private void addData() {
    mDatas.add("data");
    adapter.notifyDataSetChanged();
}
    

上拉刷新下拉加载

java

private SwipyRefreshLayout mSwipeRefreshLayout;

private void initView(View view) {
    //...
    mSwipeRefreshLayout = view.findViewById(R.id.swipyrefreshlayout);
}

private void initListener() {
    mSwipeRefreshLayout.setOnRefreshListener(direction -> {
        if (direction == SwipyRefreshLayoutDirection.TOP) {
            // refresh
        }
        if (direction == SwipyRefreshLayoutDirection.BOTTOM) {
            //loadMore
        }
    });
    //...
}

private void cancelRefreshing() {
    if (mSwipeRefreshLayout.isRefreshing())
        mSwipeRefreshLayout.setRefreshing(false);
}

xml

    <com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout
        android:id="@+id/swipyrefreshlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srl_direction="both">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/query_recycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout>

单选多选

// 主动选择,防止recycleview的holder复用导致的勾选混乱                
holder.setChecked(R.id.item_select, mDatas.get(position).isSelected());
// item的单击事件
holder.setOnClickListener(R.id.item_select, v -> {
    // 通过判断checkbox是否勾选过滤数据
    CheckBox checkBox = holder.getView(R.id.item_select);
    if (checkBox.isChecked()) {
        mDatas.get(position).setSelected(true);
    } else {
        mDatas.get(position).setSelected(false);
    }
});

确定选中项

// List<QueryBean> tempList = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for (QueryBean queryBean : mDatas) {
    if (queryBean.isSelected()) {
        // tempList.add(queryBean);
        sb.append(queryBean.getName());
    }
}
L.e("选中:" + sb);

删除项

List<QueryBean> tempList = new ArrayList<>(mDatas);
for (QueryBean queryBean : mDatas) {
    if (queryBean.isSelected()) {
        tempList.remove(queryBean);
    }
}
mDatas.clear();
mDatas.addAll(tempList);
adapter.notifyDataSetChanged();

关于删除,不能使用数组本身remove,如果使用fori,也不能使用递增
通过临时数组或者递减都是可以的

上一篇下一篇

猜你喜欢

热点阅读