I have a RecyclerView with CardView the problem is that the click doesn't repsond(unless i press the bottom of the view).I have set the clickable="true"
but it doesn't help
this is my row_item.xml :
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
card_view:cardCornerRadius="5dp"
android:clickable="true"
card_view:cardUseCompatPadding="true" >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:foreground="?selectableItemBackground"
android:clickable="true">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tv_name"
android:gravity="center"
android:padding="10dp"
android:textColor="#color/material_deep_teal_200"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cb_check_box"
android:gravity="right"
android:layout_centerVertical="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="number"
android:clickable="true"
android:id="#+id/tv_number"
android:layout_below="#+id/tv_name"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
and this is how i set the onClick:
// Create new views (invoked by the layout manager)
#Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(getActivity())
.inflate(R.layout.contacts_list_item, parent, false);
// set the view's size, margins, paddings and layout parameters
v.setOnClickListener(this);
CustomViewHolder vh = new CustomViewHolder(v);
return vh;
}
Related
I want to alternate background colors for my items in recycler view for odd and even items. I tried doing it in the onBindViewHolder method of the Adapter class, but it does not work.
Below is the code.
public void onBindViewHolder(#NonNull ReportViewHolder holder, int position) {
ReportItem currentItem = reportlist.get(position);
if(position%2==0){
holder.itemView.setBackgroundColor(mContext.getResources().getColor(R.color.teal));
} else {
holder.itemView.setBackgroundColor(mContext.getResources().getColor(R.color.lightteal));
}
holder.departureDate.setText((currentItem.getDepartureDate()));
holder.flightNumber.setText(currentItem.getFlightNumber());
Below is the xml code for the holder layout.
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="0dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:background="#color/teal"
android:layout_marginBottom="0dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
Try to use holder.relativeLayout instead of holder.itemView
I am trying to inflate the second XML inside cardview dynamically, but it throws null pointer exception.When I am trying to do hard code(by including directly inside card view layout) it works well.Is there any other way to inflate new XML dynamically?
CardView Layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/room_detail_cards"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/room_detail_Linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/module_type_5"/>
<!--<include layout="#layout/module_type_3"/>-->
<!--<It Works well when i hard code the layout here/>-->
</LinearLayout>
</android.support.v7.widget.CardView>
RecyclerAdapter.java(onCreateViewHolder)
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
layout= (LinearLayout) parent.findViewById(R.id.room_detail_Linear_layout);
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.module_type_3, layout, false);
layout.addView(itemView); //NULL Pointer Exception occurs here
View itemView1 = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.room_detail_card_view, null, false);
return new ViewHolder(itemView1);
}
First layout XML(module_type_5)
<?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">
<TextView
android:hint="module"
android:id="#+id/module_5"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/add"
android:id="#+id/switch_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#drawable/add"
android:id="#+id/switch_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#drawable/add"
android:id="#+id/switch_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#drawable/add"
android:id="#+id/switch_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#drawable/add"
android:id="#+id/switch_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Second XML Layout(module_type_3)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:hint="module"
android:id="#+id/module_5"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/add"
android:id="#+id/switch_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#drawable/add"
android:id="#+id/switch_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#drawable/add"
android:id="#+id/switch_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
You are trying to get view id before inflating it at here:
layout= (LinearLayout) parent.findViewById(R.id.room_detail_Linear_layout);
That's why layout is null.
Use onCreateViewHolder to return an instance of ViewHolder and perform all assignments in the view holder.
Something like this would work:
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.room_detail_card_view, parent, false));
}
And then in your ViewHolder class:
public class ViewHolder extends RecyclerView.ViewHolder{
TextView textView;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView)itemView.findViewById(R.id.textView);
}
}
And finally data binding in onBindViewHolder:
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.textView.setText("some data")
}
I have 2 views in a recyclerview
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
if (viewType == TYPE_IMAGE) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);
// set the view's size, margins, paddings and layout parameters
ImageViewHolder vh = new ImageViewHolder(v);
return vh;
} else {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.childlayout, parent, false);
// set the view's size, margins, paddings and layout parameters
TextViewHolder vh = new TextViewHolder(v);
return vh;
}
}
my problem is withe layout design , I want the rowlayout to be above childlayout how to do that ?
this is main activity
setContentView(R.layout.activity_recycler_view);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
// LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
mLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new MyAdapter(getDataSet());
mRecyclerView.setAdapter(mAdapter);
RecyclerView.ItemDecoration itemDecoration =
new DividerItemDecoration(this, LinearLayoutManager.VERTICAL);
mRecyclerView.addItemDecoration(itemDecoration);
this is the UPDATED layouts
rowlayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="6dp">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="TODO"
android:src="#drawable/error" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/firstLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Example application"
android:textSize="16sp" />
<TextView
android:id="#+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
childlayout
<ImageView
android:id="#+id/header_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/success" />
<TextView
android:id="#+id/textc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Gig"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
With the recent layout I have the below wrong results
I want to have such result , please notice I am trying to add a title 'today' above the view
notice that there are scroller view horizontal
Hope this could help:
rowlayouts.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="6dp">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dp"
android:contentDescription="TODO"
android:src="#drawable/logo" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="#+id/firstLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="Example application"
android:textSize="16sp" />
<TextView
android:id="#+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:ellipsize="marquee"
android:gravity="center_vertical"
android:singleLine="true"
android:text="Description"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
childlayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<ImageView
android:id="#+id/header_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/logo" />
<TextView
android:id="#+id/textc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:gravity="center_horizontal"
android:text="Android Gig"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
</LinearLayout>
In your recyclerview adapter you can override a method called getItemViewType().
Notice that one off the parameters for onCreateViewHolder() is "int viewType". This viewType is got by the getItemViewType() method. Here you can write your logic to return a certain number for the type of view you want.
Eg: -
#Override
public int getItemViewType(int position) {
// you can return whatever integers you want
//your logic goes here
// Just as an example, return 0 or 1 depending on position.
return position % 2 ;
}`
I wasn't sure what you meant by - "I want the rowlayout to be above childlayout". You can maybe return 0 for all views of type RowLayout and 1 for ChildLayout.
I want use the card flip effect (http://developer.android.com/intl/es/training/animation/cardflip.html) in each item of a recyclerview.
I have the 3 views.
item_cancion:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
item_cancion_front and item_cancion_back (Two xml with the same info for a proof)
<?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:padding="5dp"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgReproduciendo"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/txtArtista"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:maxWidth="30dp"
android:layout_marginRight="10dp" />
<TextView
android:id="#+id/txtTitulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:singleLine="true"
android:layout_toLeftOf="#+id/txtDuracion"
android:layout_toRightOf="#+id/imgReproduciendo"
android:layout_toEndOf="#+id/imgReproduciendo"
android:text="Titulo" />
<TextView
android:id="#+id/txtArtista"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"
android:singleLine="true"
android:maxWidth="190dp"
android:layout_below="#+id/txtTitulo"
android:layout_toRightOf="#+id/imgReproduciendo"
android:layout_toEndOf="#+id/imgReproduciendo"
android:text="Artista" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtAlbum"
android:singleLine="true"
android:textSize="14dp"
android:gravity="right"
android:layout_below="#+id/txtDuracion"
android:layout_alignRight="#+id/txtDuracion"
android:layout_alignEnd="#+id/txtDuracion"
android:layout_toRightOf="#+id/txtArtista"
android:layout_marginLeft="15dp"
android:text="Album" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtDuracion"
android:paddingLeft="25dp"
android:singleLine="true"
android:textSize="14dp"
android:textStyle="italic"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="Duracion" />
</RelativeLayout>
In the viewHolder adapter I have:
#Override
public AdaptadorCancionesActual.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cancion, parent, false);
actividad.getFragmentManager().beginTransaction().add(R.id.container, new CardFrontFragment()).commit();
ViewHolder vh = new ViewHolder(v, this);
return vh;
}
and the ViewHolder is:
public ViewHolder(View v, AdaptadorCancionesActual mAdapter) {
super(v);
this.mAdapter = mAdapter;
frameLayout = (RelativeLayout) v.findViewById(R.id.container);
txtCancion = (TextView) frameLayout.findViewById(R.id.txtTitulo);
txtArtista = (TextView) frameLayout.findViewById(R.id.txtArtista);
txtAlbum = (TextView) frameLayout.findViewById(R.id.txtAlbum);
txtFin = (TextView) frameLayout.findViewById(R.id.txtDuracion);
imgReproduciendo = (ImageView) frameLayout.findViewById(R.id.imgReproduciendo);
v.setOnClickListener(this);
}
#Override
public void onClick(View view) {
int position = getLayoutPosition();
Datos.getMusicSrv().setSong(position);
Datos.getMusicSrv().playSong();
mAdapter.flipCard();
//mAdapter.notifyDataSetChanged();
}
I have two question...
First, I can use this effect in the item of recyclerView?
And if Yes... Why the TextViews are null.
What I'm trying to do is to assign a different color to every other row in a RecycledView. I created a custom RecycledView.Adapter, and in onCreateViewHolder method I have this:
// Create a row_folder view
View view = inflater.inflate(R.layout.row_folder, parent, false);
if(position % 2 == 0) {
view.setBackgroundColor(Color.BLACK);
}
// Use MyViewHolder to bind the view
MyViewHolder holder = new MyViewHolder(view);
return holder;
The color does not get assigned. What am I missing ? Thanks.
edited:
row_folder 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="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="72dp"
android:background="#color/my_white"
android:orientation="horizontal">
<ImageView
android:id="#+id/folder_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:src="#drawable/folder_icon" />
<TextView
android:id="#+id/folder_name"
android:layout_width="190dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:text="The Mills"
android:textColor="#color/my_blue"
android:textSize="16sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/folder_content_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:layout_marginLeft="12dp"
android:src="#drawable/folder_content_icon" />
<TextView
android:id="#+id/content_number"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:gravity="left"
android:text="3"
android:textColor="#color/my_blue"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
do something like this in your recycler view adapter:
#Override
public void onBindViewHolder(ViewHolder holder, int position)
{
if(position % 2 == 0)
{
//holder.rootView.setBackgroundColor(Color.BLACK);
holder.rootView.setBackgroundResource(R.color.black);
}
else
{
//holder.rootView.setBackgroundColor(Color.WHITE);
holder.rootView.setBackgroundResource(R.color.white);
}
}
EDIT:
Change your xml file like this: row_folder.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rootView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="72dp"
android:background="#color/my_white"
android:orientation="horizontal">
<ImageView
android:id="#+id/folder_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:src="#drawable/folder_icon" />
<TextView
android:id="#+id/folder_name"
android:layout_width="190dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:text="The Mills"
android:textColor="#color/my_blue"
android:textSize="16sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/folder_content_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:layout_marginLeft="12dp"
android:src="#drawable/folder_content_icon" />
<TextView
android:id="#+id/content_number"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:gravity="left"
android:text="3"
android:textColor="#color/my_blue"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
create one more variable in your ViewHolder class as below:
public static class ViewHolder extends RecyclerView.ViewHolder
{
LinearLayout rootView;//newly added field
public ViewHolder(View view)
{
super(view);
rootView=(LinearLayout)view.findViewById(R.id.rootView);
}
}
I hope it might work.
I'm putting this here just for future reference for those trying to apply a zebra striping like color effect on RecyclerView's row which basically holds a CardView.
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<!--Content goes here-->
</android.support.v7.widget.CardView>
Now, in adapter side, initialize the CardView first (I'm using butterknife binding).
public class YooViewHolder extends RecyclerView.ViewHolder {
#BindView(R.id.cv)
CardView cardView;
public YooViewHolder(View v) {
super(v);
ButterKnife.bind(this, v);
}
}
And finally at onBindViewHolder, place this single line of code.
holder.cardView.setCardBackgroundColor(
ContextCompat.getColor(mContext,
position % 2 == 0 ? R.color.white : R.color.white_grayish));
Be sure to use setCardBackgroundColor (and not setBackgroundColor) on CardView.