27

我创建了一个非常简单的项目,StaggeredGridLayoutManager通过 recyclerview 显示 28 张图像。但是当我滚动recyclerview时,它会例如从左到右移动项目或交换左右列。

代码:

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;


public class MainActivity extends Activity {


    String mImageDir;
    private RecyclerView mRecyclerView;
    private StaggeredGridLayoutManager mLayoutManager;



    MyRecyclerAdapter myRecyclerAdapter;
    List<ImageModel> mImageList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);   
        mRecyclerView = (RecyclerView)findViewById(R.id.recyclerview_rootview);
        mLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);     
        mLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);        
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setHasFixedSize(false);
        mImageList = new ArrayList<ImageModel>();
        for (int i = 1; i < 29 ; i++) {
            ImageModel img = new ImageModel();
            img.setTitle("Image No " + i);
            int drawableResourceId = this.getResources().getIdentifier("image"+String.valueOf(i), "drawable", this.getPackageName());
            img.setResId(drawableResourceId);
            mImageList.add(img);

        }
        myRecyclerAdapter = new MyRecyclerAdapter(MainActivity.this,mImageList);        
        mRecyclerView.setAdapter(myRecyclerAdapter);

    }

} 

和适配器:

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> {


    private List<ImageModel> mItems;
    Context mContext;

    public MyRecyclerAdapter(Context context,List<ImageModel> objects) {
        mContext = context;
        mItems = objects;

    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        public  ImageView mImageView;
        public  TextView mTextView;
        public View rootView;
        public ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView;
            mImageView =(ImageView)itemView.findViewById(R.id.image);
            mTextView =(TextView)itemView.findViewById(R.id.title);
        }
    }

    @Override
    public int getItemCount() {

        return mItems.size();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        ImageModel item = mItems.get(position); 
        Picasso.with(mContext).load(item.getResId()).into(holder.mImageView);
        holder.mTextView.setText(item.getTitle());
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int arg1) {
        LayoutInflater inflater =    
                (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View convertView = inflater.inflate(R.layout.item, parent, false);
        return new ViewHolder(convertView);

    }

}

和一个示例移动项目:

http://i.imgur.com/FUapm2K.gif?1

如果你玩(上下滚动)你会发现更多有趣的动画:-)

如何防止这种情况并像普通列表视图一样拥有稳定的布局?

编辑

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    ImageModel item = mItems.get(position);
    RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams)holder.mImageView.getLayoutParams();
    float ratio = item.getHeight()/item.getWidth();
    rlp.height = (int)(rlp.width * ratio);
    holder.mImageView.setLayoutParams(rlp);
    Picasso.with(mContext).load(item.getResId()).into(holder.mImageView);
    holder.mTextView.setText(item.getTitle());
}
4

8 回答 8

20

对我有用的是在使用 StaggeredGridLayoutManager 时禁用回收视图上的所有动画。

mRecyclerView.setItemAnimator(null);

如果您只想限制移动动画并保留添加和删除项目动画,则可以创建自己的动画师。

于 2016-03-02T13:14:57.233 回答
19

发生这种情况是因为 SGLM 不保留有关视图的任何 w/h 信息。所以每次一个 View 被反弹时,它首先获取占位符大小,然后是加载图像时的最终大小。

加载具有不同大小(而不是占位符)的实际图像会触发新的布局请求,并且在该布局请求期间,SGLM 检测到 UI 中会有间隙(或者某些具有较高位置的项目出现在具有较低位置的项目下方position) 因此重新排序带有动画的项目。

您可以通过将占位符图像设置为实际图像的尺寸来避免它。如果您没有提前获得,您可以在第一次加载图像后保存它们,并在下一次 onBind 调用中使用它。

于 2014-12-31T09:38:11.770 回答
17

你可以试试这个

    StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL);
    manager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
    mRecyclerView.setLayoutManager(manager);

设置好后,当你滚动到顶部时,你会发现顶部有一个空白。继续设置这个

    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            ((StaggeredGridLayoutManager)recyclerView.getLayoutManager()).invalidateSpanAssignments();
        }
    });

这对我有用,我在某个地方得到了这种方式。我希望它可以帮助你!

于 2016-10-24T08:05:46.857 回答
6

更改此行

mLayoutManager.setGapStrategy(
        StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS); 

到这条线

mLayoutManager.setGapStrategy(
        StaggeredGridLayoutManager.GAP_HANDLING_NONE);           
于 2014-12-30T02:08:20.843 回答
5

在方法末尾添加以下行: holder.mImageView.requestLayout();

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    ...
    holder.mImageView.requestLayout();
}

这为我解决了这个问题。

于 2016-04-27T11:39:41.480 回答
1

也许这是一种更有效的方法:

mBrandRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            if(newState == RecyclerView.SCROLL_STATE_IDLE){
                mBrandRecyclerView.invalidateItemDecorations();
            }
        }
    });

更多信息: https ://github.com/ibosong/CommonItemDecoration

于 2017-04-01T07:06:20.310 回答
0

为 staggered-layout-manager 设置 recyclerview 的固定高度和项目高度和宽度 wrap-content

于 2018-11-14T10:34:42.863 回答
0

首先设置差距策略,如以下代码:

mLayoutManager = new StaggeredGridLayoutManager(SPAN_COUNT, StaggeredGridLayoutManager.VERTICAL);
                            mLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);

然后将您的项目添加到mItems然后使用:

mAdapter.notifyItemInserted(mItems.size() - 1);

这种方法比使用更好:

mAdapter.notifyDataSetChanged();
于 2018-08-17T06:43:55.070 回答