Can't show images on RecyclerView - android

I want to create a list of items that have a background image and a TextView. Although it creates the RecyclerView normally and sets the text, the background image doesn't set.
This is my Adapter Class
public class Casino_Adapter extends RecyclerView.Adapter<Casino_Adapter.ViewHolder> {
private Data[] mDataset;
private ClickListener clickListener;
public Context context;
// Provide a suitable constructor (depends on the kind of dataset)
public Casino_Adapter(Data[] myDataset) {
this.mDataset = myDataset;
this.context = context;
}
#Override
public Casino_Adapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.casino_card_row, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private final Context context = null;
// each data item is just a string in this case
public TextView mTextView;
public ImageView mImageView;
public CardView cardView;
//Typeface tf;
public ViewHolder(View v) {
super(v);
mTextView = (TextView) v.findViewById(R.id.text);
mImageView = (ImageView) v.findViewById(R.id.image);
cardView = (CardView) v.findViewById(R.id.card_view);
//cardView.setPreventCornerOverlap(false);
}
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.mTextView.setText(mDataset[position].getText());
holder.mImageView.setImageResource(mDataset[position].getImage());
}
public void setClickListener(ClickListener clickListener) {
this.clickListener = clickListener;
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return mDataset.length;
}
public interface ClickListener {
public void itemClicked(View view, int pos);
}
}
I have a Fragment Class where I want the RecyclerView to be.
public class CasinoFragment extends Fragment {
protected RecyclerView recyclerView;
protected RecyclerView.Adapter mAdapter;
protected RecyclerView.LayoutManager mLayoutManager;
public CasinoFragment() {
// Required empty public constructor
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initial();
}
private void initial() {
final Data datas[] =
{
new Data("This is an item", R.drawable.ic_home_white_36dp),
new Data("This is an item 2", R.drawable.car),
new Data("asdasd`sdads", R.drawable.car),
new Data("This is an item 3", R.drawable.car)
};
mAdapter = new Casino_Adapter(datas);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_casino, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(mAdapter);
return view;
}
}
And these are the Setters that I use.
public class Data {
int image;
String text;
public Data(String title, int backimage)
{
this.text = title;
this.image = backimage;
}
public String getText()
{
return text;
}
public int getImage()
{
return image;
}
public void setText()
{
this.text=text;
}
public void setImageID()
{
this.image = image;
}
}
Casino Row 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="wrap_content"
card_view:cardCornerRadius="8dp"
android:layout_margin="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#111111"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/image"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:background="#drawable/image_round"
android:src="#drawable/car" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/image"
android:id="#+id/rel_color"
android:background="#4c4c4c">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Μπύρα"
android:textColor="#fcfcfc"
android:textSize="30sp"
android:shadowColor="#000000"
android:shadowDx="3"
android:shadowDy="3"
android:shadowRadius="5"
android:id="#+id/text"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
The result I get is the following.

The reason why you aren't seeing your image, is because it isn't actually visible.
Your rel_color layout has the same size attributes as your ImageView.
If you want to display a layout above the ImageView, you just need to remove the background of it, otherwise the ImageView will be hidden.

Just Like that. but remember your images dimensions must be low. in my case i used 200x200
<?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/rl_menu_item"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_margin="5dp"
android:background="#android:color/transparent"
card_view:cardCornerRadius="4dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:id="#+id/img_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg_blrr">
<ImageView
android:id="#+id/iv_icon"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal"
android:adjustViewBounds="true"
android:background="#drawable/gray_circle"
android:padding="15dp"
android:scaleType="centerCrop"
android:src="#drawable/school" />
<TextView
android:id="#+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#id/iv_icon"
android:layout_alignLeft="#id/iv_icon"
android:layout_alignRight="#id/iv_icon"
android:layout_alignStart="#id/iv_icon"
android:layout_below="#id/iv_icon"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="Your Favourite Place"
android:textColor="#android:color/black" />
</RelativeLayout>
</android.support.v7.widget.CardView>

