Single Row Shows Insight RecyclerView - android

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.

Related

I am not able to see the grid view in my Bookmark which extends Fragment

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));

Scrolling RecyclerView inside CardView is very slow

I have 4 CardViews in a Activity and each CardView has a RecyclerView. Each RecyclerView has around 8-10 rows/item. Scrolling of this page/activity is very slow.
I suspect some problem in my RecyclerView.Adapter class.
Can somebody please let me know what could be the issue in my code?
Adapter:
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class JyotishAppAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<DataModel> dataList;
private int cardView;
public static final int CARD_2_COL = 0;
public static final int CARD_3_COL_HEAD = 1;
public static final int CARD_2_COL2 = 2;
public static class GenericViewHolder extends RecyclerView.ViewHolder {
TextView itemName;
TextView itemDescription;
public GenericViewHolder (View itemView) {
super (itemView);
this.itemName = (TextView) itemView.findViewById(R.id.item_name);
this.itemDescription = (TextView) itemView.findViewById(R.id.item_description);
}
}
public static class GenericViewHolder2 extends RecyclerView.ViewHolder {
TextView itemName;
TextView itemDescription;
TextView itemStatus;
public GenericViewHolder2 (View itemView) {
super (itemView);
this.itemName = (TextView) itemView.findViewById(R.id.item_name);
this.itemDescription = (TextView) itemView.findViewById(R.id.item_description);
this.itemStatus = (TextView) itemView.findViewById(R.id.item_status);
}
}
public JyotishAppAdapter(ArrayList<DataModel> dataList, int cardView) {
this.dataList = dataList;
this.cardView = cardView;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v;
if (cardView == CARD_2_COL) {
v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.list_item, viewGroup, false);
return new GenericViewHolder(v);
} else if (cardView == CARD_2_COL2) {
v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.list_item3, viewGroup, false);
return new GenericViewHolder(v);
} else {
v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.list_item2, viewGroup, false);
return new GenericViewHolder2(v);
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
final DataModel dataModel = dataList.get(position);
if (viewHolder instanceof GenericViewHolder) {
GenericViewHolder holder = (GenericViewHolder) viewHolder;
holder.itemName.setText(Html.fromHtml(dataModel.getValue1()));
holder.itemDescription.setText(Html.fromHtml(dataModel.getValue2()));
if (dataModel.getValue4() != null) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Utility.showAlertMessage(view.getContext(), R.string.message_title,
Html.fromHtml(dataModel.getValue4()).toString());
}
});
}
} else {
GenericViewHolder2 holder = (GenericViewHolder2) viewHolder;
holder.itemName.setText(Html.fromHtml(dataModel.getValue1()));
holder.itemDescription.setText(Html.fromHtml(dataModel.getValue2()));
holder.itemStatus.setText(Html.fromHtml(dataModel.getValue3()));
if (dataModel.getValue4() != null) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Utility.showAlertMessage(view.getContext(), R.string.message_title,
Html.fromHtml(dataModel.getValue4()).toString());
}
});
}
}
}
#Override
public int getItemCount() {
return dataList.size();
}
}
Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/PageContentBackground"
tools:context=".PanchangaActivity">
<include android:id="#+id/tool_bar"
layout="#layout/tool_bar" />
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tool_bar"
android:orientation="vertical"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:background="#color/PageContentBackground">
<TextView android:id="#+id/muhurta_zone"
style="#style/TextStyle"
android:textSize="#dimen/text_size_horo_data"
android:ellipsize="none"
android:scrollHorizontally="false"
android:layout_marginBottom="5dp"/>
<TextView android:id="#+id/muhurta_place"
style="#style/TextStyle"
android:textSize="#dimen/text_size_horo_data"
android:ellipsize="none"
android:scrollHorizontally="false"
android:layout_marginBottom="10dp"/>
<ScrollView android:layout_height="wrap_content"
android:layout_width="match_parent">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/cardview4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
card_view:cardElevation="5dp"
card_view:cardCornerRadius="5dp"
card_view:cardUseCompatPadding="true"
card_view:cardBackgroundColor="#color/CardViewColor">
<android.support.v7.widget.RecyclerView
android:id="#+id/panchanga_result_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/CardViewColor"
android:paddingLeft="3dp"
android:paddingRight="3dp" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/cardview5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/cardview4"
android:layout_marginTop="10dp"
card_view:cardElevation="5dp"
card_view:cardCornerRadius="5dp"
card_view:cardUseCompatPadding="true"
card_view:cardBackgroundColor="#color/CardViewColor">
<android.support.v7.widget.RecyclerView
android:id="#+id/panchanga_result_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/CardViewColor"
android:paddingLeft="3dp"
android:paddingRight="3dp" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/cardview6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/cardview5"
android:layout_marginTop="10dp"
card_view:cardElevation="5dp"
card_view:cardCornerRadius="5dp"
card_view:cardUseCompatPadding="true"
card_view:cardBackgroundColor="#color/CardViewColor">
<android.support.v7.widget.RecyclerView
android:id="#+id/panchanga_result_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/CardViewColor"
android:paddingLeft="3dp"
android:paddingRight="3dp" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/cardview7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/cardview6"
android:layout_marginTop="10dp"
card_view:cardElevation="5dp"
card_view:cardCornerRadius="5dp"
card_view:cardUseCompatPadding="true"
card_view:cardBackgroundColor="#color/CardViewColor">
<android.support.v7.widget.RecyclerView
android:id="#+id/panchanga_result_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/CardViewColor"
android:paddingLeft="3dp"
android:paddingRight="3dp" />
</android.support.v7.widget.CardView>
</RelativeLayout>
</ScrollView>
</LinearLayout>
</RelativeLayout>
The problem is solved by setting setNestedScrollingEnabled to false.
recyclerView1.setNestedScrollingEnabled(false);
It is better to use NestedScrollView and set setNestedScrollingEnabled to false for better performance.

