CardView未在RecyclerView中显示 [英] CardView not shown in RecyclerView
问题描述
我遇到一个让我发疯的问题.基本上,这是一个简单的RecyclerView,它显示一张卡片视图.我已经检查了很多帖子.我已按此处所示分配了LayoutManager( RecyclerView不显示任何CardView项) ,我已经实现了getItemCount方法,如此处所述(卡片视图未显示),并且我更改为以下版本(此处建议更改版本卡的RecyclerView没有显示任何内容):
I have a problem which is driving me nuts. Basically it is a simple RecyclerView which displays a cardview. There are a lot of posts out there already, which I checked. I have assigned a LayoutManager as indicated here (RecyclerView Not Displaying Any CardView Items) , I have implemented the getItemCount method as explained here (card view not showing up), and I changed to the following versions (changing version was suggested here RecyclerView of cards not showing anything):
compile 'com.android.support:support-v4:24.0.0'
compile 'com.android.support:cardview-v7:24.0.0'
compile 'com.android.support:recyclerview-v7:24.0.0'
我还按照此处指示进行了notifyDataSetChanged()( RecyclerView不显示任何CardView项).但是,没有什么可以解决我的问题.
I also did the notifyDataSetChanged() as indicated here (RecyclerView Not Displaying Any CardView Items). However, nothing fixed it for me.
我有一个具有以下代码的Activity(仅相关部分,getDummyData返回具有1个伪数据的List):
I have an Activity which has the following code (only relevant sections, getDummyData returns a List with 1 dummy data):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DatabaseHelper databaseHelper = new DatabaseHelper(getApplicationContext());
setContentView(R.layout.activity_overview);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
rv.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rv.setLayoutManager(linearLayoutManager);
RVAdapter adapter = new RVAdapter(getDummyData());
rv.setAdapter(adapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
adapter.notifyDataSetChanged();
}
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.DocumentViewHolder> {
List<Document> DocumentList;
public RVAdapter(List<Document> Documents) {
this.DocumentList = Documents;
}
@Override
public DocumentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.document_item, parent, false);
DocumentViewHolder pvh = new DocumentViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(DocumentViewHolder DocumentViewHolder, int i) {
DocumentViewHolder.Date.setText(DocumentList.get(i).getCreatedFormatted());
DocumentViewHolder.participants.setText(DocumentList.get(i).getParticipants(getApplicationContext()));
DocumentViewHolder.counterOfItems.setText(DocumentList.get(i).getcounterOfItems(getApplicationContext()) + StringUtils.EMPTY);
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public int getItemCount() {
//return DocumentList.size();
return 1;
}
public class DocumentViewHolder extends RecyclerView.ViewHolder {
TextView Date;
TextView participants;
TextView ideasPreview;
TextView counterOfItems;
ImageView Photo;
public DocumentViewHolder(View itemView) {
super(itemView);
Date = (TextView) itemView.findViewById(R.id._date);
participants = (TextView) itemView.findViewById(R.id.participants);
ideasPreview = (TextView) itemView.findViewById(R.id.ideas_preview);
counterOfItems = (TextView) itemView.findViewById(R.id.counter_of_items);
Photo = (ImageView) itemView.findViewById(R.id._photo);
}
}
}
我有一个Document_item-xml:
And I have a Document_item - xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
card_view:cardCornerRadius="@dimen/_card_corner">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingEnd="@dimen/activity_horizontal_margin"
android:paddingStart="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="21st June 2016" />
<TextView
android:id="@+id/participants"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Thomas, Christian and Johann" />
<TextView
android:id="@+id/counter_of_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Thomas, Christian and Johann" />
<TextView
android:id="@+id/ideas_preview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello, Test, Test, Test" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
活动的xml如下所示:
The activity's xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.chmaurer.idea.OverviewActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_playlist_add_white_24dp" />
</android.support.design.widget.CoordinatorLayout>
代码正在正常编译和执行,但是没有显示卡.我现在已经为自己尝试了几个小时,但这绝对是我很高兴能得到提示的地方...
The code is compiling and executing normally, but no cards are shown. I have tried a few hour for myself now, but it is definitely the point where I'd be glad to have a hint...
推荐答案
您的代码很好,问题在于布局文件:document_item.xml
:
Your code is fine, The problem is with layout file: document_item.xml
:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/_photo"
android:layout_width="wrap_content" -->mistake
android:layout_height="wrap_content"
android:layout_weight="0.25" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" -->mistake
android:gravity="center_horizontal"
android:orientation="vertical"
...
第二个LinearLayout
从其父级(第一个LinearLayout
)获取高度,第一个LinearLayout
从其唯一的子级获取其高度:ImageView
(因此,如果未设置drawable
,则第二个布局的高度将为零!),如果您为ImageView
设置了drawable
(在onBindViewHolder
方法或布局xml文件中),则可能会裁剪某些TextView
项.同样,当您将android:layout_width
设置为"wrap_content"
时,实际上您忽略了layout_weight
.
The second LinearLayout
gets its height from parent (first LinearLayout
), and the first one gets its height from its only child: ImageView
(so without no drawable
set, the second layout's height will be zero! ), if you set a drawable
for ImageView
(in onBindViewHolder
method or in layout xml file) it probably crops some of TextView
items, Also when you set android:layout_width
to "wrap_content"
, actually you're ignoring layout_weight
.
因此,请像这样编辑布局文件:
So edit layout file like this:
...
<ImageView
android:id="@+id/_photo"
android:layout_width="0dp"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content"
android:layout_weight="0.25" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
这篇关于CardView未在RecyclerView中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!