【问题标题】:CardViews not showing in RecyclerViewCardViews 未显示在 RecyclerView 中
【发布时间】:2018-06-25 16:00:45
【问题描述】:

我在RecyclerView 中有一个简单的CardView,但我看不到任何卡片出现。

这是我当前的代码:

MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".MainActivity"
android:background="@color/colorPrimaryDark">

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/pinRecyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v7.widget.RecyclerView>

</LinearLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_marginLeft="15dp"
  android:layout_marginRight="15dp"
  android:layout_marginTop="15dp"
  app:cardCornerRadius="5dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@color/colorCardBack">


        <TextView
            android:id="@+id/PinTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:fontFamily="sans-serif"
            android:text="Vanquis Credit"
            android:textColor="@color/colorAccent"
            android:textSize="25sp" />

        <TextView
            android:id="@+id/PinNumber"
            android:layout_width="wrap_content"
            android:layout_height="34dp"
            android:layout_alignBottom="@+id/PinTitle"
            android:layout_marginBottom="0dp"
            android:layout_marginEnd="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:fontFamily="sans-serif"
            android:letterSpacing="50"
            android:text="1234"
            android:textColor="@color/colorAccent"
            android:textSize="25sp" />


</LinearLayout>

RecycleAdapter 和 ViewHolder

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.PinViewHolder> {

    private List<PIN> pins;
    private LayoutInflater mInflator;
    private PinUtils pinUtils;
    private String passkey;

    RecyclerAdapter(Context context, List<PIN> data, String passkey) {
        Log.d("PinLog", "RecyclerAdapter: HERE");
        pins = data;
        mInflator = LayoutInflater.from(context);
        pinUtils = new PinUtils(context);
        this.passkey = passkey;
    }

    @NonNull
    @Override
    public PinViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Log.d("PinLog", "onCreateViewHolder: Created");
        View view = mInflator.inflate(R.layout.list_item, parent, false);
        PinViewHolder holder =  new PinViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull PinViewHolder holder, int position) {
        Log.d("PinLog", "onBindViewHolder: Created");

        PIN current = pins.get(position);
        holder.setData(current, position);
    }

    @Override
    public int getItemCount() {
        return pins.size();
    }

    class PinViewHolder extends RecyclerView.ViewHolder {

        TextView pinTitle, pinNumber;
        int position;
        PIN current;

        public PinViewHolder(View itemView) {
            super(itemView);

            pinTitle = (TextView) itemView.findViewById(R.id.PinTitle);
            pinNumber = (TextView) itemView.findViewById(R.id.PinNumber);

        }

        public void setData(PIN current, int position) {

            Log.d("PinLog", "setData: HERE");

            DecryptedPIN pin = pinUtils.DecryptPIN(current, passkey);

            Log.d("PinLog", "setData: PIN:" + pin.toString());

            this.pinTitle.setText(pin.getTitle());
            this.pinNumber.setText(pin.getPIN());
            this.position = position;
            this.current = current;
        }
    }
}

将 RecyclerView 的实例分配给 ActivityMain

private void setupRecyclerView() {

    RecyclerView rv = findViewById(R.id.pinRecyclerView);
    RecyclerAdapter ra = new RecyclerAdapter(this, db.getAll(), "1234");
    rv.setAdapter(ra);

    LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
    mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    rv.setLayoutManager(mLayoutManager);

    rv.setItemAnimator(new DefaultItemAnimator());

}

我已经记录了整个应用程序,以确保RecyclerView 的每个部分都被击中并且我有数据可以显示。我当前的数据库中有5 项目,因此应该显示5 卡片。这是登录getItemCount()

我认为该错误源于我的 list_item 布局,阅读了与此类似的问题,但我看不到问题。

与此相关的其他问题通常有更复杂的卡片,而我有一个带有 2 个嵌套 TextView 元素的布局。

谁能找出卡片不显示的原因?

编辑:

Logcat:

