GridLayout and LinearLayout both in one RecyclerView - android

any idea how to use Two different LayoutManager in one View? I have four cards in grid on top of the screen and bottom, and I want LinearLayout but RecyclerView showing one View at a time
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(mLayoutManager);
RecyclerView.LayoutManager lmLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(lmLayoutManager);
recyclerView.addItemDecoration(new GridSpacingDashboard(2, dpToPx(2), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
Is there any way I can use both Views in a single RecyclerView Or on Index O I have GridLayout and on 1 showing LinearLayout , How can achieve if there is any possibilities
Update (Card Adapter)
public class CardAdapter extends RecyclerView.Adapter<adapter.CardAdapter.CardViewHolder> {
public static final int topGridCard = 0;
public static final int bottomCards=1;
private Context mContext;
private List<CardModel> cardList;
public CardAdapter(Context mContext, List<CardModel> cardList) {
this.mContext = mContext;
this.cardList = cardList;
}
#Override
public.adapter.CardAdapter.CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
if (viewType == topGridCard) {
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_sec, parent, false);
return new adapter.CardAdapter.TopGridCardView(v);
}
else if (viewType == bottomCards) {
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view, parent, false);
return new adapter.CardAdapter.BottomCardView(v);
}
return null;
}
#Override
public void onBindViewHolder(final adapter.CardAdapter.CardViewHolder holder, final int position) {
CardModel cardModel = cardList.get(position);
if (holder.getItemViewType()==topGridCard){ adapter.CardAdapter.TopGridCardView vholder = (adapter.CardAdapter.TopGridCardView) holder;
vholder.secTitleStats.setText(cardModel.getNumStats());
vholder.secNumStats.setText(cardModel.getCardTitles());
vholder.secCardStats.setText(cardModel.getSecCartNumStats());
Glide.with(mContext).load(cardModel.getThumbnail()).into(vholder.secthumbnail);
}
else if(holder.getItemViewType()==bottomCards){
adapter.CardAdapter.BottomCardView vholder = (adapter.CardAdapter.BottomCardView) holder;
vholder.numStats.setText(cardModel.getNumStats());
vholder.image_title.setText(cardModel.getCardTitles());
Glide.with(mContext).load(cardModel.getThumbnail()).into(vholder.thumbnail);
}
}
#Override
public int getItemCount(){
return cardList.size();
}
public static class CardViewHolder extends RecyclerView.ViewHolder {
public CardViewHolder(View v) {
super(v);
}
}
public class TopGridCardView extends adapter.CardAdapter.CardViewHolder {
public TextView secTitleStats, secNumStats,secCardStats;
public ImageView secthumbnail;
public TopGridCardView(View view){
super(view);
secTitleStats = (TextView) view.findViewById(R.id.secTitleNumStats);
secNumStats = (TextView) view.findViewById(R.id.secCardNumStats);
secCardStats = (TextView) view.findViewById(R.id.sectitle);
secthumbnail = (ImageView) view.findViewById(R.id.seCthumbnail);
}
}
public class BottomCardView extends adapter.CardAdapter.CardViewHolder {
public TextView numStats,image_title;
public ImageView thumbnail;
public BottomCardView(View view) {
super(view);
numStats=(TextView)view.findViewById(R.id.numStatics);
image_title=(TextView) view.findViewById(R.id.title_overlay);
thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
}
}
#Override
public int getItemViewType(int position) {
if (isPositionHeader(position))
return topGridCard;
return bottomCards;
}
private boolean isPositionHeader(int position) {
return position == 0;
}
}
I have attached my Adapter just for more understanding , what the desired result should be
|Grid Card 1 | Grid Card 2 |
|Grid Card 3 | Grid Card 4 |
| LinearLayout Card 1 |
| LinearLayout Card 2 |
any possibilities

you can simply use gridLayout with different viewType to achieve this.
mLayoutManager = new GridLayoutManager(getActivity(), 2);
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int position) {
switch (adapter.getItemViewType(position)) {
case MenuAdapter.ITEM:
return 1;
case MenuAdapter.FULLSIZE:
return 2;
default:
return 1;
}
}
});
recyclerView.setLayoutManager(mLayoutManager);
Where ITEM is a grid(2x column) then return 1, otherwise FULLSIZE return 2(takes 2x grid column).
hope it helped.

Related

NestedRecyclerView's onBindViewHolder gettting called multiple times and not getting recycled

