CardView 未显示在 RecyclerView 中

2023-12-04

我有一个问题让我发疯。基本上它是一个简单的 RecyclerView,显示卡片视图。已经有很多帖子了,我查了一下。我已经分配了一个 LayoutManager,如此处所示(RecyclerView 不显示任何 CardView 项目),我已经实现了 getItemCount 方法,如此处所述(卡片视图未显示),我更改为以下版本(这里建议更改版本卡片的 RecyclerView 不显示任何内容):

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 项目)。然而,没有什么能解决我的问题。

我有一个 Activity,其中包含以下代码(仅相关部分,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>

代码编译执行正常,但是不显示任何卡牌。我现在已经自己尝试了几个小时,但这绝对是我很高兴得到提示的时刻......


您的代码很好,问题出在布局文件上: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设置后,第二个布局的高度将为零! ),如果您设置drawable for ImageView (in onBindViewHolder方法或在布局 xml 文件中)它可能会裁剪一些TextView项目,此外,当您设置android:layout_width to "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"
            ...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

CardView 未显示在 RecyclerView 中 的相关文章

  • 任务“:app:dexDebug”执行失败

    我目前正在处理我的项目 我决定将我的 Android Studio 更新到新版本 但在我导入项目后 它显示如下错误 Information Gradle tasks app assembleDebug app preBuild UP TO
  • ImageView 中的全尺寸图像

    我正在尝试在 ImageView 中绘制图像 但我希望它不缩放 并根据需要使用滚动条 我怎样才能做到这一点 现在我只有一个可绘制集作为 XML 中 ImageView 的 android src 这会自动缩放图像以适应屏幕宽度 我读到这可能
  • Android studio 在日志猫中“清除全部”,更改日志级别过滤器时日志仍然会回来

    在 LogCat 中 当我单击 全部清除 按钮时 它似乎清除了所有日志 但是 如果我更改日志级别并返回到之前的日志级别 则所有日志都会返回 例如 我正在查看 Verbose 我选择 全部清除 日志清除 我切换到 调试 我切换回 详细 现在所
  • doInBackground 运行时是否可以停止 asynctask?

    我正在 ActivityB Oncreate 中创建异步任务 在该任务中 我正在运行无限 while 循环doInBackground 当我转到上一个活动并再次回到该活动时 创建了另一个异步任务 我的问题现在是两个无限 while 循环正在
  • 启动 Twitter 应用程序 [重复]

    这个问题在这里已经有答案了 可能的重复 Twitter 应用程序的 Android Intent https stackoverflow com questions 2077008 android intent for twitter ap
  • 如何从另一个xml文件动态更新xml文件?

    我想从另一个 xml 文件更新 xml 文件 我使用了一个 xml 文件 如下所示 one xml
  • android 谷歌+登录定制

    我正在创建一个 Android 应用程序 现在我正在实现社交网络登录 Facebook 按钮很好 但 google 按钮的语言与 Facebook 不同 另外 它只说 登录 我想让它说 用谷歌登录 我是 android 编程的新手 看到我需
  • 无法解析符号 FlutterActivity

    我使用 VCS gt Checkout from Version Control 将 flutter 项目从 github 导入到 Android Studio 中 现在我面临的问题是 Cannot resolve symbol Flutt
  • 无法合并 Dex - Android Studio 3.0

    当我在稳定频道中将 Android Studio 更新到 3 0 并运行该项目时 我开始收到以下错误 Error Execution failed for task app transformDexArchiveWithExternalLi
  • Android 手机应用意图

    我想在手机上启动手机应用程序作为意图 我正在使用这个代码 startActivity getPackageManager getLaunchIntentForPackage com android phone 但该函数抛出一个空指针异常 因
  • 在 Android 上的测试用例之外运行 ExtractDecodeEditEncodeMuxTest

    我正在尝试添加在 Android 上提取 解码 编辑 编码和混合视频的功能 因此 我发现了一些非常有用的实现 它是Android CTS的一部分ExtractDecodeEditEncodeMuxTest https android goo
  • 如何在 Android 上的 HttpPost 中发送 unicode 字符

    我试图在我的应用程序中允许多语言支持 这会发出 HTTP post 来上传新消息 我需要做什么才能支持日语和其他非拉丁语语言 我的代码目前看起来像这样 note the msg string is a JSON message by the
  • Facebook LoginActivity 未正确显示

    我有一个使用 Facebook 登录的应用程序 我有 FacebookSDK 并且使用 com facebook LoginActivity 问题是 在 10 英寸平板电脑上 当显示软键盘时 活动无法正确显示 我使用的是 Samsung G
  • 如何从SurfaceView绘制到Canvas?

    我正在尝试做简单的画家 问题是Android看起来有三个独立的Canvas并给我它来顺序绘制 我用以下方式制作了用户界面SurfaceView 把霍尔德从中拿走 Override protected void onCreate Bundle
  • 如何在android中录制音频时暂停背景音乐

    我正在 Android 中开发一个音频记录应用程序 因此 如果设备音乐播放器中已播放任何背景音乐 则应在开始录制之前暂停该背景音乐 并且每当录制停止或暂停时 背景音乐都应恢复 播放录制的音频时也应该如此 有人可以帮我解决这个问题吗 提前致谢
  • 无法在 Android 模拟器中安装 apk

    我正在尝试通过 adb shell 在 ICS 模拟器中安装 apk 从一个站点下载 但出现以下错误 失败 INSTALL FAILED UID CHANGED 可能是什么问题 只需 rm r 有问题的数据目录即可 如果您在安装时遇到此错误
  • Android:如何通过右侧的十字按钮清除EditText

    我创建了一个EditText用于搜索 左侧包含搜索图标 右侧包含图标
  • 使用支持库中的 BottomSheet 时如何调暗背景?

    怎样才能让背景像显示的那样变暗here https material design storage googleapis com publish material v 8 material ext publish 0Bzhp5Z4wHba3
  • Android:获取最新意图

    如何获取发送到活动的最后一个意图 的文档onNewIntent 建议我需要做这样的事情 class MyActivity public void onNewIntent Intent intent setIntent intent reac
  • 在 Android 中更新到 API 26 时,清单合并失败并出现多个错误

    我尝试使用 API 26 更新我的 gradle 安卓工作室2 3 3 但我在编译项目时遇到以下错误 这是我收到的错误的屏幕截图 应用级别build gradle Top level build file where you can add

