RecyclerView和普通LinearLayout内部布局的不同宽度 [英] Different width of layout inside RecyclerView and normal LinearLayout

查看:60
本文介绍了RecyclerView和普通LinearLayout内部布局的不同宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个卡片视图类,如下所示:

I have a card view class which looks like:

public class CardTest extends LinearLayout {
    public CardTest(Context context) {
            super(context);
            LayoutInflater.from(getContext()).inflate(R.layout.card_main, this);
        }
}

card_main.xml类似于:

the card_main.xml is like:

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

然后,如果我在片段中执行

then if I do in my Fragment

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.view_vll_scrollbar, null);

        LinearLayout ll = (LinearLayout) view.findViewById(R.id.vll);
        ll.addView(new CardTest(this.getActivity()));

        return view;
    }

我会看到此结果(实际上,正如我期望的那样):

I would see this result(actually, as I expect):

但是如果我现在使用带有此代码的RecyclerView

but if I now using a RecyclerView with this code

 public class CalendarAdapter extends RecyclerView.Adapter<CalendarViewHolder> {

        private List<ItemCalendar> mList;

        public CalendarAdapter(List<ItemCalendar> list) {
            this.mList = list;
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }

        @Override
        public void onBindViewHolder(CalendarViewHolder calendarViewHolder, int i) {
            ItemCalendar ci = mList.get(i);
            calendarViewHolder.headerText.setText(ci.getCity());
        }

        @Override
        public CalendarViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View itemView = new CardTest(viewGroup.getContext());

            return new CalendarViewHolder(itemView);
        }
    }

    public static class CalendarViewHolder extends RecyclerView.ViewHolder {
        protected TextView headerText;
        protected TextView bodyText;


        public CalendarViewHolder(View v) {
            super(v);
            headerText = (TextView) v.findViewById(R.id.card_extender_header_text);
        }
    }

我将得到这个结果

为什么我的名片视图的横向尺寸不同?可能与上下文有关吗?

Why are horzontal sizes of my card view different? Could it be something with context?

推荐答案

基本上,CardTest变成了原本不希望的布局. 在适配器中,您将创建CardTest实例,而不为其提供布局参数,因此CardTest的布局结构将与card_main.xml不同:

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?

只需删除card_main.xml中的外部LinearLayout(您的视图本身已经是LinearLayout):

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

编辑:您还可以考虑不动态创建CardTest视图层次结构,而是以XML定义并直接在onCreateViewHolder(..)中对其进行膨胀.

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

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>

在代码中:

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

这篇关于RecyclerView和普通LinearLayout内部布局的不同宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