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

时间:2015-02-18 18:40:31

标签: android view android-recyclerview android-cardview

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

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

我会看到这个结果(实际上,正如我所料): enter image description here

但如果我现在使用带有此代码的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);
        }
    }

我希望得到这个结果

enter image description here

为什么我的卡片的水平尺寸不同?它可能是具有上下文的东西吗?

1 个答案:

答案 0 :(得分:5)

基本上,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(..)中对其进行充气。

布局/ 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);
    }