随机推荐

  • MySQL触发器:在插入时将auto_increment值复制到另一列

    我需要在插入时将 auto increment ID 值复制到另一列中 我想我需要一个 插入后 触发器 因为否则新 ID 是未知的 我试过这个 IF NEW content IS NULL THEN SET NEW content NEW
  • Spring boot:不支持请求方法“PUT”

    我得到了Request method PUT not supported击中时出错PUTRestful API 上的方法来上传文件 以下是上传到此票证的信息 客户端日志 休息控制器 Spring Boot 应用程序配置 Tomcat日志 P
  • 保存 CLOS 对象

    将任何 Common Lisp 结构对象保存到文件 可读 似乎相对简单 例如 defun save structure object object filename with open file stream filename direct
  • 同一应用程序中的 Django 不同时区

    我正在开发一个关于休假管理的 Django 应用程序 其中有来自不同国家的员工 我将创建休假时的数据存储到数据库中 现在 我希望插入数据库的日期时间是该特定员工工作地点的当前当地时间 例如 假设 X 先生在印度工作 Y 在纽约工作 如果X申
  • 使用 WSDL 中的 WCF 托管服务 - SVCUtil 为方法生成详细类型

    我有一个来自已发布的 ASMX Web 服务的 WSDL 文件 我追求什么 正在创建一个模仿真实服务的模拟服务以进行测试 在 WSDL 中 我使用 SvcUtil exe 生成代码 显然它也生成 服务器端接口 问题是它生成的接口非常笨重 例
  • UltragridCells 中的标准格式与自定义格式

    我正在尝试格式化Ultragridcell使用下面的代码 它工作正常 Code DefaultEditorOwnerSettings editorSettings DateTimeEditor datetime editor editorS
  • REG 添加 REG_MULTI_SZ 多行注册表值

    要添加 REG MULTI SZ 多行注册表值 我可以这样做 reg exe ADD HKLM path to registry key v RegistryValue t REG MULTI SZ d abc 0def 0 这将添加 ab
  • CMake错误:本项目中使用了以下变量,但它们被设置为NOTFOUND

    我正在 CentOS 6 上为项目运行 CMake 构建脚本 但收到以下错误 CMake Error The following variables are used in this project but they are set to
  • 快速排序与合并排序[重复]

    这个问题在这里已经有答案了 为什么快速排序比合并排序更好 See 维基百科上的快速排序 通常 快速排序显着 在实践中比其他 nlogn 更快 算法 因为它的内循环可以 在大多数情况下得到有效实施 架构以及大多数现实世界中 数据 可以进行设计
  • 使用 HttpClient 将字节数组发送到 Web API 服务器

    我想将此数据发布到 Web API 服务器 public sealed class SomePostRequest public int Id get set public byte Content get set 使用此代码作为服务器 R
  • 本机查询上的 Mockito NullPointerException

    我的查询对象有问题 即使我使用查询模拟对象对其进行存根 它也会变为空 这是代码 Query query getEntityManager createNativeQuery queryString SomeRandom class retu
  • 文件java替换字符

    我必须检查文本文档是否存在 然后我必须将其中的字母替换为 o 我已经完成了如何替换 char 的第一部分 class FDExists public static void main String args File file new Fi
  • 如何在 Bash 中编写“for”循环?

    我正在寻找基本循环 例如 for int i 0 i lt MAX i doSomething i 但对于巴什来说 From 这个网站 for i in seq 1 10 do echo i done
  • 如何在 pygame 中添加敌人? [复制]

    这个问题在这里已经有答案了 我正在用 pygame 制作一个简单的游戏 目标是躲避来袭的敌人 我想要一个领带战斗机图像随机落下 并且 xwing 必须躲避它们 否则你就会死 如何在我的脚本中实现随机平局战士 在我的代码中 我收到一个错误 指
  • java.lang.NoClassDefFoundError: org/springframework/core/io/ResourceLoader

    我正在使用 spring 进行一些测试 并且收到此错误 java lang NoClassDefFoundError org springframework core io ResourceLoader at org springframe
  • DatagridView 复选框已选中?

    我在 datagridview 窗口窗体中有一个复选框 并且有一个事件处理程序 cell Click 在单元格上单击 我检查 datagridview 列中的复选框 如果也选择了该单元格 即未选中该复选框 仅选择了 datagrid vie
  • python中父进程退出时如何杀死子进程?

    fork child py 中的代码 from subprocess import Popen child Popen ping google com stdout subprocess PIPE stderr subprocess PIP
  • PDFKit 正在垂直翻转用图像初始化的 PDFPage

    不确定这是否是 iOS 上的 PDFKit 测试版中的错误 但是当我基于图像数组创建 PDFDocument 使用 PDFPage image 时 它会垂直翻转图像 IBAction func export sender Any let a
  • 在 C 中使用 memset 表示整数数组

    char str beautiful earth memset str 6 printf s str Output ful earth 就像上面使用 memset 一样 我们可以只将几个整数数组索引值初始化为 1 如下所示 int arr
  • CardView 未显示在 RecyclerView 中

    我有一个问题让我发疯 基本上它是一个简单的 RecyclerView 显示卡片视图 已经有很多帖子了 我查了一下 我已经分配了一个 LayoutManager 如此处所示 RecyclerView 不显示任何 CardView 项目 我已经