I am using nestedRecyclerView with Vertical parent recyclerview and horizontal child views. I have list of 10 items and each item contains a list for child recyclerview.
When I run the app, parent adapter's onbindviewholder getting called 10 items which is the total size of the parent list. I want to know why it is happening. It should call 4 or 5 onbindviewholder depending on the screen size but it is calling for all items which is unintended.
Parent Recyclerview initialization
homeRecyclerViewAdapter = new HomeRecyclerViewAdapter(getContext(), MR, (AppCompatActivity) getActivity());
homeRecyclerViewAdapter.setDataList(homeArrayList);
LinearLayoutManager mLinearLayoutManager3 = new LinearLayoutManager(getContext());
mLinearLayoutManager3.setOrientation(CustomLinearLayoutManager.VERTICAL);
homeRecyclerView.setLayoutManager(mLinearLayoutManager3);
homeRecyclerView.setAdapter(homeRecyclerViewAdapter);
Parent Adapter -
public class HomeRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
List dataList;
private Context mContext;
Typeface MR;
static String PACKAGE_NAME;
Utils utils;
CustomClickEvents customClickEvents;
AppCompatActivity appCompatActivity;
public HomeRecyclerViewAdapter(Context mContext, Typeface MR, int type, CustomClickEvents customClickEvents) {
this.mContext = mContext;
this.MR = MR;
this.customClickEvents = customClickEvents;
utils = new Utils();
}
RecyclerView.RecycledViewPool pool;
public HomeRecyclerViewAdapter(Context mContext, Typeface MR, AppCompatActivity appCompatActivity) {
pool = new RecyclerView.RecycledViewPool();
this.mContext = mContext;
this.MR = MR;
this.appCompatActivity = appCompatActivity;
utils = new Utils();
}
public void setDataList(List upiAppsList) {
this.dataList = upiAppsList;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
PACKAGE_NAME = mContext.getPackageName();
if (viewType == Constants.HomePageViewTypes.TYPE_0) {
view = LayoutInflater.from(mContext).inflate(R.layout.recyclerview_layout, null);
return new RecyclerViewHolder(view);
} else if (viewType == Constants.HomePageViewTypes.TYPE_1) {
view = LayoutInflater.from(mContext).inflate(R.layout.recyclerview_layout, null);
return new RecyclerViewHolder(view);
} else if (viewType == Constants.HomePageViewTypes.TYPE_2) {
view = LayoutInflater.from(mContext).inflate(R.layout.recyclerview_layout, null);
return new RecyclerViewHolder(view);
}
return null;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder itemViewHolder, int position) {
Log.d("recyclerview", "HomeRecyclerViewHolder onbindviewholder + pos" + position);
Log.d("recyclerview", "HomeRecyclerViewHolder dataList.get(position):" + dataList.get(position));
RecyclerViewHolder holder = (RecyclerViewHolder) itemViewHolder;
holder.commonRecyclerViewAdapter.setDataList(((HomePageViewModel) dataList.get(position)).getDataList());
}
public void setbackgroundcolortoitem(int position, RecyclerViewHolder viewHolder, boolean is_checked) {
int modulus_position = position % 4;
Log.d("tag", "position ---- > " + modulus_position);
}
class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
RecyclerView mRecyclerView;
CommonRecyclerViewAdapter commonRecyclerViewAdapter;
public RecyclerViewHolder(View itemView) {
super(itemView);
mRecyclerView = itemView.findViewById(R.id.mrecyclerview);
mRecyclerView.setPadding((int) (8 * utils.getScreenDensity(mContext)), 0, 0, (int) (8 * utils.getScreenDensity(mContext)));
// mRecyclerView.requestDisallowInterceptTouchEvent(false);
commonRecyclerViewAdapter = new CommonRecyclerViewAdapter(mContext, MR, 6, appCompatActivity);
LinearLayoutManager mLinearLayoutManager3 = new LinearLayoutManager(appCompatActivity, LinearLayoutManager.HORIZONTAL, false) {
#Override
public boolean checkLayoutParams(RecyclerView.LayoutParams lp) {
// force height of viewHolder here, this will override layout_height from xml
Log.d("upisdk", "HomeRecyclerView getwidth:" + getWidth());
lp.width = (int) (getWidth() / 1.6);
return true;
}
};
mLinearLayoutManager3.setOrientation(CustomLinearLayoutManager.HORIZONTAL);
mRecyclerView.setLayoutManager(mLinearLayoutManager3);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setAdapter(commonRecyclerViewAdapter);
}
#Override
public void onClick(View v) {
int position = getLayoutPosition();
switch (v.getId()) {
case R.id.mainlayout:
break;
}
}
}
#Override
public int getItemCount() {
return dataList.size();
}
#Override
public int getItemViewType(int position) {
if (dataList.get(position) instanceof HomePageViewModel) {
if (((HomePageViewModel) dataList.get(position)).getLayoutType() == Constants.HomePageViewTypes.TYPE_0) {
return Constants.HomePageViewTypes.TYPE_0;
}
}
return -1;
}
}
I want parent onbindviewholder to get called for the items displaying currently on screen which is the supposed behaviour of RecyclerView.
Is the parent View of the Parent RecycleView a ScrollView or NestedScrollView, bro? If true, please remove it

Vertical Recyclerview inside horizontal Recyclerview using old views and populating nested recyclerview

