Gridview settings reorder using RecyclerView
The gridview items change order randomly when I click on the items and mostly when scrolling. I've searched all over the internet but couldn't find a solution that works for recyclerview.
This is my layout adapter class
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();
}
}
This is the layout file
<?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>
source to share
Could you post the layout definitions as well? We already know that if items do not have fixed or predefined dimensions, then items can shift order in the case of TwoWay / Recycler views.
This is because when we view the reseller, the old element views are reused when we scroll down, and the old element sizes may differ from the new element sizes. Hence, they can be reordered.
In this case, setting fixed sizes for your ImageView and TextView will fix the problem or determine the size of the ImageView before inflating.
source to share
I faced the same problem which is / was really annoying. I checked several things and one did the trick: I don't know why, but you have to explicitly set the element animator to zero to represent the recycler with [RecyclerView].setItemAnimator(null)
; ... Especially the first elements are reordered when scrolling up in my checkerboard layout. Now he's gone. Hope this helps you too.
source to share