Original statement: This article is an original article, it is strictly prohibited to reprint without the consent of the blogger. Abstract: In the process of using RecyclerView, the author found that RecyclerView actually has more possibilities, there are also a lot of excellent projects about RecyclerView on Github. This is an open source project initiated by the author. At present, this project is relatively simple. For this open source library, the author expects to gradually add some more interesting and efficient ways to use it. If you are interested in this project, please join me in the development and maintenance of this library. There’s no limit to how you can participate. You can submit your own code, notify the author of some interesting features (I’ll implement them based on the actual situation), and improve your project’s README file.

The topic

The library currently supports wrapped Adapters, adapters that support secondary list functionality, and well-developed splitter classes (are you sure you’re not fishing?). . The project address is: github.com/DobbyTang/R…

SimpleAdapter

SimpleAdapter is used to quickly implement Adapter development, which supports the following functions

  1. Add tops and bottoms quickly, and you can listen for events at the bottom and top as needed
  2. Maintain list data
  3. Implement click events
  4. The top and bottom of SimpleAdapter is dependent on the RecyclerView life cycle, avoiding memory overflow and some Fragment scenarios will cause crashes.

SecondaryAdapter

The purpose of a SecondaryAdapter is to implement a secondary list Adapter. Can be used in some special scenarios (shopping cart, secondary classification list, etc.).

SimpleDecoration

The slitter class is used to quickly add slitters and support multiple effects.

  1. A line segment
  2. Transparent divider
  3. Custom image divider line
  4. Custom secant width

When referring to some online code for dividing lines, the author found that when the width of dividing lines is too large, there will be a problem: when using table-type layout, the size of the first or last item will be affected because the dividing line will occupy a certain space. The text may be difficult to understand, you can take a look at the following diagram:



You can see the problem here.

The principle of dividing line drawing is to calculate the position occupied by dividing line by recalculating the space occupied by Item, and then draw line segments on these positions. The author draws the vertical dividing line in the figure above by calculating the position of the dividing line on the right side of the ordinary Item. If the Item is in the last column, the dividing line on the right side is not drawn. Therefore, when the number of columns of item is N, the vertical dividing line to be drawn is N-1.

This is ok when the width of the splitter line is small. However, when the width of the splitter line is large, we will find that the width value of the last item is actually larger than that of the other items because no splitter line is drawn at the position of the last column. This causes the problem of inconsistent item widths.

SimpleDecoration solves these problems and extends the split-line functionality, so let’s see what SimpleDecoration does in practice.



The width of the dividing line in the figure above is 32dp, so it is obvious that the size of each item is the same. However, it should be noted that since the position and size of the vertical divider have been recalculated, the width of the vertical divider will be a little larger than 32DP.

Results show

Due to time constraints, the author has written a simple demo for your reference. If you are interested, you can go to the author’s Github to download the project.

SimpleAdapter



SecondaryAdapter

Method of use

Import libraries

First add Maven {url ‘https://jitpack.io’} under AllProjects -> Repositories in the build.gradle file in the project directory as shown below

allprojects {  
    repositories {  
        ...  
        maven { url 'https://jitpack.io' }  
    }  
}Copy the code

Then add the dependencies to the build.gradle file in the module directory where you want to use the library.

Dependencies {the compile 'com. Making. DobbyTang: RecyclerUtil: 1.0.3'}Copy the code

SimpleAdapter use

public class TestSimpleAdapter extends SimpleAdapter{ @Override public RecyclerView.ViewHolder onCreateNormalView(ViewGroup parent) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemuser,parent,false); return new UserHolder(view); } @Override public void onBindNormalView(RecyclerView.ViewHolder normalHolder, int position, UserBean value) { ((UserHolder) normalHolder).name.setText(value.name); ((UserHolder) normalHolder).age.setText(value.age + ""); ((UserHolder) normalHolder).sex.setText(value.sex); } private class UserHolder extends RecyclerView.ViewHolder{ TextView name; TextView age; TextView sex; public UserHolder(View itemView) { super(itemView); name = (TextView) itemView.findViewById(R.id.itemusername); age = (TextView) itemView.findViewById(R.id.itemuserage); sex = (TextView) itemView.findViewById(R.id.itemusersex); }}}Copy the code

It can be seen that the use of SimpleAdapter and ordinary RecyclerView.Adapter use method is about the same, but SimpleAdapter is much simpler. We just define a ViewHolder and override the onCreateNormalView() and onBindNormalView() methods to complete a simple Adapter. If you want to add headers and footers, you can simply override the setHeaderView() and setFooterView() methods. If you need to listen or bind to header and footer, just overwrite onBindHeaderView() and onBindFooterView(), respectively. The complete code is as follows:

@Override public int setHeaderView() { return R.layout.header_user; } @Override public int setFooterView() { return R.layout.footer_user; } @Override public void onBindHeaderView(View header) { header.findViewById(R.id.header_button) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Snackbar.make(v," click Button on Header ", snackbar.length_short).show(); }}); } @Override public void onBindFooterView(View footer) { footer.findViewById(R.id.footer_button) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Snackbar.make(v," click Button on Footer ", snackbar.length_short).show(); }}); }Copy the code

After completing the Adapter, use the same method as a normal Apapter.

adapter = new TestSimpleAdapter();  
list.setLayoutManager(new LinearLayoutManager(getActivity()));  
list.setAdapter(adapter);Copy the code

When we need to set Adapter data, we simply call the SimpleAdapter setData() method. For more information, see the source code comment.

SecondaryAdapter use

@Override  
public RecyclerView.ViewHolder onCreateGroupHolder(ViewGroup parent) {  
    View view = LayoutInflater.from(parent.getContext())  
            .inflate(R.layout.friendgroupitem,parent,false);  
    return new TitleHolder(view);  
}  
  
@Override  
public RecyclerView.ViewHolder onCreateChildHolder(ViewGroup parent) {  
    View view = LayoutInflater.from(parent.getContext())  
            .inflate(R.layout.friendsubitem,parent,false);  
    return new FriendHolder(view);  
}  
  
@Override  
public void onBindGroupHolder(RecyclerView.ViewHolder holder, FriendGroupBean friendGroupBean) {  
    ((TitleHolder)holder).groupName.setText(friendGroupBean.title);  
}  
  
@Override  
public void onBindChildHolder(RecyclerView.ViewHolder holder, UserBean userBean) {  
    ((FriendHolder) holder).name.setText(userBean.name);  
    ((FriendHolder) holder).age.setText(userBean.age + "");  
    ((FriendHolder) holder).sex.setText(userBean.sex);  
    if (userBean.imgId != 0){  
        ((FriendHolder) holder).avatar.setImageResource(userBean.imgId);  
    }else {  
        ((FriendHolder) holder).avatar.setImageResource(R.mipmap.iclauncher);  
  
    }  
}  
  
public static class TitleHolder extends RecyclerView.ViewHolder{  
  
    TextView groupName;  
  
    public TitleHolder(View itemView) {  
        super(itemView);  
        groupName = (TextView) itemView.findViewById(R.id.title);  
    }  
}  
  
private class FriendHolder extends RecyclerView.ViewHolder{  
  
    private ImageView avatar;  
    private TextView name;  
    private TextView age;  
    private TextView sex;  
  
    public FriendHolder(View itemView) {  
        super(itemView);  
        avatar = (ImageView) itemView.findViewById(R.id.friendsubavatar);  
        name = (TextView) itemView.findViewById(R.id.friendsubname);  
        age = (TextView) itemView.findViewById(R.id.friendsubage);  
        sex = (TextView) itemView.findViewById(R.id.friendsubsex);  
    }  
}Copy the code

SecondaryAdapter is also very simple to use. The difference is that you need to write two Viewholders and create the layout separately, and use the same method as a normal Adapter. One thing to notice is how the data is set.

List data;  
List> userList = new ArrayList<>();  
adapter.setData(data,userList);
Copy the code

The first argument to setData() is List\, the second argument is List\ >, and the second argument is a two-dimensional List. For more detailed usage, readers can view the source code comments.

SimpleDecoration use

SimpleDecoration provides several factory methods:

  1. NewTransparentDivider (Context Context), creates a default transparent divider with a default width of 16dp
  2. NewTransparentDivider (Context Context, int interva), creates a transparent divider of the specified width
  3. NewLinesDivider (Context Context), which creates a common divider. The default width is 1dp
  4. NewLinesDivider (Context Context, int interval) Creates a common divider of a specified width
  5. NewDrawableDivider (Context Context, int interval, @drawableres int drawableId), which creates a divider with the specified picture and the specified width.
  6. NewDrawableDivider (Context Context, int interval, Drawable Drawable) same as above

Create the needed division line object through the factory method, call RecyclerView addItemDecoration() to add to RecyclerView.

summary

This is the current RecyclerViewUtil tool class to provide the main function, although there are few functions, but these are the problems we often encounter in daily development. In the follow-up maintenance, the author will further provide the Fragment and Activity related to RecyclerView encapsulated. I also hope that readers in the process of using any interesting ideas and suggestions, you can contact me for development, the author will consider the development. Of course, it would also be nice to initiate pull Requests on Github to work together on the project.

Android architecture – from scratch to build a perfect MVP mode development framework (a), MVP mode of a simple introduction to Android SearchView advanced usage, Using Fresco to implement Android image gesture zooming