06-25 17:18:53.073 15818-15818/? E/Zygote: isWhitelistProcess - Process is Whitelisted
06-25 17:18:53.074 15818-15818/? W/SELinux: SELinux selinux_android_compute_policy_index : Policy Index[2],  Con:u:r:zygote:s0 RAM:SEPF_SM-G950F_8.0.0_0005, [-1 -1 -1 -1 0 1]
06-25 17:18:53.074 15818-15818/? I/SELinux: SELinux: seapp_context_lookup: seinfo=untrusted, level=s0:c512,c768, pkgname=com.ids.gould.pinnumbersafe 
06-25 17:18:53.080 15818-15818/? I/zygote64: Late-enabling -Xcheck:jni
06-25 17:18:53.166 15818-15818/? D/ActivityThread: Added TimaKeyStore provider
06-25 17:18:53.445 15818-15866/com.ids.gould.pinnumbersafe I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib64/egl/libGLES_mali.so from the current namespace instead.
06-25 17:18:53.451 15818-15818/com.ids.gould.pinnumbersafe I/InstantRun: starting instant run server: is main process
06-25 17:18:53.468 15818-15866/com.ids.gould.pinnumbersafe D/libEGL: loaded /vendor/lib64/egl/libGLES_mali.so
06-25 17:18:53.591 15818-15818/com.ids.gould.pinnumbersafe I/AppCompatViewInflater: app:theme is now deprecated. Please move to using android:theme instead.
06-25 17:18:53.755 15818-15899/com.ids.gould.pinnumbersafe D/OpenGLRenderer: HWUI GL Pipeline
06-25 17:18:53.763 15818-15818/com.ids.gould.pinnumbersafe D/ViewRootImpl@2ae3da5[MainActivity]: setView = DecorView@d96b57a[MainActivity] TM=true MM=false
06-25 17:18:53.772 15818-15818/com.ids.gould.pinnumbersafe V/InputMethodManager: Not IME target window, ignoring
06-25 17:18:53.774 15818-15818/com.ids.gould.pinnumbersafe D/ViewRootImpl@2ae3da5[MainActivity]: dispatchAttachedToWindow
06-25 17:18:53.797 15818-15818/com.ids.gould.pinnumbersafe V/Surface: sf_framedrop debug : 0x4f4c, game : false, logging : 0
06-25 17:18:53.799 15818-15818/com.ids.gould.pinnumbersafe D/ViewRootImpl@2ae3da5[MainActivity]: Relayout returned: old=[0,0][0,0] new=[0,0][1440,2960] result=0x7 surface={valid=true 507229466624} changed=true
06-25 17:18:53.807 15818-15899/com.ids.gould.pinnumbersafe I/OpenGLRenderer: Initialized EGL, version 1.4
06-25 17:18:53.807 15818-15899/com.ids.gould.pinnumbersafe D/OpenGLRenderer: Swap behavior 2
06-25 17:18:53.811 15818-15899/com.ids.gould.pinnumbersafe D/libGLESv1: STS_GLApi : DTS, ODTC are not allowed for Package : com.ids.gould.pinnumbersafe
06-25 17:18:53.840 15818-15899/com.ids.gould.pinnumbersafe D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, egl_color_buffer_format *, EGLBoolean) returns 0x3000,  [1440x2960]-format:1
06-25 17:18:53.840 15818-15899/com.ids.gould.pinnumbersafe D/OpenGLRenderer: eglCreateWindowSurface = 0x762101daa0
06-25 17:18:53.859 15818-15818/com.ids.gould.pinnumbersafe D/PinLog: setData: PIN:PIN Details: PIN Title: Vanquis Card, PIN Number: 1234
06-25 17:18:53.874 15818-15818/com.ids.gould.pinnumbersafe D/PinLog: setData: PIN:PIN Details: PIN Title: Vanquis Card, PIN Number: 1234
06-25 17:18:53.887 15818-15818/com.ids.gould.pinnumbersafe D/ViewRootImpl@2ae3da5[MainActivity]: MSG_RESIZED_REPORT: frame=Rect(0, 0 - 1440, 2960) ci=Rect(0, 96 - 0, 192) vi=Rect(0, 96 - 0, 192) or=1
06-25 17:18:53.888 15818-15818/com.ids.gould.pinnumbersafe D/ViewRootImpl@2ae3da5[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1
06-25 17:18:53.892 15818-15818/com.ids.gould.pinnumbersafe V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@7acebfc nm : com.ids.gould.pinnumbersafe ic=null
06-25 17:18:53.892 15818-15818/com.ids.gould.pinnumbersafe I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
06-25 17:18:54.087 15818-15818/com.ids.gould.pinnumbersafe V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@56f7285 nm : com.ids.gould.pinnumbersafe ic=null
06-25 17:18:54.112 15818-15829/com.ids.gould.pinnumbersafe I/zygote64: Do partial code cache collection, code=24KB, data=29KB
    After code cache collection, code=24KB, data=29KB
    Increasing code cache capacity to 128KB
06-25 17:18:54.269 15818-15829/com.ids.gould.pinnumbersafe I/zygote64: Do partial code cache collection, code=61KB, data=49KB
    After code cache collection, code=61KB, data=49KB
    Increasing code cache capacity to 256KB
06-25 17:18:57.115 15818-15829/com.ids.gould.pinnumbersafe I/zygote64: Do full code cache collection, code=120KB, data=86KB
06-25 17:18:57.116 15818-15829/com.ids.gould.pinnumbersafe I/zygote64: After code cache collection, code=108KB, data=69KB

更新的 LOGCAT:

06-25 17:46:46.394 3731-3731/com.ids.gould.pinnumbersafe D/PinLog: RecyclerAdapter: HERE
06-25 17:46:46.499 3731-3731/com.ids.gould.pinnumbersafe D/PinLog: onCreateViewHolder: Created
06-25 17:46:46.512 3731-3731/com.ids.gould.pinnumbersafe D/PinLog: onBindViewHolder: Created
    setData: HERE
06-25 17:46:46.515 3731-3731/com.ids.gould.pinnumbersafe D/PinLog: setData: PIN:PIN Details: PIN Title: Vanquis Card, PIN Number: 1234
06-25 17:46:46.523 3731-3731/com.ids.gould.pinnumbersafe D/PinLog: onCreateViewHolder: Created
06-25 17:46:46.529 3731-3731/com.ids.gould.pinnumbersafe D/PinLog: onBindViewHolder: Created
    setData: HERE
06-25 17:46:46.531 3731-3731/com.ids.gould.pinnumbersafe D/PinLog: setData: PIN:PIN Details: PIN Title: Vanquis Card, PIN Number: 1234

【问题讨论】:

  • 在 Recylerview 中设置 Layoutmanager 后的适配器
  • @RajeshRajendiran 不幸的是还是一样。
  • 你能发布Logcat吗
  • @RajeshRajendiran 代表哪一部分?全部还是?
  • 仅此应用程序日志

标签: android android-layout android-recyclerview android-cardview


【解决方案1】:

你能更新你的代码如下

您的活动

private void setupRecyclerView() {

RecyclerView rv = findViewById(R.id.pinRecyclerView);
RecyclerAdapter ra = new RecyclerAdapter(this, db.getAll(), "1234");

rv.setLayoutManager(new LinearLayoutManager(this));
rv.setItemAnimator(new DefaultItemAnimator());
rv.setAdapter(ra);

}

适配器

    @Override
public PinViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_item, parent, false);

    return new PinViewHolder(itemView);
}

 @Override