I am developing an app in which we need vertical Recyclerview inside horizontal Recyclerview. And the vertical Recyclerview have multiple view types. So the problem is, when i scroll down few items and go back up again, only the nested recyclerview is populated with wrong data. Otherwise every other data is correct.
Like in this picture you can see the correct data where recyclerview is gone
In this picture you can see the recyclerview is populated with some other object data and showing image when i scroll back up
Now i have tried everything setHasStableIds(true) , using same viewpool worsen the situation,getItemId();
But nothing seems to work.
OUTER RECYCLERVIEW ADAPTER
private ArrayList<BasePostData> basePostDataArrayList;
private Context mContext;
private RecyclerView.RecycledViewPool viewPool;
public PostFragmentListAdapter(ArrayList<BasePostData> basePostDataArrayList, Context mContext) {
this.basePostDataArrayList = basePostDataArrayList;
this.mContext = mContext;
setHasStableIds(true);
}
public static class MyviewHolder extends RecyclerView.ViewHolder{
ImageView profileImage,more;
TextView name,user_category,post_category,follow,like,comment,share,description,post_type,highlight;
RecyclerView recyclerView;
HorizontalPostRecyclerview horizontalAdapter;
public MyviewHolder(#NonNull View itemView) {
super(itemView);
Context context = itemView.getContext();
this.comment = itemView.findViewById(R.id.post_comment_textview);
this.name = itemView.findViewById(R.id.post_user_name);
this.user_category = itemView.findViewById(R.id.post_category);
this.post_category = itemView.findViewById(R.id.posts_multiple_categories);
this.follow = itemView.findViewById(R.id.post_follow_button);
this.like = itemView.findViewById(R.id.post_like_textview);
this.share = itemView.findViewById(R.id.post_whatsapp_tv);
this.profileImage = itemView.findViewById(R.id.post_profile_pic);
this.more = itemView.findViewById(R.id.post_more_tv);
this.description = itemView.findViewById(R.id.post_description_textview);
this.recyclerView =itemView.findViewById(R.id.post_content_recyclerview);
this.post_type= itemView.findViewById(R.id.post_type);
this.highlight= itemView.findViewById(R.id.post_highlighted);
// recyclerView = (RecyclerView) itemView.findViewById(R.id.horizontal_list);
}
}
#NonNull
#Override
public MyviewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.post_layout,viewGroup,false);
MyviewHolder myviewHolder = new MyviewHolder(view);
return myviewHolder;
}
#Override
public void onBindViewHolder(#NonNull MyviewHolder viewHolder, int position)
{
StringBuilder post_cat= new StringBuilder();
BasePostData basePostData = basePostDataArrayList.get(position);
viewHolder.description.setText(basePostData.getDiscription());
viewHolder.name.setText(basePostData.getUser());
viewHolder.post_type.setText(basePostData.getPost_type());
viewHolder.itemView.setTag(position);
if (basePostData.getUser_category()!=null){
viewHolder.user_category.setText("("+basePostData.getUser_parent_category()+") "+basePostData.getUser_category());
}else {
viewHolder.user_category.setText(basePostData.getUser_parent_category());
}
viewHolder.like.setText(String.valueOf(basePostData.getLike())+" Likes");
viewHolder.comment.setText(String.valueOf(basePostData.getTotal_comments())+" Comments");
for (int i = 0;i<basePostData.getPost_tag().size();i++)
{
post_cat.append(basePostData.getPost_tag().get(i).getTag()).append(" | ");
}
viewHolder.post_category.setText(post_cat.toString());
Picasso.get()
.load(basePostData.getUser_profile_pic())
.placeholder(R.drawable.profile_ic)
.error(R.drawable.profile_ic)
.into(viewHolder.profileImage);
//--------------------------------highlight answer--------------
if (basePostData.getHighlight_comment()!=null)
{
viewHolder.highlight.setVisibility(View.VISIBLE);
viewHolder.highlight.setText(basePostData.getHighlight_comment().getComment());
}
//---------------------------recyclerview for files-------------------------------
if (basePostData.getFile_type()!=null) {
viewHolder.recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
viewHolder.horizontalAdapter = new HorizontalPostRecyclerview();
viewHolder. recyclerView.setAdapter(viewHolder.horizontalAdapter);
viewHolder.horizontalAdapter.setData(basePostData.getPost_file()); // List of Strings
viewHolder.horizontalAdapter.setRowIndex(position);
viewHolder.recyclerView.setVisibility(View.VISIBLE);
/*
Log.e("item ",basePostData.getFile_type());
viewHolder. recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
*//* switch (basePostData.getFile_type()){
case "Image":
viewHolder. horizontalAdapter = new HorizontalPostRecyclerview(basePostData.getPost_file() ,mContext,ConstantsOSH.TYPE_IMAGE);
viewHolder. recyclerView.setAdapter(viewHolder.horizontalAdapter);
break;
case "Video":
viewHolder. horizontalAdapter = new HorizontalPostRecyclerview(basePostData.getPost_file() ,mContext,ConstantsOSH.TYPE_VIDEO);
viewHolder. recyclerView.setAdapter(viewHolder.horizontalAdapter);
break;
case "Document":
viewHolder. horizontalAdapter = new HorizontalPostRecyclerview(basePostData.getPost_file() ,mContext,ConstantsOSH.TYPE_DOC);
viewHolder. recyclerView.setAdapter(viewHolder.horizontalAdapter);
viewHolder.horizontalAdapter.setRowIndex(position);
break;
}*//*
if (basePostData.getFile_type().equalsIgnoreCase("image")){
viewHolder. horizontalAdapter = new HorizontalPostRecyclerview(basePostData.getPost_file() ,mContext,ConstantsOSH.TYPE_IMAGE);
}else if (basePostData.getFile_type().equalsIgnoreCase("video")){
viewHolder. horizontalAdapter = new HorizontalPostRecyclerview(basePostData.getPost_file() ,mContext,ConstantsOSH.TYPE_VIDEO);
}else if (basePostData.getFile_type().equalsIgnoreCase("document"))
viewHolder. horizontalAdapter = new HorizontalPostRecyclerview(basePostData.getPost_file() ,mContext,ConstantsOSH.TYPE_DOC);
viewHolder. recyclerView.setAdapter(viewHolder.horizontalAdapter);
viewHolder.horizontalAdapter.setRowIndex(position);
*/ }
}
#Override
public int getItemCount() {
return basePostDataArrayList.size();
}
public void add(ArrayList<BasePostData> basePostDatalist) {
basePostDataArrayList.addAll(basePostDatalist);
notifyDataSetChanged();
}
public void clear() {
basePostDataArrayList.clear();
notifyDataSetChanged();
}
#Override
public long getItemId(int position) {
BasePostData product = basePostDataArrayList.get(position);
return product.hashCode();
}
}
VERTICAL NESTED RECYCLERVIEW ADAPTER
public class HorizontalPostRecyclerview extends
RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<PostFileModel> dataset;
private Context mContext;
private int mRowIndex = -1;
private int viewType;
public HorizontalPostRecyclerview() {
setHasStableIds(true);
}
public HorizontalPostRecyclerview(ArrayList<PostFileModel> dataset, Context mContext) {
this.dataset = dataset;
this.mContext = mContext;
}
public HorizontalPostRecyclerview(ArrayList<PostFileModel> dataset, Context mContext,int viewType) {
this.dataset = dataset;
this.mContext = mContext;
this.viewType = viewType;
}
public void setRowIndex(int index) {
mRowIndex = index;
}
public void setData(ArrayList<PostFileModel> data) {
if (dataset != data) {
dataset = data;
notifyDataSetChanged();
}
}
private class ImageViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView,play_btn;
private TextView name;
public ImageViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.horizontal_image_layout_imageview);
// name = itemView.findViewById(R.id.post_image_gallery_name_text);
// play_btn = itemView.findViewById(R.id.post_image_gallery_play_btn);
}
}
private class VideoViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView,play_btn;
private TextView name;
public VideoViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.horizontal_video_layout_imageview);
// name = itemView.findViewById(R.id.post_image_gallery_name_text);
play_btn = itemView.findViewById(R.id.horizontal_video_layout_play_btn);
}
}
private class DocViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView,play_btn;
private TextView name;
public DocViewHolder(View itemView) {
super(itemView);
//imageView = itemView.findViewById(R.id.post_image_gallery_imageview);
name = itemView.findViewById(R.id.horizontal_doc_layout_textview);
play_btn = itemView.findViewById(R.id.horizontal_doc_layout_playbtn);
}
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View itemView;
// ImageViewHolder holder = new ImageViewHolder(itemView);
switch (i)
{
case ConstantsOSH.TYPE_IMAGE:
itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.horizontal_post_image_layout,viewGroup, false);
return new ImageViewHolder(itemView);
// break;
case ConstantsOSH.TYPE_DOC:
itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.horizontal_post_doc_layout,viewGroup, false);
return new DocViewHolder(itemView);
// break;
case ConstantsOSH.TYPE_VIDEO:
itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.horizontal_post_video_layout,viewGroup, false);
return new VideoViewHolder(itemView);
// break;
default:
itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.horizontal_post_image_layout,viewGroup, false);
return new ImageViewHolder(itemView);
}
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder viewHolder, int i) {
// ImageViewHolder holder = (ImageViewHolder) viewHolder;
switch (dataset.get(i).getFile_type()){
case "Image":
ImageViewHolder holder = (ImageViewHolder) viewHolder;
Picasso.get().load(dataset.get(i).getFile()).error(R.drawable.camera_ic).placeholder(R.drawable.flag_india).into(holder.imageView);
holder.itemView.setTag(i);
/* Glide.with(holder.imageView.getContext()) // Bind it with the context of the actual view used
.load(dataset.get(i).getFile()) // Load the image
.thumbnail(0.2f) // make use of the thumbnail which can display a down-sized version of the image
.into(holder.imageView);
*/
break;
case "document":
DocViewHolder holder2 = (DocViewHolder) viewHolder;
holder2.play_btn.setImageResource(R.drawable.doc_ic_new);
holder2.play_btn.setVisibility(View.VISIBLE);
String[] split = dataset.get(i).getFile().split("post_file/");
holder2.name.setText(split[1]);
holder2.name.setVisibility(View.VISIBLE);
holder2.itemView.setTag(i);
break;
case "video":
VideoViewHolder holder3 = (VideoViewHolder) viewHolder;
long interval = 2000;
RequestOptions options = new RequestOptions().frame(interval).placeholder(R.drawable.camera_ic);
Glide.with(holder3.imageView.getContext()).asBitmap()
.load(dataset.get(i).getFile())
.apply(options)
.into(holder3.imageView);
holder3.play_btn.setVisibility(View.VISIBLE);
holder3.itemView.setTag(i);
break;
}
}
#Override
public int getItemCount() {
return dataset.size();
}
#Override
public long getItemId(int position) {
PostFileModel postFileModel = dataset.get(position);
return postFileModel.hashCode();
}
#Override
public int getItemViewType(int position) {
switch (dataset.get(position).getFile_type()) {
case "Image":
return ConstantsOSH.TYPE_IMAGE;
case "video":
return ConstantsOSH.TYPE_VIDEO;
case "document":
return TYPE_DOC;
default:
return -1;
}
}
}