Related

RecyclerView items inside BottomSheetDialogFragment need double touch after fast scrolling

I have a RecyclerView inside BottomSheetDialogFragment. The RecyclerView items touch working normally when it's scrolling slowly.
But when the RecyclerView is scrolled fast and after the list stops (without touching), than touching on any item doesn't work on fast touch. It needs double touching.
See in the below example gif, when touching on Andhra Pradesh it's working fine. After slow scrolling, touching on Haryana also works fine. Then doing a fast scroll and touching on Punjab doesn't work on the first touch. Touching again it works.
Following is the code:
OperatorListDialogFragment.java
package com.*;
import *;
public class OperatorListDialogFragment extends BottomSheetDialogFragment{
private static final String ARG_NAME = "item_name";
private static final String ARG_LOGO = "item_logo";
private Listener mListener;
private String header;
private Context mContext;
public static OperatorListDialogFragment newInstance(String[] name, int[] logo, String header) {
final OperatorListDialogFragment fragment = new OperatorListDialogFragment();
final Bundle args = new Bundle();
args.putStringArray(ARG_NAME, name);
args.putIntArray(ARG_LOGO, logo);
args.putString("header", header);
fragment.setArguments(args);
return fragment;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_operator_list_dialog_list_dialog, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
TextView headerTV = view.findViewById(R.id.title);
headerTV.setText(getArguments().getString("header"));
final RecyclerView recyclerView = view.findViewById(R.id.list);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(new OperatorAdapter(getArguments().getStringArray(ARG_NAME), getArguments().getIntArray(ARG_LOGO)));
view.findViewById(R.id.dismiss).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
}
});
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
mContext = context;
final Fragment parent = getParentFragment();
if (parent != null) {
mListener = (Listener) parent;
} else {
mListener = (Listener) context;
}
}
#Override
public void onDetach() {
mListener = null;
super.onDetach();
}
public interface Listener {
void onFilterSelected(String selected, String selectedQuery);
}
private class ViewHolder extends RecyclerView.ViewHolder {
final TextView text;
ImageView logo;
ViewHolder(LayoutInflater inflater, ViewGroup parent) {
// TODO: Customize the item layout
super(inflater.inflate(R.layout.fragment_operator_list_dialog_list_dialog_item, parent, false));
text = itemView.findViewById(R.id.tv_operator_name);
logo = itemView.findViewById(R.id.iv_recharge_provider_icon);
}
}
private class OperatorAdapter extends RecyclerView.Adapter<ViewHolder> {
private String[] mNames;
private int[] mLogos;
OperatorAdapter(String[] name, int[] logo) {
mNames = name;
mLogos = logo;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.text.setText(mNames[position]);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("clicked", "" + position);
}
});
}
#Override
public int getItemCount() {
return mNames.length;
}
}
}
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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_height="match_parent"
android:layout_width="match_parent"
>
<ImageView
android:focusable="true"
android:clickable="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="Close"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:id="#+id/dismiss"
android:padding="14dp"
android:src="#drawable/ic_close_black_24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/title"
app:layout_constraintTop_toTopOf="#id/dismiss"
app:layout_constraintBottom_toBottomOf="#id/dismiss"
app:layout_constraintLeft_toRightOf="#id/dismiss"
android:padding="14dp"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
tools:text="Select operator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<View
app:layout_constraintTop_toBottomOf="#id/dismiss"
android:background="#969696"
android:layout_width="match_parent"
android:layout_height="0.5dp"/>
<androidx.recyclerview.widget.RecyclerView
app:layout_constraintTop_toBottomOf="#id/dismiss"
app:layout_constraintBottom_toBottomOf="parent"
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
app:layout_constrainedHeight="true"
android:paddingTop="#dimen/list_item_spacing_half"
android:paddingBottom="#dimen/list_item_spacing_half"
tools:context=".fragments.OperatorListDialogFragment"
tools:listitem="#layout/fragment_operator_list_dialog_list_dialog_item" />
</androidx.constraintlayout.widget.ConstraintLayout>
recycler_item.xml
<?xml version="1.0" encoding="utf-8"?>
<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:gravity="center_vertical"
android:orientation="horizontal"
android:id="#+id/ll_operator_list_wrapper"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:visibility="gone"
android:layout_marginLeft="16dp"
android:id="#+id/iv_recharge_provider_icon"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginVertical="16dp"
android:layout_centerVertical="true"
android:src="#drawable/ic_bsnl_logo"
tools:visibility="visible"/>
<TextView
android:padding="16dp"
android:textColor="#212121"
android:textSize="14sp"
android:ellipsize="end"
android:id="#+id/tv_operator_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="BSNL"
android:layout_toRightOf="#+id/iv_recharge_provider_icon"
android:layout_centerInParent="true"/>
<View
android:layout_below="#id/iv_recharge_provider_icon"
android:id="#+id/divider0"
android:background="#eeeeee"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_marginLeft="16dp"
android:layout_toRightOf="#+id/iv_recharge_provider_icon"
/>
</RelativeLayout>
add android:nestedScrollingEnabled="false" in RecyclerView

