android – RecyclerView和普通LinearLayout内部布局的不同宽度
作者:互联网
我有一个卡片视图类,看起来像:
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(..)中对其进行充气.
布局/ 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);
}
标签:android,android-recyclerview,android-cardview,view 来源: https://codeday.me/bug/20190830/1765655.html