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.
Related
I am a noob in Android Development, and I was trying to build a custom sidebar today.
I used a view with 2 imageViews, and I was trying to inflate them multiple times with multiple icons, which I did using a custom class. Here is the view code:
Side_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:src="#drawable/icn"
android:layout_height="48dp"
android:tint="#afafaf"
android:id="#+id/menu_icon"
/>
<ImageView
android:layout_width="8dp"
android:layout_height="8dp"
android:id="#+id/menu_selector"
android:src="#drawable/selector"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
Here is my custom class:
public class Item
{
private int icon, code;
private boolean isSelected;
public Item(int icon, int code)
{
this.icon = icon;
this.code = code;
}
public int getIcon()
{
return icon;
}
public void setIcon(int icon)
{
this.icon = icon;
}
public int getCode()
{
return code;
}
public void setCode(int code)
{
this.code = code;
}
public boolean isSelected()
{
return isSelected;
}
public void setSelected(boolean selected)
{
isSelected = selected;
}
public Item(int icon, int code, boolean isSelected)
{
this.icon = icon;
this.code = code;
this.isSelected = isSelected;
}
}
I used this code for assigning data to this class:
public class MenuUtils
{
public static final int HOME = 0, EDU = 1, CONTACT = 2, PROJECTS = 3;
public static List<Item> getList()
{
List<Item> list = new ArrayList<>();
list.add(new Item(R.drawable.icn1, HOME, true));
list.add(new Item(R.drawable.icn2, EDU, true));
list.add(new Item(R.drawable.icn3, CONTACT, true));
list.add(new Item(R.drawable.icn4, PROJECTS, true));
return list;
}
}
Here is the code for my adapter:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>
{
List<Item> list;
public MyAdapter(List<Item> list)
{
this.list = list;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.side_bar, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position)
{
holder.icon.setImageResource(list.get(position).getIcon());
if (list.get(position).isSelected()) holder.selected.setVisibility(View.VISIBLE);
else holder.selected.setVisibility(View.INVISIBLE);
}
#Override
public int getItemCount()
{
Log.i("List Size", list.toString());
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder
{
ImageView icon, selected;
public MyViewHolder(#NonNull View itemView)
{
super(itemView);
icon = (ImageView) itemView.findViewById(R.id.menu_icon);
selected = (ImageView) itemView.findViewById(R.id.menu_selector);
}
}
}
The method used for setting up the sidebar:
private void setMenu()
{
menu = findViewById(R.id.menu_holder);
List<Item> items = MenuUtils.getList();
MyAdapter adapter = new MyAdapter(items);
menu.setLayoutManager(new LinearLayoutManager(this));
menu.setAdapter(adapter);
}
On logging, I see that the list has 4 data items, but when inflated, only one appears.
Why so?
Please help.
I guess each item of recycle view is taking the entire screen that's why you can only see one item. Adjust the LinearLayout height and width, as-
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
......
</LinearLayout>
If you want the each item to take entire screen width use
android:layout_width="match_parent"
or vice-versa.
Happy coding !!!
Because you are using height as match_parent.
It should be wrap_content:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:src="#drawable/icn"
android:layout_height="48dp"
android:tint="#afafaf"
android:id="#+id/menu_icon"
/>
<ImageView
android:layout_width="8dp"
android:layout_height="8dp"
android:id="#+id/menu_selector"
android:src="#drawable/selector"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
Because the first item expand full screen, therefore you can't see the others
You have to change your xml height from match_parent to wrap_content
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"> <!--change this line-->
<ImageView
android:layout_width="match_parent"
android:src="#drawable/icn"
android:layout_height="48dp"
android:tint="#afafaf"
android:id="#+id/menu_icon"
/>
<ImageView
android:layout_width="8dp"
android:layout_height="8dp"
android:id="#+id/menu_selector"
android:src="#drawable/selector"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
I want to build a complex layout using recyclerview android. In the layout, I want to have a camera button to the top left fixed and a recyclerview wrapped around it with gallery images. I have checked flexbox layout manager for recyclerview but it doesn't seem to match my use-case.
I want the header to be non-repeating and not to scroll with other items vertically. Here's the layout for the header:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/shareLayout"
android:layout_width="185dp"
android:layout_height="135dp"
android:layout_below="#id/trendingToolbar"
android:background="#color/black">
<ImageView
android:id="#+id/cameraShareIV"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
app:srcCompat="#drawable/camera_white" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/cameraShareIV"
android:layout_centerHorizontal="true">
<TextView
android:id="#+id/infoTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:gravity="center_horizontal"
android:text="#string/share_pic_video"
android:textColor="#android:color/white"
android:textSize="13sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/infoTxt"
android:layout_marginLeft="16dp"
android:text="#string/share_timeout_txt"
android:textColor="#color/colorPrimaryDark"
android:textSize="11sp"
android:textStyle="bold" />
</RelativeLayout>
and in my activity, here's the XML:
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="base.android.com.thumbsapp.UI.Fragments.TrendingFragment">
<include layout="#layout/trending_toolbar"
android:id="#+id/trendingToolbar"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/trendingRV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/trendingToolbar"/>
Previously, I had the header inside the activity XML but had no way to wrap a recyclerview around it. So, I have decide to use an adapter like below:
public class TrendingAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final String TAG = TrendingAdapter.class.getSimpleName();
private Context context;
private List<Trending> itemList;
private static final int HEADER = 0;
private static final int ITEMS = 1;
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
switch (viewType){
case HEADER:
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.trending_header, parent, false);
return new TrendingHeaderViewHolder(v);
case ITEMS:
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.trending_items_layout, parent, false);
return new TrendingItemsViewHolder(v);
}
return null;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Trending tr = itemList.get(position);
if (holder instanceof TrendingHeaderViewHolder){
((TrendingHeaderViewHolder) holder).cameraShareIV.setOnClickListener( view -> {
// TODO: 4/2/2018 select image from gallery
});
} else if (holder instanceof TrendingItemsViewHolder){
// TODO: 4/2/2018 populate gallery items here with picasso
}
}
#Override
public int getItemCount() {
return itemList.size();
}
#Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
}
I'm confused how to make the header stick and also what to do for getItemViewType method.
Is this the right way to approach this?
Can anyone help out? Thanks.
For this lay out i suggest better option is use this header view
https://github.com/edubarr/header-decor
To make things simple i suggest you to look into this library
In your XML Place RecylerView into StickyHeaderView,choose horizontal or vertical orientation for your RecylerView
<tellh.com.stickyheaderview_rv.StickyHeaderView
android:id="#+id/stickyHeaderView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:scrollbars="vertical" />
</tellh.com.stickyheaderview_rv.StickyHeaderView>
Create data bean class for each item type in RecyclerView. They should extend DataBean. Override the method
public boolean shouldSticky() to decide whether the item view should be suspended on the top.
public class User extends DataBean {
private String login;
private int id;
private String avatar_url;
private boolean shouldSticky;
#Override
public int getItemLayoutId(StickyHeaderViewAdapter adapter) {
return R.layout.item_user;
}
public void setShouldSticky(boolean shouldSticky) {
this.shouldSticky = shouldSticky;
}
// Decide whether the item view should be suspended on the top.
#Override
public boolean shouldSticky() {
return shouldSticky;
}
}
public class ItemHeader extends DataBean {
private String prefix;
#Override
public int getItemLayoutId(StickyHeaderViewAdapter adapter) {
return R.layout.header;
}
#Override
public boolean shouldSticky() {
return true;
}
}
Create ViewBinder to bind different type views with specific data beans. As you see, provideViewHolder(View itemView) corresponds for onCreateViewHolder in RecyclerView, and bindView corresponds for onBindViewHolder in RecyclerView.
public class ItemHeaderViewBinder extends ViewBinder<ItemHeader, ItemHeaderViewBinder.ViewHolder> {
#Override
public ViewHolder provideViewHolder(View itemView) {
return new ViewHolder(itemView);
}
#Override
public void bindView(StickyHeaderViewAdapter adapter, ViewHolder holder, int position, ItemHeader entity) {
holder.tvPrefix.setText(entity.getPrefix());
}
#Override
public int getItemLayoutId(StickyHeaderViewAdapter adapter) {
return R.layout.header;
}
static class ViewHolder extends ViewBinder.ViewHolder {
TextView tvPrefix;
public ViewHolder(View rootView) {
super(rootView);
this.tvPrefix = (TextView) rootView.findViewById(R.id.tv_prefix);
}
}
}
Instantiate StickyHeaderViewAdapter for RecyclerView and register ViewBinders for each item types.
rv = (RecyclerView) findViewById(R.id.recyclerView);
rv.setLayoutManager(new LinearLayoutManager(this));
List<DataBean> userList = new ArrayList<>();
adapter = new StickyHeaderViewAdapter(userList)
.RegisterItemType(new UserItemViewBinder())
.RegisterItemType(new ItemHeaderViewBinder());
rv.setAdapter(adapter);
RecyclerView Adapter Class.
public class TravelListAdapter extends RecyclerView.Adapter<TravelListAdapter.ViewHolder> {
Context mContext;
OnItemClickListener mItemClickListener;
String []names = {"Hotels", "Travel", "Medicine", "Education", "Travel", "Hotels"};
private int[] advertImageList = {R.drawable.hotel, R.drawable.travel, R.drawable.medical, R.drawable.education, R.drawable.travel, R.drawable.hotel};
// 2
public void setOnItemClickListener(OnItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
public TravelListAdapter(Context context) {
this.mContext = context;
}
// 3
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public LinearLayout placeHolder;
public LinearLayout placeNameHolder;
public TextView placeName;
public ImageView placeImage;
public ViewHolder(View itemView) {
super(itemView);
placeHolder = itemView.findViewById(R.id.mainHolder);
placeName = itemView.findViewById(R.id.placeName);
placeNameHolder = itemView.findViewById(R.id.placeNameHolder);
placeImage = itemView.findViewById(R.id.placeImage);
placeHolder.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(itemView, getPosition());
}
}
}
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_row_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
// final Place place = new PlaceData().placeList().get(position);
holder.placeName.setText(names[position]);
holder.placeName.setTextColor(R.color.black);
Picasso.with(mContext).load(advertImageList[position]).into(holder.placeImage);
Bitmap photo = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.image2);
Palette.generateAsync(photo, new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
//int bgColor = palette.getMutedColor(mContext.getResources().getColor(android.R.color.transparent));
holder.placeNameHolder.setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));
}
});
}
#Override
public int getItemCount() {
return names.length;
}
}`
My Xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/placeCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:cardBackgroundColor="#android:color/transparent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/placeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:transitionName="tImage"
android:layout_centerInParent="true"
android:tint="#android:color/white"
android:padding="10dp"/>
<!-- Used for the ripple effect on touch -->
<LinearLayout
android:id="#+id/mainHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
android:orientation="horizontal" />
<LinearLayout
android:id="#+id/placeNameHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:transitionName="tNameHolder"
android:padding="5dp"
android:layout_below="#+id/placeImage">
<TextView
android:id="#+id/placeName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceListItem"
android:textColor="#android:color/white"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
I am using vector images generated from Android Studio but those images are not displayed in recyclerView.
What should i do? Let me mention that the vector images that i use inside recyclerView are properly displayed if i use them in simple image views.
I am using RecyclerView in my fragment to show images with text in Grid format,the Recycler view grid_item.xml look like following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#id/card_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_margin="#dimen/card_margin_grid"
card_view:cardCornerRadius="#dimen/card_album_radius">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#id/thumbnail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="?selectableItemBackgroundBorderless"
android:clickable="true"
android:scaleType="fitCenter" />
<TextView
android:id="#id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/thumbnail"
android:layout_margin="#dimen/album_title_padding"
android:textColor="#color/album_title"
android:textSize="#dimen/album_title" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
I am using adapter to populate data in RecyclerView, the code for this is :
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<Movie> movies;
private Context context;
public DataAdapter(Context context, ArrayList<Movie> movies) {
this.movies = movies;
this.context = context;
}
public void addItems(ArrayList<Movie> movies) {
this.movies.addAll(movies);
}
#Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_view_grid_item, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {
viewHolder.title.setText(movies.get(i).getTitle());
Picasso.with(context).load(movies.get(i).getImage_url_medium()).placeholder(R.drawable.placeholder).into(viewHolder.thumbnail);
}
#Override
public int getItemCount() {
return movies.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView title;
private ImageView thumbnail;
public ViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
view.setOnClickListener(this);
}
// Handles the row being being clicked
#Override
public void onClick(View view) {
int position = getAdapterPosition(); // gets item position
if (position != RecyclerView.NO_POSITION) { // Check if an item was deleted, but the user clicked it before the UI removed it
Movie movie = movies.get(position);
// start detail activity
Intent i = new Intent(context, MovieDetail.class);
i.putExtra(Constants.MOVIES_OBJECT, movie);
context.startActivity(i);
}
}
}
}
My problem is click listener is only working on the TextView and not on the image , although I have set click listener on whole view which contains both image and text.Is something wrong with my implementation ?
try setting android:focusableInTouchMode="false" to your imageview.
and remove android:clickable="true" or set it to false
Use viewHolder.itemView like :
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//your action
});
This will enable click action on whole RecyclerView item.
Add android:clickable="true" to the root RelativeLayout.
<?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/card_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_margin="#dimen/card_margin_grid"
android:clickable="true"
android:focusableInTouchMode="true"
card_view:cardCornerRadius="#dimen/card_album_radius">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#id/thumbnail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="?selectableItemBackgroundBorderless"
android:clickable="false"
android:focusableInTouchMode="false"
android:scaleType="fitCenter" />
<TextView
android:id="#id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/thumbnail"
android:layout_margin="#dimen/album_title_padding"
android:textColor="#color/album_title"
android:textSize="#dimen/album_title" />
</RelativeLayout>
</android.support.v7.widget.CardView>
public class OffersRecycleViewAdapter extends RecyclerView.Adapter {
private Activity mActivity;
private List<OfferShopModel> offerShopModels = new ArrayList<>();
ArrayList<String> allColors = new ArrayList<String>();
public OffersRecycleViewAdapter(List<OfferShopModel> offerShopModels, Activity activity, ArrayList<String> allColors) {
this.offerShopModels = offerShopModels;
this.mActivity = activity;
}
// String[] allColors = mActivity.getResources().getStringArray(R.array.colors);
#Override
public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview_offers, viewGroup, false);
return new ItemViewHolder(v);
}
#TargetApi(Build.VERSION_CODES.M)
#Override
public void onBindViewHolder(ItemViewHolder itemViewHolder, final int position) {
itemViewHolder.mOffer.setText(offerShopModels.get(position).getOffer());
}
#Override
public int getItemCount() {
return offerShopModels.size();
}
class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView mLinearLayoutBack;
TextView mOffer;
ItemViewHolder(View itemView) {
super(itemView);
mOffer = (TextView) itemView.findViewById(R.id.offer);
mOffer.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// call click event
}
}
}
remove android:clickable="true" from your ImageView or change it to android:clickable="false"
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.