NavigationAdapter Using Two Icons

I use navigationview. I want to use two icons in navigation view. Currently, when I use .seticon, it changes 2 icons. I want to define icons in two icons separately. I want to define separate icons for navIconNew and navIcon. How can we do that?
Navigation Adapter
public class NavigationAdapter extends RecyclerView.Adapter<NavigationAdapter.ViewHolder> {
Context context;
ArrayList<NavigationDataModel> arrayList = new ArrayList<>();
public NavigationAdapter(Context context, ArrayList<NavigationDataModel> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.navigationrecyclerview_adapter11, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.navIcon.setImageResource(arrayList.get(position).getIcon());
holder.navIconNew.setImageResource(arrayList.get(position).getIcon());
holder.rootView.setBackgroundColor(arrayList.get(position).getColor());
holder.navTitle.setText(arrayList.get(position).getTitle());
}
#Override
public int getItemCount() {
return arrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView navIcon, navIconNew;
TextView navTitle;
LinearLayout rootView;
public ViewHolder(View itemView) {
super(itemView);
rootView = itemView.findViewById(R.id.rootView);
navIcon = itemView.findViewById(R.id.navIcon);
navIconNew = itemView.findViewById(R.id.navIconNew);
navTitle = itemView.findViewById(R.id.navTitle);
}
}
navigationrecyclerview_adapter11
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/rootView"
android:orientation="horizontal"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/navIcon"
android:layout_margin="10dp"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:tint="#color/white"
android:layout_width="30dp"
android:layout_height="30dp"
/>
<ImageView
android:id="#+id/navIconNew"
android:layout_margin="10dp"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:tint="#color/white"
android:layout_width="30dp"
android:layout_height="30dp"
/>
<TextView
android:id="#+id/navTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lineSpacingMultiplier="0.6"
android:text="Item"
android:fontFamily="#font/sanspro_semibold"
android:textColor="#color/white"
android:textSize="20dp"
android:layout_marginTop="10dp"
/>
</LinearLayout>
Activity
NavigationDataModel model8 = new NavigationDataModel();
model8.setColor(ContextCompat.getColor(this, R.color.red));
model8.setTitle("Mesaj At");
model8.setIcon(R.drawable.unread);
arrayList.add(model8);

OnClickListener not working with fragments

I'm cant get the onclicklistener to work with fragments. I've searched stackoverflow and tried all the tips but i still cant get it to work. So i do my first post here. I've tried adding android:focusable="false", android:clickable="false" and android:descendantFocusability="blocksDescendants" to the layouts with no luck. Ive removed them because they make no difference. I've tried other solutions as well but none of them works. This is my first post so if i posted something wrong let me know and ill redo it, I've borrowed someones customlist just to get a working example.
Here is one of the fragments i want a clickable list in
public class ActivitiesFragment extends Fragment {
ListView list;
String[] maintitle ={
"Aktivitet 1","Aktivitet 2",
"Aktivitet 3","Aktivitet 4",
"Aktivitet 5",
};
String[] subtitle ={
"A","B",
"C","D",
"E",
};
Integer[] imgid={
R.drawable.ic_dashboard_black_24dp,R.drawable.ic_dashboard_black_24dp,
R.drawable.ic_dashboard_black_24dp,R.drawable.ic_dashboard_black_24dp,
R.drawable.ic_dashboard_black_24dp,
};
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_activities, container, false);
MyListAdapter adapter = new MyListAdapter(getActivity(), maintitle, subtitle,imgid);
list = (ListView) rootView.findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position == 0) {
Toast.makeText(getActivity().getApplicationContext(),"One",Toast.LENGTH_SHORT).show();
}
else if(position == 1) {
Toast.makeText(getActivity().getApplicationContext(),"Two",Toast.LENGTH_SHORT).show();
}
else if(position == 2) {
Toast.makeText(getActivity().getApplicationContext(),"Three",Toast.LENGTH_SHORT).show();
}
else if(position == 3) {
Toast.makeText(getActivity().getApplicationContext(),"Four",Toast.LENGTH_SHORT).show();
}
else if(position == 4) {
Toast.makeText(getActivity().getApplicationContext(),"Five",Toast.LENGTH_SHORT).show();
}
}
});
return rootView;
}
}
Here is the Adapter:
public class MyListAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] maintitle;
private final String[] subtitle;
private final Integer[] imgid;
public MyListAdapter(Activity context, String[] maintitle,String[] subtitle, Integer[] imgid) {
super(context, R.layout.mylist, maintitle);
// TODO Auto-generated constructor stub
this.context=context;
this.maintitle=maintitle;
this.subtitle=subtitle;
this.imgid=imgid;
}
public View getView(int position,View rowView,ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.mylist, null,true);
TextView titleText = (TextView) rowView.findViewById(R.id.title);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
TextView subtitleText = (TextView) rowView.findViewById(R.id.subtitle);
titleText.setText(maintitle[position]);
imageView.setImageResource(imgid[position]);
subtitleText.setText(subtitle[position]);
return rowView;
}
}
The xml for the fragment:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"/>
</RelativeLayout>
The xml for the list:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/icon"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp"/>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#4d4d4d" />
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_marginLeft="10dp" />
</LinearLayout>
</LinearLayout>
Here's whole implementation of RecyclerView with item click listener.
Fragment:
public class MyFragment extends Fragment implements ItemClickListener {
RecyclerView rvList;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_activities, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
rvList = view.findViewById(R.id.rvList);
ArrayList<ItemData> list = new ArrayList<>();
list.add(new ItemData("Aktivitet 1","A",R.drawable.ic_dashboard_black_24dp))
list.add(new ItemData("Aktivitet 2","B",R.drawable.ic_dashboard_black_24dp))
list.add(new ItemData("Aktivitet 3","C",R.drawable.ic_dashboard_black_24dp))
list.add(new ItemData("Aktivitet 4","D",R.drawable.ic_dashboard_black_24dp))
list.add(new ItemData("Aktivitet 5","E",R.drawable.ic_dashboard_black_24dp))
RVAdapter adapter = new RVAdapter(this, list);
rvList.setLayoutManager(new LinearLayoutManager(getContext()));
rvList.setAdapter(adapter);
}
#Override
public void onItemClicked(ItemData data, int position) {
// item click will be listened here
Toast.makeText(getContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
}
}
Fragment Layout: frag.xml
<?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="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/rvList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Layout for the list: item.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="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/icon"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#4d4d4d"
android:textStyle="bold" />
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
RecyclerView ViewHolder:
public class RecyclerVH extends RecyclerView.ViewHolder {
private TextView titleText;
private ImageView imageView;
private TextView subtitleText;
public RecyclerVH(#NonNull View itemView) {
super(itemView);
titleText = itemView.findViewById(R.id.title);
imageView = itemView.findViewById(R.id.icon);
subtitleText = itemView.findViewById(R.id.subtitle);
}
void bind(ItemData data) {
titleText.setText(data.getMainTitle());
imageView.setImageResource(data.getImgId());
subtitleText.setText(data.getSubTitle());
}
}
RecyclerView Adapter:
public class RVAdapter extends RecyclerView.Adapter<RecyclerVH> {
private ArrayList<ItemData> list;
private ItemClickListener listener;
public RVAdapter(ItemClickListener listener, ArrayList<ItemData> list) {
this.list = list;
this.listener = listener;
}
#NonNull
#Override
public RecyclerVH onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new RecyclerVH(LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false));
}
#Override
public void onBindViewHolder(#NonNull final RecyclerVH holder, int position) {
holder.bind(list.get(position));
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onItemClicked(list.get(holder.getAdapterPosition()),
holder.getAdapterPosition());
}
});
}
#Override
public int getItemCount() {
return list.size();
}
}
interface ItemClickListener {
void onItemClicked(ItemData data, int position);
}
Update 1:
Add these lines to the root tag of item layout:
android:focusable="true"
android:clickable="true"
Like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:clickable="true"
android:orientation="horizontal">
...
</LinearLayout>
Time ago I had a similar problem, with an ImageView in my list item. My solution was changing android:focusable to false inside the ImageView block. I never knew why, but it worked fine.
Anyway, I strongly recommend to start using RecyclerView and ViewHolder pattern. https://developer.android.com/guide/topics/ui/layout/recyclerview
It's much more powerful, flexible and a major enhancement over ListView.

