Different width of layout inside RecyclerView and normal LinearLayout

故事扮演 提交于 2019-12-01 20:29:22
Drew

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.CardTest
    layout_width="wrap_content"
    layout_height="wrap_content">

<!--inflated hierarchy is added into your custom viewgroup-->

<LinearLayout 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_height="wrap_content"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    >
        <android.support.v7.widget.CardView
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            card_view:cardCornerRadius="4dp">

            <TextView
                android: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.CardView
        xmlns:android= "http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        card_view:cardCornerRadius="4dp">

        <TextView
            android: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:

 @Override
        public CalendarViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View itemView = new CardTest(viewGroup.getContext());
            RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(
    ViewGroup.LayoutParams.MATCH_PARENT, //width
    ViewGroup.LayoutParams.WRAP_CONTENT);//height
            itemView.setLayoutParams(lp);//override default layout params
            return new CalendarViewHolder(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.CardTest
        xmlns:android= "http://schemas.android.com/apk/res/android"
        layout_width="match_parent"
        layout_height="wrap_content">
<android.support.v7.widget.CardView

        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        card_view:cardCornerRadius="4dp">

        <TextView
            android: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:

    @Override
    public CalendarViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.my_item, viewGroup, false);

        return new CalendarViewHolder(itemView);
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!