Bamboy unconstrained list (commonly known as universal adapter) (with source code)

Time:2019-10-8

V3.1 update

  • Perfect more drop-down refresh scenarios
  • Add [Pull-Up Paging Loading] and [Senseless Paging Loading]

V3.0 update

  • Add pull-down refresh with super-strong function

V2.0 update

  • Increase support for team development
  • Increase support for mixed lists

brief introduction

Unconstrained list,
It is also what many students call “universal adapter”.
But I don’t agree with the idea of omnipotence.
Whose adapters ListView and RecyclerView are common?

Preview the effect first:
1. Single column list
Bamboy unconstrained list (commonly known as universal adapter) (with source code)

2. Multi-column list
Bamboy unconstrained list (commonly known as universal adapter) (with source code)

3. Mixed List
Bamboy unconstrained list (commonly known as universal adapter) (with source code)

Obviously,
Nothing can be seen from the interface.
Let me start with the advantages of my unrestricted list.

Let’s make an analogy.
A secondary school has to upgrade 10 computers.
An average computer takes one hour to operate.

Junior high school students may operate 10 times.
It takes 10 hours.

High school students may connect all the computers.
At the same time,
It only needs to be operated once.
It takes an hour.

In the words of ordinary Adapter,
Like junior high school students,
Modify one entry at a time.
Change all the classes that use this entry.
Whether it’s Activity or Adapter,
But using unconstrained lists is different.
Logic is basically in beans.
Basically, it only needs to change the style of itemLayout.
And the logic in Bean.

Come here,
There may be confusion among the students.
Why not Adapter?
Because,
My project only needs an adapter.
An adapter,
All-in-all!

To be more specific,
It is I who break up and reorganize all the logic.
Adapter overhead,
Only responsible for the connection between Activity, ViewHolder and beans.
Personally, I like to put View together with the data corresponding to it.
So I put ViewHolder and logic in Beans.
It combines the two through an interface.
Oh no,
Display the data on the View.

However,
There are so many ViewHolders,
How does Adapter connect it?
So I wrote a ViewHolder Manager.
ViewHolder Manager is responsible for the type of entry.
Find the corresponding ViewHolder,
Instantiate and return to Adapter.

The overall process is as follows:

Okay,
That’s the principle.
This is the partition line for cleaning up memory.
Let’s start with the steps to use my unconstrained list:

1:
Create a new bean class.
Write a ViewHolder inner class inside.
Inheriting ViewHolderManager. ViewHolder,
Similar to the internal classes of ordinary Adadapters,
However, it should be noted that:
The parameters in the structure are different:

/**
 * ViewHolder - > Button on Home Page
 */
public static class MusicViewHolder extends ViewHolderManager.ViewHolder {
    public RelativeLayout rl_music;
    public TextView tv_song;
    public TextView tv_singer;

    public MusicViewHolder(ViewGroup viewGroup) {
        // Two parameters, the first viewGroup does not explain, and the second is LayoutXml corresponding to this ViewHolder
        super(viewGroup, R.layout.item_music);

        rl_music = (RelativeLayout) itemView.findViewById(R.id.rl_music);
        tv_song = (TextView) itemView.findViewById(R.id.tv_song);
        tv_singer = (TextView) itemView.findViewById(R.id.tv_singer);
    }
}

It’s not hard to find out.
There is an additional parameter in the structure.
That is Layout Xml corresponding to the ViewHolder.

Two:
Enter ViewHolder Manager,
For your newly written ViewHolder,
Add an open static constant.
As long as it does not duplicate other values,
Put it into itemMap.
Key is the constant you just wrote.
Value is the class of ViewHolder just written.

/**
 * Item Type - >...
 */
public static final int //……
/**
 * Item Type - > News Card
 */
public static final int ITEM_TYPE_NEWS = 8;
/**
 * Item Type - > Music
 */
public static final int ITEM_TYPE_MUSIC = 9;

/**
 * Load entry type and corresponding entry XML
 */
static {
    itemMap = new HashMap<>();
    ……
    itemMap.put(ITEM_TYPE_NEWS, BeanNews.NewsViewHolder.class);
    itemMap.put(ITEM_TYPE_MUSIC, BeanMusic.MusicViewHolder.class);
}

Three:
Back to the Bean I just wrote,
Inheriting Freedom Bean,
In initItemType (),
Set the constant just defined in ViewHolder Manager.

@Override
protected void initItemType() {
    setItemType(ViewHolderManager.ITEM_TYPE_MUSIC);
}

In initBindView (),
Move the onBindViewHolder () code from the regular adapter.

@Override
protected void initBindView(final List list) {

    setViewHolderBindListener(new ViewHolderBindListener() {
        @Override
        public void onBindViewHolder(final Activity activity, final ViewHolderManager.ViewHolder viewHolder, final int position) {
            final MusicViewHolder vh = (MusicViewHolder) viewHolder;
            final BeanMusic bean = (BeanMusic) list.get(position);

            vh.tv_song.setText(bean.getSong());
            vh.tv_singer.setText(bean.getSinger());

            vh.rl_music.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ……
                }
            });
        }
    });
}

If you need to interact with Active,
Activity implements the Freedom Callback interface.
And write code in onClickCallback.
Then go back to the bean and call getCallback (activity). onClickCallback ();

vh.rl_music.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Click Events
        // If you don't need to interact with Activity,
        // Then just write the click event right here.
        //
        // If you need to interact with Activity,
        // Then Activity implements the Freedom Callback interface.
        // And write code in onClickCallback.
        // The callback can be triggered.
        // Interact with Activity.
        //
        // Note:
        // The activity must implement the Freedom Callback interface to trigger callbacks.
        // Otherwise, it will make a mistake.
        getCallback(activity).onClickCallback(v, position, vh);
    }
});

Example:
Vh. rl_music. setOnClickListener () in initBindView method in BeanMusic.

Okay,
Bean is done.
There is no difference between the way it is used in Activity and the ordinary one.
Just when declaring the data source mList,
Type Object or do not add type:

/**
 * data sources
 */
 private List mList;

Then you can use Freedom Adapter:

// Instantiate RecyclerView
mAdapter = new FreedomAdapter(this, mList);
recycler.setLayoutManager(new LinearLayoutManager(this));
recycler.setItemAnimator(new DefaultItemAnimator());
recycler.setAdapter(mAdapter);

It’s not much simpler to use than normal.
But it’s much more powerful than ordinary, isn’t it?

The code is simple.
I also wrote the notes very well.
If there are still doubts,
Welcome to comment under the article.
Or join the QQ discussion group: 569614530,
Look for me in the crowd.
I am dusty.

This article’s GitHub link:
https://github.com/Bamboy1203…

You can also download the APK installation experience first: