gpt4 book ai didi

android - StaggeredGridLayoutManager 和移动项目

转载 作者:IT老高 更新时间:2023-10-28 22:20:50 29 4
gpt4 key购买 nike

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

代码:

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

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

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

编辑

@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());
}

最佳答案

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

mRecyclerView.setItemAnimator(null);

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

关于android - StaggeredGridLayoutManager 和移动项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27636999/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com