RecyclerView 和普通 LinearLayout 内部布局的不同宽度

Different width of layout inside RecyclerView and normal LinearLayout

我有一个卡片视图 class 看起来像:

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

card_main.xml 就像:

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

然后如果我在我的片段中做

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

我会看到这个结果(实际上,如我所料):

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

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

我会得到这个结果

为什么我的卡片视图的水平尺寸不同?会不会是有上下文的东西?

基本上 CardTest 变成了它不想要的布局。 在您的适配器中,您创建了一个 CardTest 实例,而不是为其提供布局参数,因此 CardTest 的布局结构将不同于 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>

你不是这个意思吧?

只需在 card_main.xml:

中删除外部 LinearLayout(您的视图本身已经是 LinearLayout)
<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>

然后添加到你的适配器方法:

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

...并向莫斯科致以热烈的问候,占据了整个屏幕宽度:)

编辑:您也可以考虑不动态创建 CardTest 视图层次结构,而是在 XML 中定义它并直接在 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>

在代码中:

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