当我点击项目时,有时候滚动时,gridview项目会随机改变顺序。我在互联网上搜索过但无法找到适用于recyclerview的解决方案。
这是我的布局适配器类
public class LayoutAdapter extends RecyclerView.Adapter<LayoutAdapter.SimpleViewHolder>{
private static final int COUNT = 100;
private static final String TAG = "LayoutAdapter";
private final Context mContext;
private final TwoWayView mRecyclerView;
private final int mLayoutId;
private int mCurrentItemId = 0;
private FileManager file_manager;
private ArrayList<Integer> positions;
private static LayoutInflater inflator = null;
private TextView folder_name;
private Context c;
ArrayList<String> mDataSource, mMultiSelectData;
private File file, files;
private File[] list2;
public boolean multi_select_flag = false;
public class SimpleViewHolder extends RecyclerView.ViewHolder {
public SimpleViewHolder(View view) {
super(view);
thumbnail = (ImageView) view.findViewById(R.id.ivFolderThumbnail);
folder_name = (TextView) view.findViewById(R.id.tvFolderTitle);
}
}
//In logcat I see this constructor is being called again and again and again!
public LayoutAdapter(Context context, TwoWayView recyclerView, int layoutId, FileManager filemanager, String file_path) {
mContext = context;
file_manager = filemanager;
c = context;
mDataSource = new ArrayList<String>(file_manager.setHomeDir
(Environment.getExternalStorageDirectory().getPath()));
mRecyclerView = recyclerView;
mLayoutId = layoutId;
String root_sd = Environment.getExternalStorageDirectory().toString();
if (file_path == null) {
file = new File(root_sd);
} else {
file = new File(root_sd + "/" + file_path);
}
Log.d(TAG, "GOT ALL FILES >>>>>>" + root_sd);
list2 = file.listFiles();
}
public String getName(int position) {
String name = list2[position].getName();
return name;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(mContext).inflate(R.layout.grid_item, parent, false);
return new SimpleViewHolder(view);
}
public String getData(int position) {
if (position > mDataSource.size() - 1 || position < 0)
return null;
return mDataSource.get(position);
}
@Override
public void onBindViewHolder(SimpleViewHolder holder, int position) {
// boolean isVertical = (mRecyclerView.getOrientation() ==TwoWayLayoutManager.Orientation.VERTICAL);
final View itemView = holder.itemView;
int num_items = 0;
String temp = file_manager.getCurrentDir();
File file = new File(temp + "/" + mDataSource.get(position));
String[] list = file.list();
if (list != null)
num_items = list.length;
Log.d(TAG, ">>>>>>>>>>>>> " + String.valueOf(file.length()));
folder_name.setText(file.getName());
}
}
@Override
public int getItemCount() {
return mDataSource.size();
}
}
这是布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="105dp"
android:layout_height="105dp"
android:orientation="vertical" >
<com.filemanager.android.SquareImageView
android:id="@+id/ivFolderThumbnail"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginLeft="15dp"
android:scaleType="centerCrop"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/tvFolderTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/ivFolderThumbnail"
android:text="Medium Text"
android:textColor="@android:color/black"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/ivMultiSelect"
android:layout_width="50dp "
android:layout_height="50dp"
android:layout_above="@+id/tvFolderTitle"
android:layout_toRightOf="@+id/ivFolderThumbnail"
android:src="@drawable/ic_action_new" />
</RelativeLayout>
答案 0 :(得分:1)
您也可以发布布局定义吗?我们已经知道,如果商品没有固定或预定义的尺寸,那么在TwoWay / Recycler Views的情况下,商品可能会改变订单。
发生这种情况是因为在回收站视图中,当我们向下滚动并且旧项目维度可能与新项目维度不同时,会重复使用旧项目视图。因此他们可能会重新排序。
在这种情况下,为ImageView和TextView设置固定尺寸可以解决问题或在充气前确定ImageView的尺寸。
答案 1 :(得分:1)
我遇到了同样的问题,这真的很烦人。我测试了几个东西,其中一个做了诀窍:我不知道为什么,但是你必须使用[RecyclerView].setItemAnimator(null)
将物品动画显式设置为null为回收器视图; ...尤其是在交错布局中再次向上滚动时重新排序的第一项。现在它消失了。希望这对你也有帮助。
答案 2 :(得分:0)
是的,这很烦人;我已经意识到的解决方案是在适配器中清除所有条件,例如每个if条件也会有其他有效的主体。 尝试这种方法,你将摆脱这个问题。