热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

RecyclerView应用复习

导包implementationandroidx.recyclerview:recyclerview:1.1.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 mDatas = new ArrayList();
private RecyclerView.Adapter adapter;

private void initRecycler() {
    adapter = new CommonAdapter(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 mDatas = new ArrayList();
private RecyclerView.Adapter adapter;

private void initRecycler() {
    adapter = new CommonAdapter(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

    

单选多选

// 主动选择,防止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 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 tempList = new ArrayList(mDatas);
for (QueryBean queryBean : mDatas) {
    if (queryBean.isSelected()) {
        tempList.remove(queryBean);
    }
}
mDatas.clear();
mDatas.addAll(tempList);
adapter.notifyDataSetChanged();

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


推荐阅读
author-avatar
深哥和小P孩的美好生活_590
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有