StaggerdRecyclerView
android cachoeira, solução perfeita processo correr confusão localização do item, pull-down top branco de atualização, carga superior mais desordem e outras questões, você pode conseguir efeitos de animação
renderings
integrado
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.moo611:StaggerdRecyclerView:latestversion'
}
uso Básico
1. Adicione arquivo de layout
<com.atech.staggedrv.StaggerdRecyclerView
android:id="@+id/str"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
2. Seu modelo, a necessidade de alcançar 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. O adaptador, necessidade de staggedadapter herdar
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. Seu viewholder, e a redação do recyclerview ao mesmo tempo.
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();
...
outras características
animação
str.addAnimation(R.anim.right_to_left);
espaçamento Set
str.addDecoration(new GridItemDecoration(this,10));
proibição de atualização
str.enableRefresh(false);
Mais de carregamento
str.enableLoadMore(false);
princípio
Quando o distúrbio posição de deslizamento
Em Recyclerview de correr, porque o mecanismo de cache reutilização, ponto irá redesenhar, se você é largura ImageView inseguro e altura, ele irá causar confusão e pisca.
Quando você atualizar a parte superior do branco
Quando se utiliza método notifyDataSetChanged () para fazer uma actualização desencadeada StaggeredGridLayoutManager de OnItemsChanged método, resultando no item calculado de spanIndex reproduzem, coluna localização item foi alterado, que conduz à parte superior da peça em bruto. As mudanças não causam a atualização parcial com notifyItemRangeInserted, notifyItemRangeChanged.
referência
https://www.jianshu.com/p/d34075c0f287