How to make RecyclerView to width="wrap_content" - android

I want to make RecyclerView like this one:
But in my case, child view doesn't set as width="wrap_content" or it RecyclerView doesn't set as width="wrap_content" and on "center"
Here is RecyclerView in activity_layout.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
Here is setting Adapter onCreate:
public void initRecyclerView(List<Invitation> invitationList) {
recyclerView.setHasFixedSize(true);
GridLayoutManager manager = new GridLayoutManager(this, 4);
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(new ItemGridAdapter(getApplicationContext(), invitationList));
}
here is my Adapter:
public class ItemGridAdapter extends RecyclerView.Adapter<ItemGridAdapter.ViewHolderItem> {
private final Context context;
private final List<Invitation> list;
private final DrawableHelper drawableHelper;
public ItemGridAdapter(Context context, List<Invitation> list) {
this.context=context;
this.list=list;
this.drawableHelper = new DrawableHelper();
}
#Override
public ItemGridAdapter.ViewHolderItem onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_grid, parent, false);
return new ItemGridAdapter.ViewHolderItem(context,view);
}
#Override
public void onBindViewHolder(final ItemGridAdapter.ViewHolderItem viewHolder, int position) {
Invitation invitation = list.get(position);
viewHolder.position=position;
Picasso.with(context)
.load(invitation.getCustomUser().getAvatar())
.transform(new CircleTransformation())
.placeholder(drawableHelper.getDrawableForName(invitation.getCustomUser().getFullName()))
.into(viewHolder.userIcon);
if (invitation.getYelpID()!=null&&invitation.getYelpID().length()>0){
viewHolder.votedIcon.setVisibility(View.VISIBLE);
}else{
viewHolder.votedIcon.setVisibility(View.GONE);
}
}
#Override
public int getItemCount() {
return list.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolderItem extends RecyclerView.ViewHolder {
public ImageView votedIcon;
public ImageView userIcon;
Context mContext;
int position;
public ViewHolderItem(Context mContext,View itemView) {
super(itemView);
this.mContext = mContext;
userIcon=(ImageView)itemView.findViewById(R.id.userIcon);
votedIcon = (ImageView)itemView.findViewById(R.id.votedIcon);
}
}
}
here is layout of item R.layout.item_grid for adapter :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center">
<ImageView
android:background="#drawable/white_circle"
android:padding="3dp"
android:id="#+id/userIcon"
android:layout_width="50dp"
android:layout_height="50dp"/>
<ImageView
android:id="#+id/votedIcon"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/icon_voted"/>
</RelativeLayout>

Android Support Library as of 23.2 supports this WRAP_CONTENT in RecyclerView by default.

Related

Why these random marks are appearing in every recyclerview and viewpager items?