public void onBindViewHolder(@NonNull PinViewHolder holder, int position) {
    Log.d("PinLog", "onBindViewHolder: Created");

    PIN current = pins.get(position);

   DecryptedPIN pin = pinUtils.DecryptPIN(current, passkey);

   holder.pinTitle.setText(pin.getTitle());
   holder.pinNumber.setText(pin.getPIN());

}

列表项

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
app:cardCornerRadius="5dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorCardBack"
    android:orientation="horizontal">


    <TextView
        android:id="@+id/PinTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:fontFamily="sans-serif"
        android:text="Vanquis Credit"
        android:textColor="@color/colorAccent"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/PinNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/PinTitle"
        android:layout_marginEnd="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:fontFamily="sans-serif"
        android:text="1234"
        android:textColor="@color/colorAccent"
        android:textSize="25sp" />


</LinearLayout>

更新

如果您希望保留上面的剩余代码或恢复为初始代码,则必须在您的 activity_main.xml 中为您的 LinearLayout 添加一个方向,如下所示。

<LinearLayout 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"
tools:context=".MainActivity"

//Add line below
android:orientation="vertical"

android:background="@color/colorPrimaryDark">

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/pinRecyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v7.widget.RecyclerView>

【讨论】:

  • 仍然没有显示。就在您发表评论之前,我更新了 setData 方法来记录 xml 元素的文本,它似乎以这种方式工作。我的 XML 可以用于list_item 吗?我现在不知道要寻找什么
  • 我之前从一个数据库查询中获取列表项,我已将其切换为硬编码列表,但仍然是同样的问题。
  • 你可以试试更新的答案吗?我试过你的布局,那里没有错,但我也做了一些小改动
  • 我应该将代码上传到 github 存储库,以便您自己查看发生了什么吗?可能会更容易。
  • 好吧,我去看看
【解决方案2】:

将 list_item 中的 android:layout_width 从 match_parent 更改为 wrap_content,因为方向是水平的。

【讨论】:

  • 没有任何变化,在对list_item.xml 进行更改后仍将多个项目记录在数据库中。我在list_item 中匹配了最高的layout_width
【解决方案3】:

您需要首先将布局管理器设置为回收器视图,然后设置适配器,因为您通过构造函数传递数据...那时回收器视图没有布局管理器,所以您看不到..

 RecyclerView rv = findViewById(R.id.pinRecyclerView);
    LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
    mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    rv.setLayoutManager(mLayoutManager);
    rv.setItemAnimator(new DefaultItemAnimator());
    RecyclerAdapter ra = new RecyclerAdapter(this, db.getAll(), "1234");
    rv.setAdapter(ra);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多