i am trying to switch setLayoutManager
between LinearLayoutManager
(List) to GridLayoutManager
(GridView) on toggle button it is working fine but when the layout changes for list to grid its giving me an exception in list view i am showing extra data than girdView and setting texts cant not find this Textviews in gridView single item file so its giving me an error can you please guide how can i handle two layouts and views in single Adapter
this is my Adapter Code
public class SavedAlbumAdapter2 extends RecyclerView.Adapter<SavedAlbumAdapter2.ItemViewHolder> {
private final GlideRequest<Drawable> request;
private ArrayList<AlbumLocalObj> itemModels;
private static final int LIST_ITEM = 0;
private static final int GRID_ITEM = 1;
private ClickListener mListener;
private LocalPreferences localPreferences;
public SavedAlbumAdapter2(Activity context, ArrayList<AlbumLocalObj> values, ClickListener clickListener) {
itemModels = values;
this.mListener = clickListener;
localPreferences = new LocalPreferences(context);
int reqWidth = ((HomeActivity) context).screenWidth / 2;
request = GlideApp.with(context)
.asDrawable()
.centerCrop()
.override(reqWidth, reqWidth)
.transition(withNoTransition());
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView;
if (viewType == LIST_ITEM) {
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_gallery2, parent, false);
} else {
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_gallery, parent, false);
}
return new ItemViewHolder(itemView, mListener);
}
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
AlbumLocalObj model = itemModels.get(position);
holder.img_label.setText(model.getName());
holder.tv_count.setText(String.valueOf(model.getCount()));
initializeViews(model, holder, position);
}
@Override
public int getItemViewType(int position) {
if (localPreferences.isGridViewEnabled()) {
return GRID_ITEM;
} else {
return LIST_ITEM;
}
}
private void initializeViews(AlbumLocalObj model, final ItemViewHolder holder, int position) {
if (model.getMediaFileObject() == null)
holder.gallery_img.setImageResource(R.drawable.ic_empty_fol);
else {
request.load(model.getMediaFileObject()).listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
Log.i("glide_action", "failure");
clearResource((MediaFileObject) model);
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
clearResource((MediaFileObject) model);
Log.i("glide_action", "success");
return false;
}
})
.into(holder.gallery_img);
}
}
public interface ClickListener {
void onItemClick(View view, int piecesAtPos);
void onItemLongClick(View view, int piecesAtPos);
}
private void clearResource(MediaFileObject item) {
try {
if (item.getInputStream() != null) {
Log.i("resource", "clear");
item.getInputStream().close();
item.setInputStream(null);
//Log.i("closing_str", pos + "");
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return itemModels.size();
}
public static class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
SquarImageView gallery_img;
TextView img_label;
TextView tv_count;
ImageButton info;
private ClickListener mListener;
ItemViewHolder(View itemView, ClickListener listener) {
super(itemView);
gallery_img = itemView.findViewById(R.id.img);
info = itemView.findViewById(R.id.info);
img_label = itemView.findViewById(R.id.label);
tv_count = itemView.findViewById(R.id.count);
this.mListener = listener;
info.setOnClickListener(this);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
@Override
public void onClick(View view) {
mListener.onItemClick(view, getAdapterPosition());
}
@Override
public boolean onLongClick(View view) {
mListener.onItemLongClick(view, getAdapterPosition());
return false;
}
}
}
this is my layout for single item of grid View
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_add_messages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="1dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<SquarImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:padding="0dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/img"
android:alpha="0.8"
android:background="@color/darkColor"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:padding="7dp">
<TextView
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:ellipsize="end"
android:maxLines="1"
android:singleLine="true"
android:text="akhskajhskjahkasjkhajskhjksasahjksahjkshajksahkjsahkjshjksjhakjhskjah"
android:textColor="@android:color/white"
android:textSize="16sp" />
<TextView
android:id="@+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="lahslahashkjjsahshjaksjhaksajhksahkjsahjksahkjsahjksahkjsahkjsahjksajhksahkjsakhjsahkjsahkjsahsklhask"
android:textColor="@android:color/white"
android:textSize="12sp" />
</LinearLayout>
<ImageButton
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="?android:attr/selectableItemBackground"
android:padding="7dp"
app:srcCompat="@drawable/info_white" />
</LinearLayout>
</RelativeLayout>
and this is for list item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<RelativeLayout
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginLeft="3dp"
android:layout_marginStart="3dp"
android:layout_marginTop="3dp"
android:background="@drawable/album_item_background" />
<RelativeLayout
android:layout_width="70dp"
android:layout_height="70dp"
android:background="@drawable/album_item_background">
<SquarImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="3dp"
android:scaleType="fitXY" />
</RelativeLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="kajhskahskhas" />
<TextView
android:id="@+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="kajhskahskhas" />
<TextView
android:id="@+id/3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
</LinearLayout>
<LinearLayout
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageButton
android:id="@+id/info"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="@drawable/info"
android:background="?attr/selectableItemBackgroundBorderless"
/>
</LinearLayout>
in my list view there are extra textview and button when i change list view to grid it can not find that views what should i do? do i have to create separate adapter?or how can i handle this in single adapter please help me