I have 2 recycler views and a view pager in a fragment.
Everything else works fine but both recycler view items are showing a random mark on the upper left corner of every items. And same thing is happening for the view pager items.
This fragment is in a fragment activity and view pager is using pager adapter. And I am using Picasso library to load images on the items and also using network policy to cache the images in disk.
recycler view items appearing like this
view pager items appearing like this
Recycler View adapter
public class CourseListAdapter extends RecyclerView.Adapter<CourseListAdapter.CourseListViewHolder> {
public static final int HOME_PAGE = 1;
public static final int DISPLAY_COURSE = 2;
private Picasso mPicasso;
private List<DisplayCourse> courseList;
private static final String TAG = "CourseListAdapter";
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(int position, View view);
}
private String mSender;
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
public CourseListAdapter(List<DisplayCourse> courseList, String sender) {
this.courseList = courseList;
this.mSender = sender;
mPicasso = Picasso.get();
}
#NonNull
#Override
public CourseListViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType) {
case 1:
View view1 = inflater.inflate(R.layout.recom_course_home_layout, parent, false);
return new CourseListViewHolder(view1, mListener);
case 2:
View view2 = inflater.inflate(R.layout.display_course_layout, parent, false);
return new CourseListViewHolder(view2, mListener);
default:
View view3 = inflater.inflate(R.layout.section_video_item_layout, parent, false);
return new CourseListViewHolder(view3, mListener);
}
}
#Override
public void onBindViewHolder(#NonNull CourseListViewHolder holder, int position) {
holder.title.setText(courseList.get(position).getCourseTitle());
mPicasso.load(courseList.get(position).getThumbnailURL())
.networkPolicy(NetworkPolicy.OFFLINE)
.into(holder.thumbnail, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError(Exception e) {
mPicasso.load(courseList.get(position).getThumbnailURL())
.error(R.drawable.ofklogo)
.into(holder.thumbnail, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError(Exception e) {
}
});
}
});
}
#Override
public int getItemCount() {
return courseList.size();
}
public static class CourseListViewHolder extends RecyclerView.ViewHolder {
ImageView thumbnail;
TextView title;
public CourseListViewHolder(#NonNull View itemView, final OnItemClickListener listener) {
super(itemView);
title = itemView.findViewById(R.id.courseTitle);
thumbnail = itemView.findViewById(R.id.courseThumbNailImageView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int position = getAdapterPosition();
if (listener != null) {
if (position != RecyclerView.NO_POSITION) {
listener.onItemClick(position, view);
}
}
}
});
}
}
#Override
public int getItemViewType(int position) {
if (mSender.equals("home_page")) {
return HOME_PAGE;
} else if (mSender.equals("displayCourse")) {
return DISPLAY_COURSE;
}
return -1;
}
}
View pager adapter
public class VideoSliderAdapter extends PagerAdapter {
private boolean doNotifyDataSetChangedOnce = false;
private static final String TAG = "VideoSliderAdapter";
private YouTubePlayerView youTubePlayerView;
private View gradientView;
private ImageView thumbNail;
private LinearLayout layout;
private Picasso picasso;
private List<Video> videoList;
private Context mContext;
private Lifecycle mLifeCycle;
public VideoSliderAdapter(List<Video> videoList, Context context, Lifecycle lifecycle) {
this.videoList = videoList;
this.mContext = context;
this.mLifeCycle = lifecycle;
doNotifyDataSetChangedOnce = true;
picasso = Picasso.get();
}
#Override
public int getCount() {
if (doNotifyDataSetChangedOnce) {
doNotifyDataSetChangedOnce = false;
notifyDataSetChanged();
}
return videoList.size();
}
#Override
public int getItemPosition(#NonNull Object object) {
return POSITION_NONE;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return (view == (CardView) object);
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.video_paly_layout, container, false);
TextView title = view.findViewById(R.id.videoTitle);
title.setText(videoList.get(position).getVideoTitle());
gradientView = view.findViewById(R.id.gradientView);
thumbNail = view.findViewById(R.id.videoThumbNail);
picasso.load(videoList.get(position).getVideoThumbNail())
.networkPolicy(NetworkPolicy.OFFLINE)
.into(thumbNail, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError(Exception e) {
picasso.load(videoList.get(position).getVideoThumbNail())
.error(R.drawable.ofklogo)
.into(thumbNail, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError(Exception e) {
}
});
}
});
layout = view.findViewById(R.id.videoPlayLayout);
youTubePlayerView = view.findViewById(R.id.youtube_player_view);
mLifeCycle.addObserver(youTubePlayerView);
new AddListener(youTubePlayerView, position).execute();
container.addView(view);
return view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((CardView) object);
}
Recycler view holder layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="150dp"
android:layout_height="200dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/courseThumbNailImageView"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="#drawable/art_thumb" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/courseThumbNailImageView">
<TextView
android:id="#+id/courseTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:maxLines="2"
android:text="Course title"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
view pager layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="200dp"
app:cardCornerRadius="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
android:id="#+id/youtube_player_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:autoPlay="false" />
<ImageView
android:id="#+id/videoThumbNail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />
<View
android:id="#+id/gradientView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_drawable" />
<LinearLayout
android:id="#+id/videoPlayLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="16dp">
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:padding="8dp"
android:src="#drawable/play_button" />
<TextView
android:id="#+id/videoTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:text="Course title"
android:textColor="#android:color/white"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
When loading image using Picasso use: setIndicatorsEnabled(false)
picasso.load(videoList.get(position).getVideoThumbNail())
.networkPolicy(NetworkPolicy.OFFLINE)
.setIndicatorsEnabled(false)
.into(...);
The colors indicate this:
Green: Image is fetched from memory
Blue: Image is fetched from disk
Red: Image is fetched from network

