What I'm trying to create is the horizontal scrolling of the image gallery. I have a RecyclerView (support 22.0.0). The problem I am facing is that when I scroll to the end and then scroll back, usually one image will sometimes be missing two. Oddly enough, when I hold back and forth, another image may be missing. Here is the layout for the item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="160dp"> <ImageView android:id="@+id/product_variation_image" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" android:layout_gravity="center"/>
Here is the Adaper:
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder> { private String[] mDataset; public static class ViewHolder extends RecyclerView.ViewHolder { public ImageView mImageView; public ViewHolder(View v) { super(v); mImageView = (ImageView) v.findViewById(R.id.product_variation_image); } } public TestAdapter(String[] myDataset) { mDataset = myDataset; } @Override public TestAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
The upload method retrieves an image from a URL or retrieves it from memory if it was cached. This works great in all other layouts, for example. ListView or GridView. Here is the code that I use to configure it in the snippet:
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); mRecyclerView.setLayoutManager(layoutManager);
This is the onCreateView method. When I get the URLs, I populate them and install the adapter using:
myDataset[i] = imageURL; // for each image mAdapter = new TestAdapter(myDataset); mRecyclerView.setAdapter(mAdapter);
An interesting thing is the line in the onBindViewHolder method in the adapter, where I record the position. I found that the cells where the image is not shown are that this method is not being called. It looks like it is skipping this cell for some reason. Even a stranger, if I hold the cell and continue to scroll from left to right, if the cell leaves the screen and then returns, its image, like again, is not called onBindViewHolder method.
android android-recyclerview recycler-adapter
Clive jefferies
source share