This is the extension library for RecyclerView you want

Since RecyclerView was launched in 2015, there have been a variety of open source libraries in open source, but, is it really suitable for you in the process of using it? I'm not sure about this, however, I feel like I haven't found a library that is more convenient to use, so I created this library ExpandRecyclerView .

In 2015, I created a RecyclerView dividing line library called RecyclerViewDecoration . Although the 1.x version has many properties, it is not very convenient to use. It was not until 2017 that I changed to version 2.x of the Builder method, and the algorithm was also optimized. At the same time, some small partners gave me some new requirements. So far, there are still some new requirements, and I will update these issues in the future.

Including ExpandRecyclerView to be introduced now , the content of RecyclerViewDecoration has been merged here since version 1.3 . There will be more APIs that combine dividing lines and adapters to appear. The purpose of this library is very simple, which is to minimize the amount of code of users in the process of use. If you don't know how to use it, you can refer to the example in the sample. The following is just a brief introduction.

ExpandRecyclerView currently mainly contains several parts:

Adapter

1.RecyclerViewAdapter

This adapter can support a single item and multiple items.

Example: (single item)

RecyclerViewAdapter adapter = new RecyclerViewAdapter<>(this, Arrays.asList(titles)
    , R.layout.item_main_list
    , new RecyclerViewSingleTypeProcessor<String>() {
        @Override
        public void onBindViewHolder(RecyclerViewViewHolder holder, final int position, String str) {
            TextView textView = holder.getView(R.id.tv_content);
            textView.setText(str);
        }
    });

Example: (multiple items)

mAdapter = new RecyclerViewAdapter<>(this, mDataList
    , new int[]{R.layout.item_list_type0, R.layout.item_list_type1}
    , new RecyclerViewMultipleTypeProcessor<Car>() {

        @Override
        public void onBindViewHolder(RecyclerViewViewHolder holder, int position, Car object) {
            if(getItemViewType(position) == 0) {
                TextView textView = holder.getView(R.id.tv_content);
                textView.setText(object.getBrand() + "/" + object.getTypeName());
            }else{
            }
        }

        @Override
        public int getItemViewType(int position) {
            //define two viewTypes
            if (position % 2 == 0)
                return 1;
            return 0;
        }
    });

2.RecyclerViewGroupAdapter

It is important to note that the first item of this Adapter must be a group type.

example:

mGroupAdapter = new RecyclerViewGroupAdapter<>(this, mDataList
                , new int[]{R.layout.item_group_type, R.layout.item_list_type1}
                , new RecyclerViewGroupTypeProcessor<Car>() {


            @Override
            public void onBindGroupViewHolder(RecyclerViewViewHolder holder, int groupPosition, Car car) {
                TextView tvGroup = holder.getView(R.id.tv_group);
                tvGroup.setText(car.getGroup());
            }


            @Override
            public void onBindItemViewHolder(RecyclerViewViewHolder holder, final int groupPosition, final int itemPosition, Car car) {
                TextView tvContent = holder.getView(R.id.tv_content);
                tvContent.setText("Car brand:" + car.getBrand() + " / type: " + car.getTypeName());


                tvContent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(GroupListActivity.this
                                , "Group: " + groupPosition + "\titemPosition: " + itemPosition, Toast.LENGTH_SHORT).show();
                    }
                });
            }


            @Override
            public int getItemViewType(int position) {
                if (mDataList.get(position).getGroup() != null)
                    return 0;
                return 1;
            }
        });

ItemDecoration

Previously introduced https://blog.csdn.net/arjinmc/article/details/74508483

There is also the new RecyclerViewGroupItemDecoration grouping divider. One of its usages can be seen in the GroupGridActivity in the sample.


Style

Convert some common RecyclerView layouts.


For more details about the API, please check the wiki .

If you find a bug, or you have any new ideas, please email me. [email protected]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324605372&siteId=291194637