RecyclerView items not displayed

I am getting really frustrated with this. I don't understand why my CardView grid items are not displayed in my RecyclerView
Here is CardView grid item fragment_album_cardview_item.xml
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="8dp"
android:layout_marginBottom="16dp"
card_view:cardBackgroundColor="#android:color/white">
</android.support.v7.widget.CardView>
Here I set the adapter
public void setGridViewAdapter(){
GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 2);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerViewAdapter = new RecyclerViewAdapter(App.mApp.mAlbums);
mRecyclerView.setAdapter(mRecyclerViewAdapter);
}
And here is my adapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.Holder> {
private List<Album> itemList;
//private Context context;
// TODO adapter may be initialized by the time some of the albums have been added to the list
public RecyclerViewAdapter(List<Album> itemList) {
this.itemList = itemList;
Timber.d("itemList.size = "+String.valueOf(itemList.size()));
Timber.d("itemList.get(0).name = "+itemList.get(0).name);
//this.context = context;
}
#Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
Timber.d("onCreateViewHolder");
View cardView = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_album_cardview_item, null);
Timber.d("cardView = "+String.valueOf(cardView));
Holder rcv = new Holder(cardView);
return rcv;
}
#Override
public void onBindViewHolder(Holder holder, int position) {
//holder.countryName.setText(itemList.get(position).getName());
//holder.countryPhoto.setImageResource(itemList.get(position).getPhoto());
}
#Override
public int getItemCount() {
return this.itemList.size();
}
// HOLDER
public class Holder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView countryName;
public ImageView countryPhoto;
public Holder(View itemView) {
super(itemView);
//itemView.setOnClickListener(this);
//countryName = (TextView)itemView.findViewById(R.id.country_name);
//countryPhoto = (ImageView)itemView.findViewById(R.id.country_photo);
}
#Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "Clicked Country Position = " + getPosition(), Toast.LENGTH_SHORT).show();
}
}
}
Apparently, just adding a some kind of widget for example a TextView to my CardView results in some grid items being shown
<android.support.v7.widget.CardView
android:id="#+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="10dp"
android:layout_height="100dp"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="8dp"
android:layout_marginBottom="16dp"
card_view:cardBackgroundColor="#android:color/white">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Bleh"/>
</android.support.v7.widget.CardView>

Horizontal RecyclerView Like Paytm

