android waterfall, perfect solution sliding process item location confusion, pull-down refresh blank top, top load more disorder and other issues

StaggerdRecyclerView

android waterfall, perfect solution sliding process item location confusion, pull-down refresh blank top, top load more disorder and other issues, you can achieve animation effects

Renderings

Here Insert Picture Description

integrated

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

dependencies {
	        implementation 'com.github.moo611:StaggerdRecyclerView:latestversion'
	}

Basic Usage

1. Add layout file


<com.atech.staggedrv.StaggerdRecyclerView
    android:id="@+id/str"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

2. Your model, the need to achieve StaggedModel Interface

//示例 example
public class FakeModel implements StaggedModel {
 
    private int width;
    private int height;
    private int resourceId;

    public FakeModel(int width, int height, int resourceId){
        this.width = width;
        this.height = height;
        this.resourceId = resourceId;
    }



    @Override
    public int getWidth() {
        return width;
    }

    @Override
    public int getHeight() {
        return height;
    }

    @Override
    public String getTitle() {
        return null;
    }

    @Override
    public String getThumb() {
        return null;
    }

    @Override
    public int localResorce() {
        return resourceId;
    }
}

3. Your adapter, need to inherit staggedadapter

  class MyAdapter<T extends StaggedModel> extends StaggedAdapter<T> {

        MyAdapter(Context c) {
            super(c);
        }


        @Override
        public RecyclerView.ViewHolder addViewHolder(ViewGroup viewGroup, int i) {
            //绑定自定义的viewholder
            View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.custom_item_layout,viewGroup,false);
            return new MyHolder(v);
        }

        @Override
        public void bindView(RecyclerView.ViewHolder viewHolder, int i) {

            MyHolder myHolder = (MyHolder)viewHolder;


            // 在加载图片之前设定好图片的宽高,防止出现item错乱及闪烁
            ViewGroup.LayoutParams layoutParams = myHolder.img.getLayoutParams();
            layoutParams.height = datas.get(i).getHeight();
            myHolder.img.setLayoutParams(layoutParams);

            myHolder.img.setImageResource(datas.get(i).localResorce());

        }


    }

4. Your viewholder, and the wording of recyclerview the same time.

class MyHolder extends RecyclerView.ViewHolder{

        ImageView img;

        public MyHolder(@NonNull View itemView) {
            super(itemView);

            img = itemView.findViewById(R.id.img);

        }
    }

5.mainactivity

MyAdapter<FakeModel> staggedAdapter;
StaggerdRecyclerView str;
private List<FakeModel> datas = new ArrayList<>();
...


        str = findViewById(R.id.str);
        staggedAdapter = new MyAdapter<>(this);
        str.link(staggedAdapter,2);

      
        str.addCallbackListener(new LoadMoreAndRefresh() {
            @Override
            public void onLoadMore() {
                loadMore();//加载更多
            }

            @Override
            public void onRefresh() {

                refresh();//下拉刷新
            }
        });

        refresh();

...

Other functions

Animation

str.addAnimation(R.anim.right_to_left);

Set spacing

str.addDecoration(new GridItemDecoration(this,10));

Refresh ban

str.enableRefresh(false);

More from loading

str.enableLoadMore(false);

principle

When the slide position disorder

In Recyclerview sliding, because the cache reuse mechanism, Item will redraw, if you are unsure imageview width and height, it will cause confusion and position flashes.

When you refresh the top of the blank

When using notifyDataSetChanged () method to do a refresh triggered StaggeredGridLayoutManager of onItemsChanged method, resulting in calculated item of spanIndex reproduce, item location column has changed, leading to the top of the blank. The changes do not cause the partial refresh with notifyItemRangeInserted, notifyItemRangeChanged.

reference

https://www.jianshu.com/p/d34075c0f287

Released six original articles · won praise 4 · Views 1379

Guess you like

Origin blog.csdn.net/qq_39678305/article/details/105180336