I am trying to create something very simple: a CardView that stretches to screen height and has a TextView inside it which should stretch to the card width. I have made the card fill the screen vertically with android:height="match_parent" on the CardView. However, now the CardView contents, like a simple TextView will not stretch across the whole card horizontally when setting android:height="match_parent" on the TextView. It just looks like wrap_content instead.
Note that this CardView is being placed inside a RecyclerView with a horizontal LinearLayoutManager.
Note that setting android:layout_width="match_parent" on the card does not actually stretch it out to the screen width when using a horizontal LinearLayoutManager, and the opposite is true if using a vertical LinearLayoutManager! I think this has something to do with my problem.
Here is the CardView xml:
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="6dp"
card_view:contentPadding="8dp"
card_view:cardElevation="8dp"
card_view:cardCornerRadius="4dp"
card_view:cardUseCompatPadding="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView_video_thumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/textView_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:padding="2sp"
android:background="#80000000"
android:gravity="center"
android:textColor="#android:color/white"
android:textSize="16sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/textView_preview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:background="#80000000"
style="?android:attr/buttonBarButtonStyle"
android:text="Preview"/>
<Button
android:id="#+id/textView_set"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#80000000"
android:gravity="center"
style="?android:attr/buttonBarButtonStyle"
android:text="Set"/>
</LinearLayout>
</RelativeLayout>
Here is the RecyclerView xml:
<LinearLayout 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"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:gravity="center"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_wallpapers" tools:context=".WallpapersActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/cardList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
I fixed this by placing the CardView inside a RelativeLayout as the outer-most layout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
style="#style/CardLayout">
...
I guess you have problem with your inflater.
I had similar problem and changed my inflater from:
inflater.inflate(R.layout.services_card_list_item, null);
to this:
inflater.inflate(R.layout.services_card_list_item, parent, false);
This is working code. please try it.
public class MyRecyclerAdapter extends RecyclerView.Adapter<FeedListRowHolder> {
private List<FeedItem> feedItemList;
private Context mContext;
public MyRecyclerAdapter(Context context, List<FeedItem> feedItemList) {
this.feedItemList = feedItemList;
this.mContext = context;
}
#Override
public FeedListRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row,viewGroup, false);
FeedListRowHolder mh = new FeedListRowHolder(v);
return mh;
}
#Override
public void onBindViewHolder(FeedListRowHolder feedListRowHolder, int i) {
// feedListRowHolder.title.setText("set data");
}
#Override
public int getItemCount() {
return 10;
}
}
OR
Replace with your code.
LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.card_listitem, viewGroup, false);
This was actually a bug in an early version of the RecylerView library. I forget exactly in which version it was fixed, but I think it was somewhere around support:recyclerview-v7:23.1.0 where it was fixed.
Related
I want my cards be one to another,but they spawn with extra space,i don't know how to fix this.
I thought that my problem with recycleView.
//My RVadapter
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder>{
List<Bus> abstractlistbus;
public static class PersonViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView bus_name;
TextView controller_Status;
Context mContext;
PersonViewHolder(final View itemView, final Context context) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
mContext=context;
bus_name = (TextView)itemView.findViewById(R.id.name);
controller_Status = (TextView)itemView.findViewById(R.id.Status);
}
}
RVAdapter(List<Bus> abstractlist){
this.abstractlistbus = abstractlist;
}
#Override
public int getItemCount() {
return abstractlistbus.size();
}
#Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cards_layout, viewGroup, false);
PersonViewHolder pvh = new PersonViewHolder(v,v.getContext());
return pvh;
}
#Override
public void onBindViewHolder(/*final*/ PersonViewHolder personViewHolder, final int i) {
personViewHolder.bus_name.setText("Номер автобуса:"+abstractlistbus.get(i).number);
personViewHolder.controller_Status.setText(abstractlistbus.get(i).controller+"");
}
}
//My activity_main.xml
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
/>
</RelativeLayout>
//My cards_layout.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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:tag="cards main container">
<android.support.v7.widget.CardView
android:id="#+id/cv"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="5dp"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_weight="2"
android:orientation="vertical"
>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Android Name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/Status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Android Version"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
//And piece of code im MainActivity.java
RecyclerView rv = (RecyclerView) findViewById(R.id.my_recycler_view);
rv.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
rv.setLayoutManager(llm);
extra space between cards
enter image description here
enter image description here
enter image description here
in your card linear layout make **android:layout_height="match_parent"** to **android:layout_height="wrap_content"**
<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:orientation="vertical"
android:tag="cards main container">
In your cards_layout.xml - add these margins to your linear layout.
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
This will solve your problem.
in you adapter view change CardView's height match_parent to wrap_content
Try it with this tag in your CardView:
app:cardUseCompatPadding="true"
I have a very simple scenario involving a NestedScrollView and LinearLayout where I want the LinearLayout to translate as the NestedScrollView scrolls up. Here is the layout in picture:(the concerned child view is the second last LinearLayout with the Id of linearlayout )
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="com.example.snapsboardmainpage.MainActivity"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<android.support.v4.widget.NestedScrollView
android:id="#+id/nested_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="vertical"
android:layout_gravity="top"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="0dp"
android:layout_marginRight="0dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="parent">
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="0dp"
android:layout_height="112dp"
android:orientation="vertical"
android:background="#android:color/holo_green_light"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/linearLayout3">
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/id_viewpager_photosvideos_albums"
android:layout_width="0dp"
android:layout_height="1000dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout1">
<android.support.design.widget.TabLayout
android:id="#+id/id_tab_photosvideos_albums"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top">
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.design.widget.TabLayout>
</android.support.v4.view.ViewPager>
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="vertical"
android:layout_gravity="top"
app:layout_behavior="com.example.snaps.TopActionBarBehavior"
android:background="#android:color/holo_blue_light">
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="56dp"
android:orientation="vertical"
android:layout_gravity="bottom"
android:background="#android:color/holo_orange_light">
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
And, the concerned behavior(TopActionBarBehavior) as follows:
public class TopActionBarBehavior extends CoordinatorLayout.Behavior<LinearLayout> {
private static final String TAG = "TopActionBarBehavior";
private int thresholdScrollDistance;
private int mNestedScrollViewInitialTop;
public TopActionBarBehavior() {
}
public TopActionBarBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean layoutDependsOn(CoordinatorLayout parent, LinearLayout child, View dependency) {
return dependency instanceof NestedScrollView;
}
#Override
public boolean onDependentViewChanged(CoordinatorLayout parent, LinearLayout child, View dependency) {
// Translate the child view as per the NestedScrollView
int currentNestedScrollViewTop = dependency.getTop();
// Have we scrolled up?
if(currentNestedScrollViewTop > mNestedScrollViewInitialTop){
// Translate the child view by the same distance
child.setTranslationY(currentNestedScrollViewTop - mNestedScrollViewInitialTop);
}
return true;
}
#Override
public boolean onLayoutChild(CoordinatorLayout parent, LinearLayout child, int layoutDirection) {
View topBar = parent.findViewById(R.id.linearLayout);
int topBarHeight = topBar.getHeight();
View nestedScrollView = parent.findViewById(R.id.nested_container);
mNestedScrollViewInitialTop = nestedScrollView.getTop();
View profileBar = parent.findViewById(R.id.linearLayout1);
int profileBarHeight = profileBar.getHeight();
View dummyTopBarUnderlay = parent.findViewById(R.id.linearLayout3);
int dummyTopBarHeight = dummyTopBarUnderlay.getHeight();
View tabLayout = parent.findViewById(R.id.id_tab_photosvideos_albums);
int tabLayoutHeight = tabLayout.getHeight();
thresholdScrollDistance = profileBarHeight + dummyTopBarHeight + tabLayoutHeight;
return super.onLayoutChild(parent, child, layoutDirection);
}
}
I tried debugging and layoutDependsOn() is getting called as it should be but onDependentViewChanged() is getting called only once.
Finally found the answer after countless hours of debugging. My NestedScrollView wasn't actually moving at all; it's content was moving the whole time which didn't trigger this method. For scroll events, we can tap onto the onNested*() methods in the Behavior class. Simple.
I have 2 EditText views in my app:
<?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="fill_parent"
android:background="#color/white">
<MyProject.MyView
android:id="#+id/view1"
android:layout_width="match_parent"
android:layout_height="60dp" />
<EditText
android:id="#+id/view2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black"
style="#style/my_style"
android:hint="my hint2"/>
Here is the code for MyProject.MyView
public class MyView : LinearLayout
{
// not important code...
public LinearLayout Panel { get; private set; }
// Gets called from the constructor:
private void Init()
{
var layoutInflater = (LayoutInflater)this.Context.GetSystemService(Android.Content.Context.LayoutInflaterService);
this.content = layoutInflater.Inflate(Resource.Layout.my_content, this.Panel, true);
this.AddView(this.content);
}
}
And here is my_content:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
style="#style/my_style2">
<EditText
android:id="#+id/view1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/my_style"
android:hint="my hint">
</EditText>
</LinearLayout>
The first EditText (that's being inflated from my MyView class) won't match the parent's width, it will only wrap the content. But the second EditText matches the parent's width. Why?
I solved it by wrapping the <LinearLayout> with a <RelativeLayout> in my my_content xml. I'm not sure why this was needed, so I would still appreciate an explanation so that I can better understand the way Android works.
I have a HorizontalScrollView inside a VerticalScrollView. If I scroll Horizontally , the scrolling is smooth. But if I scroll Vertically , the scroll is not smooth, it is getting scrolled horizontally sometimes. How to make the vertical scroll smooth ?
Here is my 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"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sasank.calendarview.MainActivity"
android:background="#android:color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_marginLeft="200dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/chapter_list"
android:layout_width="150dp"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
<HorizontalScrollView
android:id="#+id/horizontal_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/chapter_list"
android:scrollbars="horizontal">
<android.support.v7.widget.RecyclerView
android:id="#+id/calendar"
android:layout_width="wrap_content"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</HorizontalScrollView>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
</RelativeLayout>
Im using a Vertical LinearLayoutManager for Chapter_list Recyclerview and GridLayoutManager for Calendar RecyclerView
android:nestedScrollingEnabled="true"
You can try this, but still problems connected to the nested scrolling and so on are actually common. People have had the same problems here on StackOverflow, take a look, i hope it can help you: Problems with Nested Scrolling
I have vertical scrollview and horizontal recycleview photo gallery in one of my projects and it works fine. Here's the code:
fragment/activity layout:
<ScrollView 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
...
<android.support.v7.widget.RecyclerView
android:id="#+id/horizontal_recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:horizontalSpacing="10dp"
android:isScrollContainer="false"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"
/>
...
</LinearLayout>
</ScrollView>
item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/horizontal_item_view_image"
android:layout_marginRight="10dp"
android:layout_width="109dp"
android:layout_height="109dp" />
</LinearLayout>
adapter:
public class HorizontalPhotosAdapter extends RecyclerView.Adapter<HorizontalPhotosAdapter.MyViewHolder> {
private Context context;
private LayoutInflater inflater;
private ArrayList<Bitmap> bitmapList;
public class MyViewHolder extends RecyclerView.ViewHolder {
private ImageView riv;
public MyViewHolder(View view) {
super(view);
riv = (ImageView) view.findViewById(R.id.horizontal_item_view_image);
}
}
public HorizontalPhotosAdapter(Context context, ArrayList<Bitmap> bitmapList, String[] imageUrls) {
this.context = context;
this.bitmapList = bitmapList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.horizontal_item_view, parent, false);
if (itemView.getLayoutParams ().width == RecyclerView.LayoutParams.MATCH_PARENT)
itemView.getLayoutParams ().width = RecyclerView.LayoutParams.WRAP_CONTENT;
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.riv.setImageBitmap(bitmapList.get(position));
}
#Override
public int getItemCount() {
return bitmapList.size();
}
}
implementation in fragment/activity:
horizontalAdapter=new HorizontalPhotosAdapter(getContext(), bitmapList);
horizontal_recycler_view.setAdapter(horizontalAdapter);
horizontalAdapter.notifyDataSetChanged();
LinearLayoutManager horizontalLayoutManagaer = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
horizontal_recycler_view.setLayoutManager(horizontalLayoutManagaer);
horizontal_recycler_view.setAdapter(horizontalAdapter);
I have a problem which is driving me nuts. Basically it is a simple RecyclerView which displays a cardview. There are a lot of posts out there already, which I checked. I have assigned a LayoutManager as indicated here (RecyclerView Not Displaying Any CardView Items) , I have implemented the getItemCount method as explained here (card view not showing up), and I changed to the following versions (changing version was suggested here RecyclerView of cards not showing anything):
compile 'com.android.support:support-v4:24.0.0'
compile 'com.android.support:cardview-v7:24.0.0'
compile 'com.android.support:recyclerview-v7:24.0.0'
I also did the notifyDataSetChanged() as indicated here (RecyclerView Not Displaying Any CardView Items). However, nothing fixed it for me.
I have an Activity which has the following code (only relevant sections, getDummyData returns a List with 1 dummy data):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DatabaseHelper databaseHelper = new DatabaseHelper(getApplicationContext());
setContentView(R.layout.activity_overview);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
rv.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rv.setLayoutManager(linearLayoutManager);
RVAdapter adapter = new RVAdapter(getDummyData());
rv.setAdapter(adapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
adapter.notifyDataSetChanged();
}
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.DocumentViewHolder> {
List<Document> DocumentList;
public RVAdapter(List<Document> Documents) {
this.DocumentList = Documents;
}
#Override
public DocumentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.document_item, parent, false);
DocumentViewHolder pvh = new DocumentViewHolder(v);
return pvh;
}
#Override
public void onBindViewHolder(DocumentViewHolder DocumentViewHolder, int i) {
DocumentViewHolder.Date.setText(DocumentList.get(i).getCreatedFormatted());
DocumentViewHolder.participants.setText(DocumentList.get(i).getParticipants(getApplicationContext()));
DocumentViewHolder.counterOfItems.setText(DocumentList.get(i).getcounterOfItems(getApplicationContext()) + StringUtils.EMPTY);
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public int getItemCount() {
//return DocumentList.size();
return 1;
}
public class DocumentViewHolder extends RecyclerView.ViewHolder {
TextView Date;
TextView participants;
TextView ideasPreview;
TextView counterOfItems;
ImageView Photo;
public DocumentViewHolder(View itemView) {
super(itemView);
Date = (TextView) itemView.findViewById(R.id._date);
participants = (TextView) itemView.findViewById(R.id.participants);
ideasPreview = (TextView) itemView.findViewById(R.id.ideas_preview);
counterOfItems = (TextView) itemView.findViewById(R.id.counter_of_items);
Photo = (ImageView) itemView.findViewById(R.id._photo);
}
}
}
And I have a Document_item - xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView 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"
android:layout_gravity="center"
card_view:cardCornerRadius="#dimen/_card_corner">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingEnd="#dimen/activity_horizontal_margin"
android:paddingStart="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<TextView
android:id="#+id/_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="21st June 2016" />
<TextView
android:id="#+id/participants"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Thomas, Christian and Johann" />
<TextView
android:id="#+id/counter_of_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Thomas, Christian and Johann" />
<TextView
android:id="#+id/ideas_preview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello, Test, Test, Test" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
The activity's xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
android:fitsSystemWindows="true"
tools:context="com.chmaurer.idea.OverviewActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/ic_playlist_add_white_24dp" />
</android.support.design.widget.CoordinatorLayout>
The code is compiling and executing normally, but no cards are shown. I have tried a few hour for myself now, but it is definitely the point where I'd be glad to have a hint...
Your code is fine, The problem is with layout file: document_item.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/_photo"
android:layout_width="wrap_content" -->mistake
android:layout_height="wrap_content"
android:layout_weight="0.25" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" -->mistake
android:gravity="center_horizontal"
android:orientation="vertical"
...
The second LinearLayout gets its height from parent (first LinearLayout), and the first one gets its height from its only child: ImageView (so without no drawable set, the second layout's height will be zero! ), if you set a drawable for ImageView (in onBindViewHolder method or in layout xml file) it probably crops some of TextView items, Also when you set android:layout_width to "wrap_content", actually you're ignoring layout_weight.
So edit layout file like this:
...
<ImageView
android:id="#+id/_photo"
android:layout_width="0dp"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content"
android:layout_weight="0.25" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
You are using RecyclerView with vertical orientation (in Java code)
In the Activity XML you are setting the layout height to Wrap_Content
The card layout also has an height of match_parent
They will not work like that, I lost three days on this
Try to give fix height to your card layout, test also other alteration of other layout, it will work for you
Good luck
Just try after adding this line inside your recyclerview:
app:layout_behavior="#string/appbar_scrolling_view_behavior"
like this:
<android.support.v7.widget.RecyclerView
android:id="#+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>
Hope it will help you.