I have to implement a custom horizontal RecyclerView having a header (title) at top and a section (See All) right side at the end of the RecyclerView.
I created a RecyclerView with a header and footer but I want to have a right sided section (See All) of which onclick event I wish to fire some event.
In Paytm App, it is implemented
I wish to get results as follows
This will Gives You Idea
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/verticalScrollRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView></RelativeLayout>
vertical_scroll_single_entry.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:weightSum="1"
android:gravity="center_vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</android.support.v7.widget.RecyclerView>
<Button
android:id="#+id/selectAllButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="See All >>"
android:textAllCaps="false"/></LinearLayout>
Custom Adapter class For Vertical Scroller
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.CustomViewHolder> {
private Context context;
private ArrayList arrayList;
private LayoutInflater layoutInflater;
public CustomAdapter(Context context, ArrayList arrayList) {
this.context = context;
this.layoutInflater = LayoutInflater.from(context);
this.arrayList = arrayList;
}
#Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = layoutInflater.inflate(R.layout.vertical_scroll_single_entry, parent, false);
return new CustomViewHolder(view);
}
#Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
//initialise values to views inside holder at runtime
holder.recyclerView.setAdapter(new CustomAdapterTwo(context, arrayList));
holder.recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
holder.recyclerView.setHasFixedSize(true);
}
#Override
public int getItemCount() {
return arrayList.size();
}
class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
RecyclerView recyclerView;
Button selectAllButton;
public CustomViewHolder(View itemView) {
super(itemView);
recyclerView = (RecyclerView) itemView.findViewById(R.id.recyclerView);
selectAllButton = (Button) itemView.findViewById(R.id.selectAllButton);
selectAllButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Toast.makeText(context, "Select All At : " + String.valueOf(getLayoutPosition()), Toast.LENGTH_SHORT).show();
}
}}
horizontal adapter single entry file recycler_view_single_item.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="match_parent" android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remove this button \n and put image view"
android:textAllCaps="false"/></LinearLayout>
Horizontal recycler view adapter class
public class CustomAdapterTwo extends RecyclerView.Adapter<CustomAdapterTwo.CustomViewHolder> {
private Context context;
private ArrayList arrayList;
private LayoutInflater layoutInflater;
public CustomAdapterTwo(Context context, ArrayList arrayList) {
this.context = context;
this.arrayList = arrayList;
this.layoutInflater = LayoutInflater.from(context);
}
#Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = layoutInflater.inflate(R.layout.recycler_view_single_item, parent, false);
return new CustomViewHolder(view);
}
#Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
}
#Override
public int getItemCount() {
return arrayList.size();
}
class CustomViewHolder extends RecyclerView.ViewHolder {
public CustomViewHolder(View itemView) {
super(itemView);
}
}}
Your main activity class
public class MainActivity extends AppCompatActivity {
private RecyclerView verticalScrollRecyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialiseView();
}
private void initialiseView() {
verticalScrollRecyclerView = (RecyclerView) findViewById(R.id.verticalScrollRecyclerView);
ArrayList<String> stringArrayList = new ArrayList<>();
stringArrayList.add("One");
stringArrayList.add("Two");
stringArrayList.add("Three");
stringArrayList.add("Four");
stringArrayList.add("Five");
stringArrayList.add("Six");
stringArrayList.add("Seven");
stringArrayList.add("Eight");
stringArrayList.add("Nine");
stringArrayList.add("Ten");
//setting adapter and layout manager to recyclerView
verticalScrollRecyclerView.setLayoutManager(new LinearLayoutManager(this));
verticalScrollRecyclerView.setAdapter(new CustomAdapter(this, stringArrayList));
verticalScrollRecyclerView.setHasFixedSize(true);
}}
Looks Like

When scroll in Recyler View Radio Button changes its selection

I am using RecylerView to set RadioButton.But on selection selected changes its position when scroll.Please help me.
Thanx in advance!!!
My RecylerView Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/dummy_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
My RadioButton Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/radio_dummy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>
My RecylerView Java File
public class Dummy extends AppCompatActivity {
ArrayList<String> arr_qty;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dummy);
arr_qty=new ArrayList<>();
for(int i =0;i<50;i++){
arr_qty.add(String.valueOf(i));
}
RecyclerView dummy = (RecyclerView)findViewById(R.id.dummy_recycle);
DummyAdapter adapter = new DummyAdapter(getApplicationContext(), arr_qty);
dummy.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
dummy.setAdapter(adapter);
}
}
My RecylerView Adapter
public class DummyAdapter extends RecyclerView.Adapter<DummyAdapter.MyViewHolder>{
private Context c;
ArrayList<String> arr=new ArrayList<>();
public DummyAdapter(Context context, ArrayList<String> arr_qty) {
this.arr = arr_qty;
this.c = context;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.dummy_value, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
}
#Override
public int getItemCount() {
return arr.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public RadioButton radio;
public MyViewHolder(View itemView) {
super(itemView);
radio = (RadioButton) itemView.findViewById(R.id.radio_dummy);
}
}
}
In Recycleview adapter you can add the below the line. It's already working in my code. This code helps to fix your list position.
public int getItemViewType(int position) {
return position;
}

Set image in cardview one by one from drawable

I am trying to add images in a cardview from drawable and text one by one using a recyclerview. say there are 10 images in my drawable i want to use only 5 of them. So how to do that ?
here is my cardview :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/placeCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
card_view:cardCornerRadius="#dimen/card_corner_radius">
<ImageView
android:id="#+id/placeImage"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop" />
<!-- Used for the ripple effect on touch -->
<LinearLayout
android:id="#+id/mainHolder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:selectableItemBackground"
android:orientation="horizontal" />
<LinearLayout
android:id="#+id/placeNameHolder"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_gravity="bottom"
android:orientation="horizontal">
<TextView
android:id="#+id/placeName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="left"
android:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/white" />
</LinearLayout>
</android.support.v7.widget.CardView>
here is my recyclerview adapter :
public class SubPlaceAdapter extends RecyclerView.Adapter<SubPlaceRecyclerViewHolder>{
String ImageUri;
#Override
public SubPlaceRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_places, null);
SubPlaceRecyclerViewHolder viewHolder = new SubPlaceRecyclerViewHolder(view);
}
#Override
public void onBindViewHolder(SubPlaceRecyclerViewHolder holder, int position) {
// holder.subPlaceImage
}
#Override
public int getItemCount() {
return 0;
}
}
my view holder :
public class SubPlaceRecyclerViewHolder extends RecyclerView.ViewHolder {
protected ImageView subPlaceImage;
protected TextView subPlaceTitle;
public SubPlaceRecyclerViewHolder(View view) {
super(view);
this.subPlaceImage = (ImageView) view.findViewById(R.id.placeImage);
this.subPlaceTitle = (TextView) view.findViewById(R.id.placeName);
}
}
thanks for the help :)
Step 1. First create a Model
public class ImageModel {
int imageId;
String aboutText;
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public String getAboutText() {
return aboutText;
}
public void setAboutText(String aboutText) {
this.aboutText = aboutText;
}
}
Step 2. Create a method which return arraylist of imageModel in your fragment/activity from where you set the adapter
private ArrayList<ImageModel> setImageData(){
ArrayList<ImageModel> projectList=new ArrayList<>();
ImageModel imageModel=new ImageModel();
imageModel.setImageId(R.mipmap.ic_star_fill);
imageModel.setAboutText("RandomText");
projectList.add(imageModel);
ImageModel imageModel1=new ProjectModel();
projectModel.setImageId(R.mipmap.ic_star_fill);
projectModel.setAboutText("RandomText");
projectList.add(projectModel);
return projectList;
}
Step 3. Create constructor in your adapter which takes ArrayList as an argument
public SubPlaceAdapter(ArrayList<ImageModel> mImageList) {
this.mActivity = mActivity;
this.mImageList = mImageList);
}
Step 4. Set your adapter in your fragment/ activity.
mSubPlaceAdapter = new SubPlaceAdapter(setImageData());
Step 5. Set your item in on BindView Holder
#Override
public void onBindViewHolder(SubPlaceRecyclerViewHolder holder, int position) {
ImageModel imageModel= mImageList.get(position)
holder.subPlaceImage.setImageResource(imageModel.getImageId());
}
#Override
public int getItemCount() {
return mImageList.size();
}
This code will give you the idea how to do it.Fill free to modify this answer.

Categories

Resources