Recycler view cardview showing wrap parent instead of match parent

I have passed the parent to onCreateViewHolder instead of null but it is not showing match parent.
Please tell me if there is any error in the code?
Here is my Main_Adapter.class:
public class Main_Adapter extends RecyclerView.Adapter<Main_Adapter.ViewHolder> {
private List<MainModel> mainModels;
private Context context;
private OnClicklisteners listener;
public Main_Adapter(List<MainModel> mainModels, Context context, OnClicklisteners onClicklisteners) {
this.mainModels = mainModels;
this.context = context;
this.listener=onClicklisteners;
}
public interface OnClicklisteners{
public void onPosClicked(View view, int pos, ImageView mainimage, ArrayList<Integer> colorlist);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_card, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.main_text.setText(mainModels.get(position).getName());
Picasso.with(context).load(mainModels.get(position).getImageName()).fit().centerInside().into(holder.mainimage);
}
#Override
public int getItemCount() {
return mainModels.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
#BindView(R.id.main_image)
ImageView mainimage;
#BindView(R.id.main_text)
TextView main_text;
Typeface ubuntu;
int colors;
ArrayList<Integer> colorlist =new ArrayList<>();
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
int pos = getAdapterPosition();
listener.onPosClicked(view,pos,mainimage,colorlist);
}
}
Here is main_card.xml:
<android.support.v7.widget.CardView android:id="#+id/movie_container"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_gravity="center"
android:clickable="true"
android:layout_margin="2dp"
android:foreground="?attr/selectableItemBackground"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="2dp"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<ImageView
android:layout_marginTop="50dp"
android:id="#+id/main_image"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:background="#color/white"
android:scaleType="centerCrop"
/>
<TextView
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/main_text"
android:textSize="16sp"
android:text="hello"
android:textAlignment="center"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
Here is a screenshot:
It should show full width but it's showing only a wrap content. I have found a similar post but they are telling to pass parent in onCreateViewHolder. I have done that but it didn't help me.

Using vector images in RecycleView

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.

Categories

Resources