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

Keywords: Java github xml

v3.1 Update

  • Perfect more drop-down refresh scenes
  • Add [pull-up paging load] and [insensitive paging load]

v3.0 Update

  • Add a drop-down refresh with super power

v2.0 Update

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

brief introduction

Non-constrained list,
It is also the "universal adapter" in the mouth of many students.
But I don't agree with the statement that everything works.
Whose adapters ListView and RecyclerView are common?

Preview the effect first:
1. Single column list

2. Multi-column list

3. Mixed List

Obviously,
I don't see anything on the interface.
Let's start with the advantages of my unconstrained list.

For example,
A middle school wants to upgrade 10 computers.
An average computer takes one hour to operate at a time.

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

High school students might connect all their computers.
At the same time,
Only once,
It takes an hour.

In the words of ordinary Adadapter,
Like a junior high school student,
Modify one entry at a time.
Change all the classes that use this entry.
Whether it's an Activity or an Adapter,
But using an unconstrained list is different.
Logic is basically in Bean s.
Basically you just need to change the style of itemLayout.
And the logic in Bean.

Come here,
There may be puzzles from the classmates.
Why not ignore the Adapter?
Because,
My project only needs one Adapter,
An Adapter,
All-round!

To put it in more detail,
That's when I break up and reorganize all the logic,
Adapter is overhead,
Only responsible for the connection between Activity, ViewHolder, bean s.
I personally like to put View and its corresponding data together,
So I put ViewHolder and logic in Bean,
Combine the two through an interface.
Oh no,
Is to display data on the View.

However,
So many ViewHolder s,
How does the Adapter connect it?
So I wrote a ViewHolderManager,
ViewHolderManager is responsible for
Find the corresponding ViewHolder,
Instantiate it and return it to the Adapter.

The overall process is as follows:

Okay,
And that's how it works.
This is the splitter line for cleaning up memory.
Here's how to use my unconstrained list:

One:
Create a new Bean class,
Write a ViewHolder inner class inside,
Inherit ViewHolderManager.ViewHolder,
Similar to the internal class of the normal Adapter,
Notice, however,
The parameters in the construction are different:

/**
 * ViewHolder --> Buttons for 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 interpret, the second is the 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,
There are more parameters in the construction.
That is, the LayoutXml corresponding to the ViewHolder.

Two:
Enter ViewHolderManager,
For your new ViewHolder,
Add an open static constant,
As long as it does not repeat with other values,
And put it into the itemMap,
key is the constant you just wrote,
value is the class of the ViewHolder you just wrote.

/**
 * Entry Type-->...
 */
public static final int //……
/**
 * Entry Type-->News Card
 */
public static final int ITEM_TYPE_NEWS = 8;
/**
 * Entry 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 you just wrote,
Inherit FreedomBean,
In initItemType(),
set up the constant just defined in ViewHolderManager.

@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 Activity,
Activity implements the FreedomCallback interface.
And write code in onClickCallback,
Then go back to Bean and call getCallback(activity).onClickCallback();

vh.rl_music.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Click Event
        // If you don't need to interact with Activity,
        // Write the click event directly here
        //
        // If you need to interact with Activity,
        // Then Activity implements the FreedomCallback interface,
        // And write code in onClickCallback,
        // This triggers the callback.
        // Interact with Activity.
        //
        // Be careful:
        // This Activity must implement the FreedomCallback interface to trigger callbacks.
        // Otherwise, an error will be made
        getCallback(activity).onClickCallback(v, position, vh);
    }
});

Example:
vh.rl_music.setOnClickListener() in initBindView method of class BeanMusic.

Okay,
Bean is done,
There is no difference in how it is used in Activity.
Just when declaring the data source mList,
Type Object or don't type it:

/**
 * data source
 */
 private List mList;

Then you can use FreedomAdapter:

// 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 normal, isn't it?

The code is simple,
I wrote all the notes,
If you still have questions,
Welcome to comment under the article,
Or join the QQ discussion group: 569614530,
Find me in the group,
I'm dusty.

This article github link:
https://github.com/Bamboy1203...

You can also download the apk installation experience first:

Posted by Cleibe on Thu, 09 May 2019 19:12:38 -0700