Different Width Of Layout Inside Recyclerview And Normal Linearlayout
I have a card view class which looks like: public class CardTest extends LinearLayout { public CardTest(Context context) { super(context); LayoutInflate
Solution 1:
Basically CardTest becomes the layout it was not intended to be. In your adapter you create an instance of CardTest, not supplying layout params to it, and thus the layout structure of CardTest will be different from card_main.xml:
<com.yourpackage.CardTestlayout_width="wrap_content"layout_height="wrap_content"><!--inflated hierarchy is added into your custom viewgroup--><LinearLayoutxmlns: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_height="wrap_content"android:layout_width="fill_parent"android:orientation="vertical"
><android.support.v7.widget.CardViewandroid:layout_height="wrap_content"android:layout_width="fill_parent"card_view:cardCornerRadius="4dp"><TextViewandroid:id="@+id/tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Moscow"/></android.support.v7.widget.CardView></LinearLayout></com.yourpackage.CardTest>
and that's not what you meant, isn't it?
Just remove the outer LinearLayout (your view is already a LinearLayout itself) in card_main.xml:
<android.support.v7.widget.CardViewxmlns:android= "http://schemas.android.com/apk/res/android"android:layout_height="wrap_content"android:layout_width="fill_parent"card_view:cardCornerRadius="4dp"><TextViewandroid:id="@+id/tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Moscow"/></android.support.v7.widget.CardView>
Then add to your adapter method:
@Overridepublic CalendarViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
ViewitemView=newCardTest(viewGroup.getContext());
RecyclerView.LayoutParamslp=newRecyclerView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, //width
ViewGroup.LayoutParams.WRAP_CONTENT);//height
itemView.setLayoutParams(lp);//override default layout paramsreturnnewCalendarViewHolder(itemView);
}
...and send warm greetings to Moscow, taking the whole screen width :)
EDIT: You may also consider not creating your CardTest view hierarchy dynamically, but rather defining it in an XML and inflate it directly in onCreateViewHolder(..)
.
layout/my_item.xml
<com.yourpackage.CardTestxmlns:android= "http://schemas.android.com/apk/res/android"layout_width="match_parent"layout_height="wrap_content"><android.support.v7.widget.CardViewandroid:layout_height="wrap_content"android:layout_width="fill_parent"card_view:cardCornerRadius="4dp"><TextViewandroid:id="@+id/tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Moscow"/></android.support.v7.widget.CardView></com.yourpackage.CardTest>
And in code:
@Overridepublic CalendarViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
ViewitemView= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.my_item, viewGroup, false);
returnnewCalendarViewHolder(itemView);
}
Post a Comment for "Different Width Of Layout Inside Recyclerview And Normal Linearlayout"