RecyclerView View Items

I want to show 2 columns in a RecyclerView, but they show in 1 column like this photo:
How can I show my view in 2 columns?
I try it in my code with 2 columns:
rcv_pro.setLayoutManager(new GridLayoutManager(this, 2, LinearLayoutManager.VERTICAL, false));
but they don't show in 2 columns.
i find a soulotion for show in 2 culmn , i can fix it whit clear this code :
" menuList_info_models.clear(); "
but i have a new probem now , when i clear that cod , my items in recyclerview is mess , any time i scrole in recyclerview , my items change
public class MultiItem_Music_Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
ArrayList<Object> objects = new ArrayList<Object>();
Context context;
private final static int MUSIC_CAT = 1;
private final static int MUSIC_ADA = 2;
private ArrayList<MenuList_Info_Model> menuList_info_models;
private ArrayList<Cat_Model> cat_models;
private Music_Adapter adapter;
private List<MenuList_Info_Model> personList = new ArrayList<>();
private RecyclerView recyclerView;
private ListAdapter mAdapter;
private ArrayList<MenuList_Info_Model> music_data;
public GridLayoutManager manager ;
public MultiItem_Music_Adapter(ArrayList<Object> objects, Context context) {
Log.e("MyLog","farzad");
this.objects = objects;
this.context = context;
if (menuList_info_models == null)
menuList_info_models = new ArrayList<MenuList_Info_Model>();
if (cat_models == null) cat_models = new ArrayList<Cat_Model>();
adapter = new Music_Adapter(menuList_info_models, this.context);
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder ;
LayoutInflater inflater = LayoutInflater.from(context);
View view;
switch (viewType) {
case MUSIC_CAT:
view = inflater.inflate(R.layout.music_cat_item, parent, false);
viewHolder = new Music_cat_ViewHolder(view);
break;
case MUSIC_ADA:
view= inflater.inflate(R.layout.music_item_multi, parent, false);
viewHolder = new Multi_item_ViewHolder(view);
break;
default:
view = inflater.inflate(R.layout.music_item_multi, parent, false);
viewHolder = new Multi_item_ViewHolder(view);
break;
}
return viewHolder;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Log.e("hamid_ali", "---------------------");
switch (holder.getItemViewType()) {
case MUSIC_CAT:
Cat_Config((Music_cat_ViewHolder) holder,position);
break;
case MUSIC_ADA:
item_Config((Multi_item_ViewHolder) holder,position);
break;
}
}
private void item_Config(Multi_item_ViewHolder item_viewHolder,int pos) {
MenuList_Info_Model menuList_info_model = (MenuList_Info_Model) objects.get(pos);
menuList_info_models.clear();
menuList_info_models.add(menuList_info_model);
Log.e("hamid_st",menuList_info_models.size() +"");
manager = new GridLayoutManager(this.context, 2, LinearLayoutManager.VERTICAL, false);
item_viewHolder.rcv_multi.setLayoutManager(manager);
item_viewHolder.rcv_multi.setAdapter(adapter);
}
private void Cat_Config(Music_cat_ViewHolder musicCatViewHolder,int pos) {
Cat_Model cat_model = (Cat_Model) objects.get(pos);
musicCatViewHolder.txt_cat_name.setText(cat_model.getMenu_name());
}
#Override
public int getItemCount() {
return objects.size();
}
#Override
public int getItemViewType(int position) {
if (objects.get(position) instanceof Cat_Model) {
return MUSIC_CAT;
} else {
return MUSIC_ADA;
}
}
}
you can use GridLayoutManager
set GridLayoutManager to your RecyclerView uisng below code
GridLayoutManager gridLayoutManager = new GridLayoutManager(MainActivity.this, 2);
recyclerView.setLayoutManager(gridLayoutManager);
here is link how to use GridLayoutManager with RecyclerView
RvAdapter MultiItem_Music_Adapter ;
RecyclerView rcv_pro;
MultiItem_Music_Adapter = new MultiItem_Music_Adapter (getActivity());
rcv_pro.setLayoutManager(new GridLayoutManager(getActivity(), 2));
rcv_pro.setAdapter(MultiItem_Music_Adapter );
You can try this for 2 columns :-
mRecycler.setLayoutManager(new GridLayoutManager(YourActivity.this, 2));
mRecycler.setLayoutManager(gridLayoutManager);
It is mendatory to setLayoutMangaer with your recyclerview otherwise it will not work.
You can get the two columns of an items in RecyclerView via StaggeredGridLayoutManager too.
From documentation, you can use following method:
StaggeredGridLayoutManager(int spanCount, int orientation)
StaggeredGridLayoutManager yourStaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
yourRecyclerView.setLayoutManager(staggeredGridLayoutManager);
Or directly:
yourRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
i must be create two ViewHolder see more code
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
class ViewHolder0 extends RecyclerView.ViewHolder {
...
public ViewHolder0(View itemView){
...
}
}
class ViewHolder2 extends RecyclerView.ViewHolder {
...
public ViewHolder2(View itemView){
...
}
#Override
public int getItemViewType(int position) {
// Just as an example, return 0 or 2 depending on position
// Note that unlike in ListView adapters, types don't have to be contiguous
return position % 2 * 2;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 0: return new ViewHolder0(...);
case 2: return new ViewHolder2(...);
...
}
}
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
switch (holder.getItemViewType()) {
case 0:
ViewHolder0 viewHolder0 = (ViewHolder0)holder;
...
break;
case 2:
ViewHolder2 viewHolder2 = (ViewHolder2)holder;
...
break;
}
}
}

