RecyclerView items not in center with GridLayoutManager - android

i have a RecyclerView with a GridLayoutManager which load items like this:
as you can see there is a margin to left. I didn't add any sort of ItemDecoration to recyclerview and didn't add any margin to any of the layouts here.
i've already tried:
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
and using LinearSnapHelper but none of them fixed it.
item layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="#dimen/_130sdp"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="#dimen/_130sdp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">
<androidx.cardview.widget.CardView
app:cardPreventCornerOverlap="true"
app:cardElevation="#dimen/_5sdp"
app:cardCornerRadius="#dimen/_3sdp"
android:layout_width="#dimen/_60sdp"
android:layout_height="#dimen/_90sdp"
android:layout_marginTop="#dimen/_20sdp">
<com.makeramen.roundedimageview.RoundedImageView
android:id="#+id/choosestorycoverimage"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:riv_corner_radius="#dimen/_3sdp"
android:scaleType="fitXY"
android:src="#drawable/test"/>
<com.makeramen.roundedimageview.RoundedImageView
android:id="#+id/choosestorycoverimagelayer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:riv_corner_radius="#dimen/_3sdp"
android:visibility="gone"
android:scaleType="fitXY"
android:src="#drawable/choosen_story_layer"/>
</androidx.cardview.widget.CardView>
<TextView
android:id="#+id/choosestorynametext"
android:layout_marginTop="#dimen/_3sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/_15sdp"
android:text="text"/>
</LinearLayout>
</RelativeLayout>
activity layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ChooseStoryActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/choosestoryrecyclerview"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_gravity="center_horizontal"
android:layout_height="match_parent" />
</RelativeLayout>
recyclerview adapter:
public class ChooseStoryRecyclerViewAdapter extends RecyclerView.Adapter<ChooseStoryRecyclerViewAdapter.ViewHolder> {
private static final String TAG = "LibraryRecyclerViewAdap";
private ArrayList<String> coverImageUrlList = new ArrayList<>();
private ArrayList<String> nameTextList = new ArrayList<>();
private ArrayList<String> shelfNameList = new ArrayList<>();
private ArrayList<HashSet<String>> shelfStoryIDList = new ArrayList<>();
private HashSet<String> shelfStoryIDSet = new HashSet<>();
private Context context;
public ChooseStoryRecyclerViewAdapter(ArrayList<String> coverImageUrlList, ArrayList<String> nameTextList,Context context) {
this.coverImageUrlList = coverImageUrlList;
this.nameTextList = nameTextList;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.choose_story_item,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
//***********the main method***********
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
Glide.with(context)
.asBitmap()
.load(coverImageUrlList.get(position))
.into(holder.coverImage);
holder.coverImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(holder.coverImageLayer.getVisibility() == View.VISIBLE){
holder.coverImageLayer.setVisibility(View.GONE);
}
else{
holder.coverImageLayer.setVisibility(View.VISIBLE);
}
}
});
holder.nameText.setText(nameTextList.get(position));
}
#Override
public int getItemCount() {
return coverImageUrlList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
RoundedImageView coverImage;
TextView nameText;
RoundedImageView coverImageLayer;
public ViewHolder(#NonNull View itemView) {
super(itemView);
coverImage = itemView.findViewById(R.id.choosestorycoverimage);
nameText =itemView.findViewById(R.id.choosestorynametext);
coverImageLayer = itemView.findViewById(R.id.choosestorycoverimagelayer);
}
}
}

Remove these unnecessary attributes from your recyclerView tag:
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal"
You should change the width of the parent RelativeLayout of your item layout to use match_parent instead of a fixed width and then play with the spanCount attribute of your GridLayoutManager if you want smaller item layout
You should also add the GridLayoutManager directly in your activity_main.xml if you don't really need any Custom GridLayoutManager.
For that, remove the GridLayoutManager from MainActivity.java if you have one and add these two new lines in your recyclerView tag
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="3"

Related

Reuse a inflated view, without inflating more than once

i want to reuse an inflated view just the same way a listView adapter does with convertView. Ive been lurking on the source code of adapters with no luck.
I need to add dinamically views depending on data to a recyclerItemView, now im inflating as much times as i need the view but having in mind its inside a recyclerView, this operation could become absurdly costly depending on the amount of data and scrolling behaviour of the user.
Other solution to my problem would be to create a listview inside the recyclerItem, so the listview wouldnt be scrollable, expands to maximum height depending on data added dynamically(making the listview container to expand to show all its data) and make the recyclerItem expand depending on its height.
ACTIVITY
public class Main1Activity extends AppCompatActivity {
RecyclerView recyclerView;
CustomAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
recyclerView = (RecyclerView) findViewById(R.id.recycler);
setUpRecycler();
}
private void setUpRecycler(){
Sample sample1 = new Sample("List of Items1");
Sample sample2 = new Sample("List of Items2");
Sample sample3 = new Sample("List of Items3");
List<Sample> sampleList = new ArrayList<>();
sampleList.add(sample1);
sampleList.add(sample2);
sampleList.add(sample3);
adapter = new CustomAdapter(this,sampleList);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="30dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
MODEL
public class Sample {
String name;
public Sample(){}
public Sample(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
XML ITEMS
**item_list**
<?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="40dp">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
**item_smaple**
<?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">
<TextView
android:id="#+id/name"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_margin="10dp"
android:text="List of Items"
android:textSize="20sp"/>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:orientation="vertical"/>
</LinearLayout>
ADAPTER
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private List<Sample> mySamples;
private Context mContext;
public CustomAdapter(Context context, List<Sample> mySamples) {
this.mContext = context;
this.mySamples = mySamples;
}
private Context getContext() {
return mContext;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public LinearLayout linearLayout;
public ViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
linearLayout = (LinearLayout) itemView.findViewById(R.id.linearLayout);
}
}
#Override
public CustomAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View contactView = inflater.inflate(R.layout.item_sample, parent, false);
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}
#Override
public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) {
Sample sample = mySamples.get(position);
holder.name.setText(sample.getName());
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.item_list, null, false);
for (int i = 0; i < 20; i++){
TextView textView = (TextView) v.findViewById(R.id.textView);
textView.setText("hello"+i);
holder.linearLayout.addView(v);
}
}
#Override
public int getItemCount() {
return mySamples.size();
}
}
Right now i´m inflating inside the loop. How could i modify view parameters to make it reusable to the framework?
My app crashes on the addView()
-->java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
The issue that you are referring to is the one which was addressed by creating RecyclerView which is an upgrade to ListView. What RecyclerView does is that it will create/inflate say 5-10 list item and when the user starts scrolling new listItem view(s) are not inflated infact just the data is updated inside the listItem.
This is a great explainer video of how recycler view actually works with a bit of code as well. https://www.youtube.com/watch?v=Wq2o4EbM74k
I hope this clears your doubt regarding the inflation issue.
My app crashes on the addView() -->java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
try:
#Override
public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) {
Sample sample = mySamples.get(position);
holder.name.setText(sample.getName());
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context
.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < 20; i++) {
View v = inflater.inflate(R.layout.item_list, holder.linearLayout, false);
TextView textView = (TextView) v.findViewById(R.id.textView);
textView.setText("hello" + i);
holder.linearLayout.addView(v);
}
}
itemSample.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="vertical">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_margin="10dp"
android:text="List of Items"
android:textSize="20sp"/>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="30dp"
android:orientation="vertical"
android:isScrollContainer="true"
/>
</LinearLayout>
itemList.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="40dp"
android:isScrollContainer="true"
android:id="#+id/txt_cont"
>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
You should try answer #

Only one item displaying in recycler view

I am having problems displaying all the items in my recyclerview. My code only shows one item (Home. I have 3 items: Home, Browse Photos and My Travels)
fragment_navigation_drawer.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_navigation_drawer"
android:background="#EEE"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.traveldiaries.ztir.traveldiaries.NavigationDrawerFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="220dp"
android:background="#16a085"
android:id="#+id/linearLayout">
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/drawerList"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private LayoutInflater inflater;
List<MenuList> data = Collections.emptyList();
public RecyclerAdapter(Context context, List<MenuList> data){
inflater= LayoutInflater.from(context);
this.data = data;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.itemlist,parent,false);
MyViewHolder holder= new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
MenuList current=data.get(position);
holder.title.setText(current.title);
holder.image.setImageResource(current.icon);
}
#Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView title;
ImageView image;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.item_list);
image = (ImageView) itemView.findViewById(R.id.icons);
}
}
}
NavigationDrawerFragment.java (posted code only related to recyclerview)
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
recyclerView = (RecyclerView) layout.findViewById(R.id.drawerList);
adapter = new RecyclerAdapter(getActivity(),getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return layout;
}
public static List<MenuList> getData(){
List<MenuList> data = new ArrayList<>();
String[] titles={"Home","My Travels","Browse Photos"};
int[] icons ={R.drawable.home,R.drawable.sunglasses,R.drawable.tooltip_image};
for(int i=0;i<3;i++){
MenuList current = new MenuList();
current.title=titles[i];
current.icon=icons[i];
Log.d("Data",titles[i]);
Log.d("Data",""+icons[i]);
data.add(current);
}
return data;
}
itemlist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:padding="8dp"
android:id="#+id/icons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_menu_camera"
android:layout_gravity="center_vertical"
/>
<TextView
android:id="#+id/item_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dummy Text"
android:layout_gravity="center_vertical"
android:padding="8dp"
/>
</LinearLayout>
What I was able to do is to check all the passed data (all seems to be fine). Also tried to change width to wrap content (fragment_navigation_drawer.xml) in both my relative and linear layout but I just lose my background color instead and still only one item appear.
Change height of LinearLayout to wrap_content in itemlist.xml
android:layout_height="wrap_content"
Just change the height of your itemlist from "match_parent" to "wrap_content".

Single textview list in recyclerview

I want to make an app which shows list (RecyclerView). I want to include a single TextView in the Viewholder of this RecyclerView.
MainActivity code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private CustomAdapter customAdapter;
ListView listView;
public Cursor cursor;
public StudentRepo studentRepo;
private final static String TAG = MainActivity.class.getName().toString();
private static final String TAG_BOOKMARKS="bookmarks";
private static final String TAG_ABOUT_US="about";
//recyclerView implementation
private List<TopSample> wordlist=new ArrayList<>();
private RecyclerView recyclerView;
private StudentAdapter studentAdapter;
//recyclerView implementation done
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
studentRepo = new StudentRepo(this);
//recyclerView implement
recyclerView=(RecyclerView)findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
databaseAccess.open();
Cursor cursor= databaseAccess.getInfo();
cursor.moveToFirst();
do{
TopSample topSample=new TopSample(cursor.getString(0));
wordlist.add(topSample);
}while (cursor.moveToNext());
databaseAccess.close();
studentAdapter=new StudentAdapter(wordlist);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
//recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(studentAdapter);
StudentAdapter class
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.MyViewHolder> {
private List<TopSample> wordslist= Collections.emptyList();
public class MyViewHolder extends RecyclerView.ViewHolder{
public TextView sword;
public MyViewHolder(View view){
super(view);
sword=(TextView)view.findViewById(R.id.vocab);
}
}
public StudentAdapter(List<TopSample> wordslist){
this.wordslist=wordslist;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView=LayoutInflater.from(parent.getContext()).inflate(R.layout.dictionary_list_row,parent,false);
MyViewHolder vh=new MyViewHolder(itemView);
return vh;
//return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
TopSample sample=wordslist.get(position);
holder.sword.setText(sample.getVocab());
}
#Override
public int getItemCount() {
return wordslist.size();
}
}
TopSample code:
public class TopSample {
public String vocab;
public TopSample(){}
public TopSample(String vocab){
this.vocab=vocab;
}
public String getVocab() {
return vocab;
}
public void setVocab(String vocab) {
this.vocab = vocab;
}
}
content_main.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:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</RelativeLayout>
dictionary_list_row.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/vocab"
android:textSize="32dp"
android:textStyle="bold"/>
</RelativeLayout>
So the output was like:
As you can see that an individual viewholder is taking longer rectangle size as expected and recycler view is not taking full match_parent command.
I wish to have an output like this, just as an example.:
I tried to changed length and width with different combination in the two xml files but didn't get the result as expected. Kindly have a look and tell me where i am wrong.
Just one more thing to add, i am extracting data from external database in which i am successful but this problem is probably layout problem, i think. Kindly correct me.
in dictionary_list_row.xml
In Relative layout
android:layout_height="wrap_content"
And of TextView
android:layout_height="wrap_content"
And
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
Did you try to set the RelativeLayout height to wrap_content instead of match_parent ??
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/vocab"
android:textSize="32dp"
android:textStyle="bold"
/>
</RelativeLayout>
Problem is you have given fixed height to TextView and given match_parent width and have assigned font size very large.. that is 32dp.
Use below code. You can remove android:textSize="18sp" for default size.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/vocab"
android:textSize="18sp"
android:textStyle="bold"
/>

Gap between CardViews increases on scrolling

I'm using CarViews with RecyclerView and it looks fine when loaded but once when the list is scrolled the gap between the CardViews increases and there shows only one card in the view at a time.
Here is my CardView.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:id="#+id/conversationCard"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="#+id/sender"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/abstractConvo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="35dp"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
and here is how I'm using that view
public class ConversationsListAdapter extends RecyclerView.Adapter<ConversationsListAdapter.ConversationsListViewHolder> {
List<Conversation> conversationList;
public ConversationsListAdapter(List<Conversation> conversationList) {
this.conversationList = conversationList;
}
#Override
public int getItemCount() {
return conversationList.size();
}
#Override
public ConversationsListViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.conversation_card, viewGroup, false);
ConversationsListViewHolder listViewHolder = new ConversationsListViewHolder(view);
return listViewHolder;
}
#Override
public void onBindViewHolder(ConversationsListViewHolder holder, int position) {
holder.sender.setText(conversationList.get(position).getSenderPhNo());
holder.abstractConvo.setText(conversationList.get(position).getAbstractConvo());
}
public static class ConversationsListViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView abstractConvo, sender;
public ConversationsListViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.conversationCard);
sender = (TextView) itemView.findViewById(R.id.sender);
abstractConvo = (TextView) itemView.findViewById(R.id.abstractConvo);
}
}
}
Here is the screenshot before the list is scrolled
and after scrolling
Thanks in advance.
the root layout use wrap_content
<LinearLayout 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">

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

Categories

Resources