I'm having a problem with adding CardView to my Tab content, So here's the full story :
1.Created sliding tabs, with 3 tabs .
2.Made a different layout for each tab ( tab_1, tab_2, tab_3 .
3.Set an adapter ..etc for them, so the tabs are working great ! .
Now, i want to add a different CardView for each tab .
So, i did the following :
1.Made an item layout, which contain the following ( card view + TextView + Image ) .
2. Made a RecyclerView layout .
3.Made "Person" class, so set the info .
4.Made an adapter for the "item" class .
So, What's the problem ?
The problem is i can't set the content for the tab, it's showing an empty content, I want to display the items in Tab_1 .
-Forgot to mention, I've made a class for each tab, and it contains the following :
public class tab_1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab_1, container, false);
return v;
}
}
The Codes :
Person.java :
class Person {
String name;
String age;
int photoId;
Person(String name, String age, int photoId) {
this.name = name;
this.photoId = photoId;
}
}
Adapter.java :
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {
public static class PersonViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView personName;
ImageView personPhoto;
PersonViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
personName = (TextView)itemView.findViewById(R.id.person_name);
personPhoto = (ImageView)itemView.findViewById(R.id.person_photo);
}
}
List<Person> persons;
RVAdapter(List<Person> persons){
this.persons = persons;
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview_activity, viewGroup, false);
PersonViewHolder pvh = new PersonViewHolder(v);
return pvh;
}
#Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
personViewHolder.personName.setText(persons.get(i).name);
personViewHolder.personPhoto.setImageResource(persons.get(i).photoId);
}
#Override
public int getItemCount() {
return persons.size();
}
}
Tab_1.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:padding="16dp"
>
<include layout="#layout/item" />
</LinearLayout>
item.xml :
<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="match_parent"
android:layout_height="wrap_content"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/person_photo"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
android:src="#drawable/facebook_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/person_name"
android:text="Title"
android:textSize="20sp"
android:layout_toRightOf="#+id/person_photo"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
RecyclerView.xml :
I've tried to delete item.xml, and put the content in Tab_1.xml, and the tab_1.xml is like this :
<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="match_parent"
android:layout_height="wrap_content"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/person_photo"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
android:src="#drawable/facebook_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/person_name"
android:text="Title"
android:textSize="20sp"
android:layout_toRightOf="#+id/person_photo"/>
</RelativeLayout>
I've fixed the issue, it was an understanding problem from me about Recycler & Card Views .
Just made the files again and setup everything, now it's working like a charm!.
*Check slidenerd for how to setup if you want a tutorial .
Related
My rows are not in the same size for some reason.I tried everything but it didn't work.By the way I'm very new to android.
I tried changing my row.xml but it did not work:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="3dp"
app:cardElevation="3dp"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_gravity="top">
<ImageView
android:id="#+id/imageIv"
android:layout_width="35dp"
android:layout_height="34dp"
android:background="#drawable/circulo_azul"
android:src="#drawable/transferenciablue" />
<TextView
android:id="#+id/titleTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_toEndOf="#id/imageIv"
android:layout_toRightOf="#id/imageIv"
android:text="Title"
android:textColor="#000"
android:textSize="22sp"
android:textStyle="bold" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
This is the activitymain.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation = "vertical"
tools:context=".MainActivity"
>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:id="#+id/recyclerView"/>
</LinearLayout>
MyAdapter.java with onBindViewHolder:
package com.example.demoapp;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import android.content.Context;
public class MyAdapter extends RecyclerView.Adapter<MyHolder> {
Context c;
ArrayList<Model> models; // this array list creates a list of arrays which parameters define in my model class
public MyAdapter(Context c, ArrayList<Model> models) {
this.c = c;
this.models = models;
}
#NonNull
#Override
public MyHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row,null); // this line inflate my row
return new MyHolder(view); // this will return my view to holder class
}
#Override
public void onBindViewHolder(#NonNull MyHolder myHolder, int i) {
myHolder.mTitle.setText(models.get(i).getTitle()); // here i is position
myHolder.mImaeView.setImageResource(models.get(i).getImg());
}
#Override
public int getItemCount() {
return models.size();
}
}
MyHolder class:
package com.example.demoapp;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class MyHolder extends RecyclerView.ViewHolder {
ImageView mImaeView;
TextView mTitle;
public MyHolder(#NonNull View itemView) {
super(itemView);
this.mImaeView = itemView.findViewById(R.id.imageIv);
this.mTitle = itemView.findViewById(R.id.titleTv);
}
}
What do I need to change to make them appear proportional?Thanks
make sure that you will write this type of code in BindViewHolder:
#Override
public folder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.your_layout_name,parent,false);
return new viewHolderClass(view);
}
some time width cannot appear properly because viewGroup cannot use
Change android:layout_gravity="top" to android:layout_gravity="center"
I have recyclerview and it shows just single records instead of two records. I have searched many post on google and found to modify height of listview items from "match_parent " To "Wrap_Content" hence I did the same but still it shows single records.
Activity Mumbai_Male
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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_width="match_parent"
android:layout_height="match_parent"
tools:context=".MumbaiMale">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/myImg"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/age"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/education"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:scrollbars="vertical"
android:id="#+id/recyclerView" />
</LinearLayout>
CustomeAdapter is:
package com.maheshwaghela.mahesh.rukhivivah;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAdapter extends
RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private MumbaiMale.IconData[] data;
public CustomAdapter (MumbaiMale.IconData[] data) {
this.data = data;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View listItem= layoutInflater.inflate(R.layout.activity_mumbai_male, parent, false);
ViewHolder viewHolder = new ViewHolder(listItem);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.name.setText(data[position].getPname());
holder.age.setText(data[position].getPage());
holder.education.setText(data[position].getPedu());
holder.imageView.setImageResource(data[position].getImgId());
}
#Override
public int getItemCount() {
return data.length;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView name;
public TextView age; public TextView education;
public ViewHolder(View itemView) {
super(itemView);
this.imageView = (ImageView) itemView.findViewById(R.id.myImg);
this.name = (TextView) itemView.findViewById(R.id.name);
this.age=(TextView) itemView.findViewById(R.id.age);
this.education=(TextView) itemView.findViewById(R.id.education);
}
}
}
Mumbai_Male.Java
package com.maheshwaghela.mahesh.rukhivivah;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
public class MumbaiMale extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mumbai_male);
IconData[] data = new IconData[] {
new IconData("Name:- Mahesh K. Waghela","Age:-45","Education:-B.com", R.drawable.dilip333),
new IconData ("Name:- Sunil K. Waghela","Age:-33","Education:-S.S.C.",R.drawable.hiteshhh)
};
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
CustomAdapter adapter=new CustomAdapter(data);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
public class IconData {
private String pname;
private String page;
private String pedu;
private int imgId;
public IconData(String name, String age, String edu, int imgId) {
this.pname = name;
this.page= age;
this.pedu= edu;
this.imgId = imgId;
}
public String getPname()
{
return pname;
}
public void setPname(String name)
{
this.pname = name;
}
public String getPage()
{
return page;
}
public void setPage(String age)
{
this.page=age;
}
public String getPedu()
{
return pedu;
}
public void setMyPedu(String edu)
{
this.pedu=edu;
}
public int getImgId()
{
return imgId;
}
public void setImgId(int imgId)
{
this.imgId = imgId;
}
}
}
Where I am wrong don't know but it shows single records instead of two records.
As per your current code, your RecyclerView will display both record for both wrap_content and match_parent. You can see second record if you will scroll your list.
Also You are getting this type of wired output because your are using the same layout as a Activity Layout and Adapter's item layout. So adapter will bind image, name etc.. but it will also display a blank RecyclerView for each row.
So the solution is just keep your RecyclerView inside activity_mumbai_male.xml and create new xml file for your RecyclerView item and inflate that layout xml file inside your adapter. Your issue will be fixed.
Please check below corrected code.
activity_mumbai_male.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MumbaiMale">
<LinearLayout
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:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:scrollbars="vertical" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
item_list.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:orientation="horizontal">
<ImageView
android:id="#+id/myImg"
android:layout_width="90dp"
android:layout_height="90dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/education"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Replace your code with my below code in CustomAdapter.xml
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View listItem = layoutInflater.inflate(R.layout.item_list, parent, false);
ViewHolder viewHolder = new ViewHolder(listItem);
return viewHolder;
}
You are using RecyclerView, but I could not found your xml regarding Row, you have set text to the Simple Single TextView Which you have provide in your activity_mumbai.xml, To you recyclerview you need to create another xml, which is counted as a row, means single item of your RecyclerView, and Print your xml data in that view.
row_item_view.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/myImg"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/age"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/education"/>
</LinearLayout>
</LinearLayout>
and Change SetContentView in your custom adapter with this new xml.
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View listItem= layoutInflater.inflate(R.layout.row_item_view, parent, false);
ViewHolder viewHolder = new ViewHolder(listItem);
return viewHolder;
}
This will fix your problem.
I am facing an issue the problem is I can see the Fragment but not the
GridView which I have there created, in the LogCat no error nothing but somehow the code it is not working.
But if I remove GridView from bookmark.xml and only leave a TextView than I can show the TextView
I am trying to reach a GridView into a Fragment.
This is my code.
Fragment
public class FragmentBookmark extends Fragment {
View paramView;
public FragmentBookmark() {
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
paramView = inflater.inflate(R.layout.bookmark, container, false);
return paramView;
}
}
This is the Main Activity
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.AddFragment(new FragmentExplore(), "");
adapter.AddFragment(new FragmentBookmark(), "");
adapter.AddFragment(new FragmentStore(), "");
mViewPager.setAdapter(adapter);
mTabLayout.setupWithViewPager(mViewPager);
This is the xml for Bookmark Fragment
<?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"
android:gravity="center">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>
Try like this using framelayout
<FrameLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<GridView
android:id = "#+id/gvList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="spacingWidth"
android:numColumns="2"/>
</FrameLayout>
You need to set adapter to your GridView as only GridView will never show anything as it is a simple ViewGroup.. Also rather than using GridView try to use RecyclerView in grid mode. Below is a simple tutorial you can follow-
RecyclerView as GridView with GridLayoutManager
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="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/m_recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp" />
</LinearLayout>
grid_item.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/imageview1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:contentDescription="#null" />
<TextView
android:id="#+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLines="1"
android:singleLine="true"
tools:text="Category" />
</LinearLayout>
</android.support.v7.widget.CardView>
Adapter:
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private Context mContext;
private List<String> myNameList;
public MyAdapter(Context context, List<String> categoryInfo) {
mContext = context;
myNameList = categoryInfo;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_item, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, final int position) {
String myName = myNameList.get(position);
holder.tvName.setText(myName);
}
#Override
public int getItemCount() {
return myNameList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
private TextView tvName;
MyViewHolder(View itemView) {
super(itemView);
tvName = itemView.findViewById(R.id.tv_name);
}
}
}
In your Activity or Fragment:
Context mContext = this;
RecyclerView mRecyclerView;
mRecyclerView = findViewById(R.id.m_recyclerView);
mRecyclerView.setLayoutManager(new android.support.v7.widget.GridLayoutManager(mContext, 3));
ArrayList<String> nameArray = new ArrayList<>();
nameArray.add("name 1");
nameArray.add("name 2");
nameArray.add("name 3");
nameArray.add("name 4");
mRecyclerView.setAdapter(new MyAdapter(mContext, nameArray));
I got problems on the recycler view alignment. Each time I'm creating a recycler view list, the alignment for horizontal or vertical is not really straight.
Based on the red line I've marked, showing the inconsistency of my CardView # Layout view alignment.
My expected outcome is that recycler view list is in a straight line, either vertical or horizontal.
My current display output:
My current codes for the recycler view list.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/dialog_holo_light_frame"
android:padding="4dp">
<android.support.v7.widget.CardView
android:background="?attr/selectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<com.github.lzyzsd.circleprogress.DonutProgress
android:id="#+id/donut_progress"
custom:donut_progress="30"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_weight="1" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="40"
android:orientation="vertical"
android:paddingLeft="10dp">
<TextView
android:id="#+id/chapterNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chapter 1"
android:textSize="18sp" />
<TextView
android:id="#+id/chapterTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Standard Form"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
MainActivity.java
adapterListChapters = new AdapterChapterList(chaptersList);
recyclerViewChapters = findViewById(R.id.recyclerViewChapterView);
recyclerViewChapters.setHasFixedSize(true);
recyclerViewChapters.setNestedScrollingEnabled(false);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerViewChapters.addItemDecoration(new SpacingItemDecoration(2, Tools.dpToPx(this, 8), true));
recyclerViewChapters.setLayoutManager(layoutManager);
recyclerViewChapters.setItemAnimator(new DefaultItemAnimator());
recyclerViewChapters.setAdapter(adapterListChapters);
adapterListChapters.setOnClickListener(new AdapterChapterList.OnClickListener() {
#Override
public void onItemClick(View view, ChapterList obj, int pos) {
final Snackbar snackbar = Snackbar.make(parent_view_chapter, "Item " + obj.chapterTitle + " clicked", Snackbar.LENGTH_SHORT);
snackbar.show();
}
});
AdapterChapterList.java
package xxx.xxxx.components.adapter;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.github.lzyzsd.circleprogress.DonutProgress;
import com.material.components.R;
import com.material.components.model.ChapterList;
import java.util.ArrayList;
import java.util.List;
public class AdapterChapterList extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public List<ChapterList> chapterLists = new ArrayList<>();
private OnClickListener onClickListener = null;
public AdapterChapterList(List<ChapterList> chapterLists) {
this.chapterLists = chapterLists;
}
public class OriginalViewHolder extends RecyclerView.ViewHolder{
public View lyt_parent_chapter_list;
public TextView chapterNumber,chapterTitle;
public DonutProgress learningProgress;
public OriginalViewHolder(View itemView) {
super(itemView);
chapterNumber = itemView.findViewById(R.id.chapterNumber);
chapterTitle = itemView.findViewById(R.id.chapterTitle);
lyt_parent_chapter_list = itemView.findViewById(R.id.lyt_parent_chapter_list);
learningProgress = itemView.findViewById(R.id.donut_progress);
learningProgress.setTextSize(40);
}
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder vh;
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chapter_list,parent,false);
vh = new OriginalViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if(holder instanceof OriginalViewHolder)
{
OriginalViewHolder view = (OriginalViewHolder) holder;
final ChapterList c = chapterLists.get(position);
view.chapterNumber.setText(c.chapterNumber);
view.chapterTitle.setText(c.chapterTitle);
view.learningProgress.setDonut_progress(String.valueOf(c.learningProgress));
view.lyt_parent_chapter_list.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(onClickListener == null) return;
onClickListener.onItemClick(v,c,position);
}
});
}
}
#Override
public int getItemCount() {
return chapterLists.size();
}
public void setOnClickListener(OnClickListener onClickListener) {
this.onClickListener = onClickListener;
}
public interface OnClickListener {
void onItemClick(View view, ChapterList obj, int pos);
}
}
Thanks!
In Recycler view listItem
1.Using layout_weight requires layout_width to be 0dp t function properly
Here Is Updated XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/dialog_holo_light_frame"
android:padding="4dp">
<android.support.v7.widget.CardView
android:background="?attr/selectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<com.github.lzyzsd.circleprogress.DonutProgress
android:id="#+id/donut_progress"
custom:donut_progress="30"
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_weight="1" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="10"
android:orientation="vertical"
android:paddingLeft="10dp">
<TextView
android:id="#+id/chapterNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Chapter 1"
android:textSize="18sp" />
<TextView
android:id="#+id/chapterTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Standard Form"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
In case If Above Layout Doesn't Solve Your Problem Try Removing this line of code
recyclerViewChapters.addItemDecoration(new SpacingItemDecoration(2, Tools.dpToPx(this, 8), true));
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingTop="#dimen/activity_horizontal_margin"
android:background="#android:color/transparent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/string_for_newsletter"
android:paddingLeft="16dp"
android:id="#+id/textQuestion"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bookmark_star"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:layout_alignParentBottom="true"
android:id="#+id/bookmarkButton"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textQuestion"
android:text="answer"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:textColor="#00ff00"
android:visibility="gone"
android:id="#+id/textAnswer"/>
</RelativeLayout>
this is my layout for card view
and below is the code for RecyclerView Adapter
package com.example.user_2.tcc_app.QuestionAnswer;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import com.example.user_2.tcc_app.R;
/**
* Created by USER-2 on 23-Feb-15.
*/
public class QuestionAnswerAdapter extends RecyclerView.Adapter<QuestionAnswerAdapter.QuestionAnswerAdapterViewHolder>{
int length;
int layout_id;
public long item_id;
public QuestionAnswerAdapter(int length, int id_for_layout){
this.length = length;
this.layout_id = id_for_layout;
}
#Override
public QuestionAnswerAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View root = LayoutInflater.from(parent.getContext()).inflate(layout_id, parent, false);
QuestionAnswerAdapterViewHolder questionAnswerAdapterViewHolder = new QuestionAnswerAdapterViewHolder(root);
return questionAnswerAdapterViewHolder;
}
#Override
public void onBindViewHolder(final QuestionAnswerAdapterViewHolder holder, int position) {
}
#Override
public int getItemCount() {
return length;
}
public class QuestionAnswerAdapterViewHolder extends RecyclerView.ViewHolder{
TextView textQuestion;
ImageButton bookmarkButton;
TextView textAnswer;
public QuestionAnswerAdapterViewHolder(View v){
super(v);
textQuestion = (TextView)v.findViewById(R.id.textQuestion);
bookmarkButton = (ImageButton)v.findViewById(R.id.bookmarkButton);
textAnswer = (TextView)v.findViewById(R.id.textAnswer);
textQuestion.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textAnswer.setVisibility(View.GONE);
}
});
}
}
}
I have 10 Items in the recyclerview. I want to show the textview with id textAnswer when textQuestion is clicked and every thing is working fine till now but when i scroll down i can see that ninth item also has the textAnswer field visible. I can understand that this is happening due to onBindViewHolder Method as recyclerView while recycling its item uses previously visible items which are not visible any more. But i have no clue on how to sort it out. Some one please help
The RecyclerView re-uses the ViewHolders so the trick is to set the expandable area to VISIBLE/GONE inside the onBindViewHolder() method. Hopefully you solved your problem by now but for those who run into similar issues like me, a functional example:
An example of an adapter with expendable cardViews:
public class FooAdapter extends RecyclerView.Adapter<FooAdapter.ViewHolder> {
List<LightGroup> mFoos;
List<Boolean> mExpandedFoos;
public GroupAdapter(List<Foo> Foos) {
mFoos = foos;
mExpandedFoos = new ArrayList<>(mFoos.size());
for(int i = 0; i < mGroups.size(); i++){
mExpandedFoos.add(false);
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.foo_row, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
if(mExpandedFoos.get(position)) {
holder.expandableLayout.setVisibility(View.VISIBLE);
} else {
holder.expandableLayout.setVisibility(View.GONE);
}
Foo foo = mFoos.get(position);
holder.fooName.setText(foo.getName());
//Setup the rest of your row views from your Foo object
}
#Override
public int getItemCount() {
return mFoos == null ? 0 : mFoos.size();
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView fooName;
ViewGroup expandableLayout;
ImageButton expandButton;
public ViewHolder(View itemView) {
super(itemView);
fooName = (TextView) itemView.findViewById(R.id.foo_name_textview);
expandableLayout = (ViewGroup) itemView.findViewById(R.id.expandable_part_layout);
expandButton = (ImageButton) itemView.findViewById(R.id.foo_row_expand_button);
expandButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int position = getAdapterPosition();
if(v.getId() == expandButton.getId()){
if(mExpandedFoos.get(position)) {
expandableLayout.setVisibility(View.GONE);
} else {
expandableLayout.setVisibility(View.VISIBLE);
}
mExpandedFoos.set(position, !mExpandedFoos.get(position));
}
}
}
}
With layout:
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="4dp"
app:cardPreventCornerOverlap="false"
android:background="#android:color/background_dark"
android:layout_marginBottom="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/top_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="false"
android:layout_alignParentTop="true">
<TextView
android:id="#+id/foo_name_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="#dimen/activity_horizontal_margin"
android:paddingEnd="#dimen/activity_horizontal_margin"
android:gravity="center"/>
<ImageButton
android:id="#+id/foo_row_expand_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="?android:selectableItemBackground"
android:clickable="true"
android:padding="6dp"
android:rotation="90"
android:src="#drawable/ic_play_light" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/expandable_part_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/top_layout"
android:animateLayoutChanges="true"
android:clickable="false"
android:visibility="gone">
<!-- Your expanded content views go here -->
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>