Android, Any idea to show nested list with random width and height?

I have a nested list like A{a1, a2, a3...aN}, B{B1, BM}, .... O{O1, o2, ... OP} this. The list has a random number of members. Each member is also list with random of members. This will be shown like below image. As you see, outer cell's width and height is not fixed. I guess maybe it can be possible if I use nested recyclerview. It is little bit like StaggeredGridLayoutManager. But as I understood, StaggeredGridLayoutManager should have one of width or height should be fixed. Any suggestion how to implement this kind of UI design?
This is what I want to display
This is what displayes when I use StaggeredGridLayoutManager
This is outer List Adapter
/**********
class GroupAdapter extends RecyclerView.Adapter{
#Override
public GroupHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_group, null);
GroupHolder ih = new GroupHolder(v);
return ih;
}
#Override
public void onBindViewHolder(GroupHolder holder, int position) {
if(mGroups!=null && mGroups.size()>position) {
GroupObject group = mGroups.get(position);
ArrayList<CandidateObject> cans = group.getCandidates();
CandidateAdapter adapter = new CandidateAdapter(cans);
CustomLinearLayoutManager mLinearManager = new CustomLinearLayoutManager(MainActivity.this);
mLinearManager.setAutoMeasureEnabled(true);
holder.rvCandis.setLayoutManager(mLinearManager);
holder.tvTitle.setText(group.getName());
holder.rvCandis.setAdapter(adapter);
}
}
#Override
public int getItemCount() {
return mGroups.size();
}
}
private static class GroupHolder extends RecyclerView.ViewHolder {
private TextView tvTitle;
private RecyclerView rvCandis;
public int index;
private GroupHolder(View v) {
super(v);
this.tvTitle = (TextView) v.findViewById(R.id.tv_partyname);
this.rvCandis = (RecyclerView) v.findViewById(R.id.rvCandi);
}
}
And this is Inner list adapter
class CandidateAdapter extends RecyclerView.Adapter{
ArrayList candis;
CandidateAdapter(ArrayList<CandidateObject> candis){
this.candis = candis;
}
#Override
public CandidateHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_candidate, null);
CandidateHolder ih = new CandidateHolder(v);
return ih;
}
#Override
public void onBindViewHolder(CandidateHolder holder, int position) {
if(candis!=null && candis.size()>position) {
CandidateObject c = candis.get(position);
holder.tvName.setText(c.getName());
}
}
#Override
public int getItemCount() {
return candis.size();
}
}
private static class CandidateHolder extends RecyclerView.ViewHolder {
private ImageView ivThumb;
private TextView tvName;
public int index;
private CandidateHolder(View v) {
super(v);
this.tvName = (TextView) v.findViewById(R.id.tv_name);
this.ivThumb = (ImageView) v.findViewById(R.id.ivNaver);
}
}
StaggeredGridLayoutManager is made like this.
setContentView(R.layout.activity_main);
StaggeredGridLayoutManager mLayManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
mRvVote = (RecyclerView) findViewById(R.id.rvVote);
mRvVote.setLayoutManager(mLayManager);

Unable to add second child in recyclerview

Unable to add second child in Recyclerview I am passing two different arrays to RecyclerAdapter to display two child layout with different data and views.Is there any solution to add different child layout using same header layout.I added horizontal Recyclerview in vertical Recyclerview and I want to display details like I attached the image
private void setupRecyclerView(RecyclerView recyclerView) {
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
RecyclerAdapter recyclerAdapter = new RecyclerAdapter();
int[] images = new int[]{
R.drawable.finance,
R.drawable.business,
R.drawable.financejob,
R.drawable.ecomchallenges
};
ArrayList<ChildView> childViews = new ArrayList<>();
childViews.add(new ChildView(images[0], "The \"Best\" Startup Pitch Deck - How To Raise Venture Capital", "$100"));
childViews.add(new ChildView(images[1], "An Entire MBA in 1 Course:Award Winning Business School Prof", "$100"));
childViews.add(new ChildView(images[2], "What Finance Job is for You? Explanation of 14 Finance Roles", "$100"));
childViews.add(new ChildView(images[3], "Learn To Build Beautiful HTML5 And CSS3 Websites In 1 Month", "$100"));
int[] courseImage = new int[] {
R.drawable.php,
R.drawable.development,
R.drawable.web,
R.drawable.java
};
ArrayList<CourseByType> courseByTypes = new ArrayList<>();
courseByTypes.add(new CourseByType("Technology", courseImage[0]));
courseByTypes.add(new CourseByType("Business", courseImage[1]));
courseByTypes.add(new CourseByType("Photography", courseImage[2]));
courseByTypes.add(new CourseByType("Development", courseImage[3]));
Log.d("","Above adapter");
recyclerAdapter.addItem(new GroupView("Business", childViews));
Log.d("","Below Child");
recyclerAdapter.addCourseByType(new CourseByHeader("Technology", courseByTypes));
Log.d("","Below Course");
recyclerView.setAdapter(recyclerAdapter);
}
This is the main fragment where I set the values to two different
arraylist ArrayList<ChildView> childViews = new ArrayList<>()
and
ArrayList<CourseByType> courseByTypes = new ArrayList<>()
Values of child views are passing properly but CourseByType values are not passing.This is the adapter class for this fragment class.
RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
ArrayList<PassValues> containerArrayList;
ArrayList<GroupView> groupViews;
ArrayList<CourseByHeader>courseByHeaders;
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
View view = LayoutInflater.from(context).inflate(R.layout.group_title, parent, false);
return new ViewHolder(view);
}
public RecyclerAdapter(){
containerArrayList = new ArrayList<>();
groupViews = new ArrayList<>();
courseByHeaders = new ArrayList<>();
}
public void addContainer(PassValues container){
containerArrayList.add(container);
}
public void addItem(GroupView groupView){
Log.d("","Inside Group method");
groupViews.add(groupView);
}
public void addCourseByType(CourseByHeader courseByHeader){
Log.d("","Inside Course method");
courseByHeaders.add(courseByHeader);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Log.d("", "Pass Values out of IF" + position);
ChildViewAdapter childViewAdapter = new ChildViewAdapter();
if(position == 0){
GroupView groupView = groupViews.get(position);
holder.title.setText(groupView.getTitle());
Log.d("", "Passing Values" + groupView.getTitle());
holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder.recyclerView.getContext(), LinearLayoutManager.HORIZONTAL, false));
holder.recyclerView.setOnFlingListener(null);
childViewAdapter.addChild(groupView.getChildViewList());
holder.recyclerView.setAdapter(childViewAdapter);
}
if (position == 1) {
CourseByHeader courseByHeader = courseByHeaders.get(position);
holder.title.setText(courseByHeader.getTitle());
Log.d("", "Passing Values" + courseByHeader.getTitle());
holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder.recyclerView.getContext(), LinearLayoutManager.HORIZONTAL, false));
holder.recyclerView.setOnFlingListener(null);
childViewAdapter.addCourse(courseByHeader.getCourseByTypes());
holder.recyclerView.setAdapter(childViewAdapter);
}
}
#Override
public int getItemCount() {
if(getItemViewType(0) == TYPE_HEADER)
return groupViews.size() ;
if (getItemViewType(1) == TYPE_ITEM)
return courseByHeaders.size();
else return -1;
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView title;
RecyclerView recyclerView;
public ViewHolder(View itemView) {
super(itemView);
title = (TextView)itemView.findViewById(R.id.course_title);
recyclerView = (RecyclerView)itemView.findViewById(R.id.group_recycler);
}
}
}
This RecyclerAdapter contains one RecyclerView in that first row has one image and 3 textviews and 2nd row has 1 ImageView and 1 TextView. At position first,one image and 3 textviews are shown but it's not going on 2nd view
This is the view I getting after run on emulator.
This are two child for RecyclerViews
ChildView.java
public class ChildView {
int image;
String course, price;
public ChildView(int image, String course, String price) {
this.image = image;
this.course = course;
this.price = price;
}
public int getImage() {
return image;
}
public String getCourse() {
return course;
}
public String getPrice() {
return price;
}
}
CourseByType.java
public class CourseByType {
String courseName;
int courseImage;
public CourseByType(String courseName, int courseImage) {
this.courseName = courseName;
this.courseImage = courseImage;
}
public String getCourseName() {
return courseName;
}
public int getCourseImage() {
return courseImage;
}
}
CourseByHeader.java
public class CourseByHeader {
String title;
ArrayList<CourseByType> courseByTypes;
public CourseByHeader(String title, ArrayList<CourseByType> courseByTypes) {
this.title = title;
this.courseByTypes = courseByTypes;
}
public String getTitle() {
return title;
}
public ArrayList<CourseByType> getCourseByTypes() {
return courseByTypes;
}
}
GroupView.java
public class GroupView {
String title;
ArrayList<ChildView> childViewList;
String courseBy;
ArrayList<CourseByType> courseByTypes;
public GroupView(String title, ArrayList<ChildView> childViewList) {
this.title = title;
this.childViewList = childViewList;
}
public String getTitle() {
return title;
}
public ArrayList<ChildView> getChildViewList() {
return childViewList;
}
}
Groupview and CouseByType class have title and child list for recycleradapter
ChildViewAdapter.java
public class ChildViewAdapter extends RecyclerView.Adapter {
ArrayList<ChildView> childViewList;
ArrayList<CourseByType> courseByTypes;
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
public class ViewHolder extends RecyclerView.ViewHolder{
public ViewHolder(View itemView) {
super(itemView);
}
}
public class GroupHolder extends ViewHolder {
public ImageView iamView;
public TextView course, price;
public GroupHolder(View itemView) {
super(itemView);
iamView = (ImageView) itemView.findViewById(R.id.course_image);
course = (TextView) itemView.findViewById(R.id.course_by);
price = (TextView) itemView.findViewById(R.id.price);
}
}
public void addCourse(ArrayList<CourseByType> courseByType){
courseByTypes = courseByType;
}
public void addChild(ArrayList<ChildView> childView){
childViewList = childView;
}
public class Course extends ViewHolder {
public ImageView courseTypeImage;
public TextView courseType;
public Course(View itemView) {
super(itemView);
courseTypeImage = (ImageView)itemView.findViewById(R.id.course_image);
courseType = (TextView)itemView.findViewById(R.id.course_name_course);
}
}
public ChildViewAdapter() {
childViewList = new ArrayList<>();
courseByTypes = new ArrayList<>();
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
RecyclerView.ViewHolder vh = null;
View v;
if(viewType == TYPE_HEADER){
v = LayoutInflater.from(context).inflate(R.layout.recycler_item, parent, false);
return new GroupHolder(v);
}if(viewType == TYPE_ITEM){
v = LayoutInflater.from(context).inflate(R.layout.type_of_courses, parent, false);
return new Course(v);
}
return vh;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(holder instanceof GroupHolder){
Log.d("","instance of Group Holder");
ChildView childView = childViewList.get(position);
((GroupHolder)holder).iamView.setImageResource(childView.getImage());
((GroupHolder)holder).course.setText(childView.getCourse());
((GroupHolder)holder).price.setText(childView.getPrice());
return;
}
if(holder instanceof Course){
Log.d("","instance of Course ");
CourseByType courseByType = courseByTypes.get(position);
((Course)holder).courseTypeImage.setImageResource(courseByType.getCourseImage());
((Course)holder).courseType.setText(courseByType.getCourseName());
return;
}
}
#Override
public int getItemCount() {
int size;
if(childViewList.size()>0){
return size = childViewList.size();
}else return size = courseByTypes.size();
}
#Override
public int getItemViewType(int position) {
if(childViewList.size() != 0 && childViewList.size()>0){
return TYPE_HEADER;
}else return TYPE_ITEM;
}
}
This childview adapter has two view types first is one image and 3 text and second view type contain one image and one text.When I pass values from fragment only first view type get displayed and second view type not gets value from fragment.
To show multiple different views in a recyclerview, you have to override getItemViewType() in the recyclerview adapter.
//getItemViewType enables dynamic viewholder creation
#Override
public int getItemViewType(int position) {
//you will need to add a integer with variable name viewTypeCode
//for view1 set viewTypeCode = 100 and for view2 set viewTypeCode = 200
viewTypeCode = itemList.get(position).getViewTypeCode();
return viewTypeCode;
}
This is how the onCreateViewHolder will be different for multiple viewtypes. You will have to modify yours like this
#Override
public FeedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 100: return new FeedViewHolder(layoutInflater.inflate(R.layout.v1, parent, false),100);
case 200: return new FeedViewHolder(layoutInflater.inflate(R.layout.v2, parent, false),200);
}
return null;
}
OnBindViewHolder will be similarly modified
#Override
public void onBindViewHolder(final FeedViewHolder holder, int position) {
viewTypeCode = itemList.get(position).getViewTypeCode();
switch ( viewTypeCode) {
case 100:
//your code for v1
case 200:
//your code for v2
}
}
Similarly the ViewHolder class is modified
class FeedViewHolder extends RecyclerView.ViewHolder{
//declare variables here
public FeedViewHolder(View v, int viewType) {
super(v);
switch (viewType) {
//instead of itemView.findViewById you will have to use v.findViewById
case 100:
//your code for v1
case 200:
//your code for v2
}
}
For further reference refer to this SO answer
Don't pass two separate list.Make a custom class like this-
class MyClass {
int viewTypeCode;
CustomClass1 c1;
CustomClass2 c2;
//add the setter getter
}
In your activity while preparing the data.
List<MyClass> itemList = new ArrayList<>();
//put whatever logic you need to make the order of the list
//if CustomClass1 object is put then setViewTypeCode(100), setCustomClass2 = null
//if CustomClass2 object is put then setViewTypeCode(200), setCustomClass1 = null
After data is built, then send this to the adapter.

Categories

Resources