Adding CardView & Items to SlidingTabs, Multi-questions

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 .

RecyclerView strange behaviour inflating cells

I have this layout in two apps, one inside a RecyclerView and the other in a root activity layout.
Here the layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="vertical"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:background="#CFCFCF"
android:minHeight="250dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_red_dark"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_bright"
android:layout_above="#+id/commerceTextView"
>
<ImageView
android:src="#drawable/imagen"
android:id="#+id/commerceImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/commerceTextView"
android:gravity="center"
android:textStyle="bold"
android:textSize="24sp"
android:textColor="#F1F1F1"
android:background="#color/colorPrimary"
android:layout_alignParentBottom="true"
android:paddingTop="10dp"
android:text="Best food ever"
android:paddingBottom="10dp"/>
</RelativeLayout>
</FrameLayout>
Here the adapter
public class CommercesAdapter extends RecyclerView.Adapter<CommercesAdapter.CommercesViewHolder> {
private final Context context;
private final ImageLoader loader;
private List<CommerceEntity> commercesList;
#Inject
public CommercesAdapter(Context context, ImageLoader loader) {
this.context = context;
this.loader = loader;
}
public void setData(List<CommerceEntity> commercesList) {
this.commercesList = commercesList;
}
#Override
public CommercesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context)
.inflate(R.layout.list_comerces_item, parent, false);
return new CommercesViewHolder(view);
}
#Override
public void onBindViewHolder(CommercesViewHolder holder, int position) {
CommerceEntity commerce = commercesList.get(position);
String imageUri = commerce.getImageUri();
String name = commerce.getName();
// holder.commerceTypeName.setText(name);
//loader.bind(holder.commerceImage, imageUri);
}
#Override
public int getItemCount() {
return commercesList.size();
}
public static class CommercesViewHolder extends RecyclerView.ViewHolder {
public ImageView commerceImage;
public TextView commerceTypeName;
public CommercesViewHolder(View itemView) {
super(itemView);
commerceImage = (ImageView) itemView.findViewById(R.id.commerceImageView);
commerceTypeName = (TextView) itemView.findViewById(R.id.commerceTextView);
}
}
Here the RecyclerView
And here in a root activity layout
Someone knows why this happen? if I add android:centerInParent"true" to the nested FrameLayout the image appears but i don't understand why.
In your adapter, uncomment the two lines that you have commented out;
`
// holder.commerceTypeName.setText(name);
//loader.bind(holder.commerceImage, imageUri);
`

RecyclerView Showing weird behavior on expanding CardView Android

<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>

Categories

Resources