Cards not shows in CardView

六月ゝ 毕业季﹏ 提交于 2020-01-21 10:12:30

问题


I have a simple CardView inside a RecyclerView but I can't see any cards appearing.

Here's my current code:

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 and 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;
        }
    }
}

Assigning instance of RecyclerView to 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());

}

I've logged throughout the application to make sure that each part of the RecyclerView is being hit and I have data available to display. My current database has 5 items in it, thus 5 cards should be shown. This is logged in getItemCount().

I think the bug originates in my list_item layout, having read questions similar to this one, but I can't see the issue.

Other questions relating to this typically have more complex cards, whereas I have a single layout with 2 nested TextView elements.

Can anybody spot why the cards aren't displaying?

EDIT:

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

UPDATED 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

回答1:


Could you update your code as below

Your Activity

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);

}

Adapter

    @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());

}

List item

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

Update

You have to add an orientation for your LinearLayout in your activity_main.xml as below if you want you the keep the remaing code as above or revert to your initial code.

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




回答2:


change android:layout_width in list_item from match_parent to wrap_content, because orientation is horizontal.




回答3:


Your need first set layout manager to recycler view , then set adapter because u passing data via constructor... that time recyclerview don't have layout manager so u can't see..

 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);


来源:https://stackoverflow.com/questions/51027599/cards-not-shows-in-cardview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!