【问题标题】:CardView not shown in RecyclerViewCardView 未显示在 RecyclerView 中
【发布时间】:2016-07-12 21:14:36
【问题描述】:

我有一个让我发疯的问题。基本上它是一个显示卡片视图的简单 RecyclerView。已经有很多帖子了,我查了一下。我已经分配了一个 LayoutManager,如此处所示 (RecyclerView Not Displaying Any CardView Items),我已经实现了此处所解释的 getItemCount 方法 (card view not showing up),并且我更改为以下版本(此处建议更改版本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 Not Displaying Any CardView Items)。但是,没有什么能帮我解决这个问题。

我有一个包含以下代码的活动(仅相关部分,getDummyData 返回一个包含 1 个虚拟数据的列表):

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

<?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 如下所示:

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

代码正常编译执行,但没有显示卡片。我现在已经为自己尝试了几个小时,但这绝对是我很乐意得到提示的时候......

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    你的代码没问题,问题出在布局文件: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)获取高度,第一个从其唯一的子级获取高度:ImageView(所以如果没有设置drawable,第二个布局的高度将为零!),如果您为ImageView 设置drawable(在onBindViewHolder 方法或布局xml 文件中)它可能会裁剪一些TextView 项目,而且当您将android:layout_width 设置为"wrap_content" 时,实际上你忽略了layout_weight

    所以像这样编辑布局文件:

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

    【讨论】:

    • 感谢您的回复 - 这正是错误!
    【解决方案2】:

    您正在使用垂直方向的 RecyclerView(在 Java 代码中)

    在 Activity XML 中,您将布局高度设置为 Wrap_Content

    卡片布局也有 match_parent 的高度

    他们不会那样工作的,我为此损失了三天

    尝试为您的卡片布局提供固定高度,同时测试其他布局的其他更改,它会为您工作

    祝你好运

    【讨论】:

      【解决方案3】:

      在你的 recyclerview 中添加这一行后尝试:

      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      

      像这样:

      <android.support.v7.widget.RecyclerView
          android:id="@+id/rv"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:layout_behavior="@string/appbar_scrolling_view_behavior">
      </android.support.v7.widget.RecyclerView>
      

      希望对你有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多