代码之家  ›  专栏  ›  技术社区  ›  James Gould

卡片视图中不显示卡片

  •  1
  • James Gould  · 技术社区  · 7 年前

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

    这是我当前的代码:

    主活动.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>
    

    列表项.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>
    

    回收头和取景器

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

    我在整个应用程序中都进行了登录,以确保 回收服务 正在被击中,我有数据可供显示。我当前的数据库 5 其中的项目,因此 5个 应该出示卡片。已登录 getItemCount() 是的。

    我认为这个bug起源于我。 list_item 布局,读过类似的问题,但我看不出问题。

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

    有人能找出为什么卡片不显示吗?

    编辑:

    日志:

    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
    

    更新日志:

    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
    
    3 回复  |  直到 7 年前
        1
  •  2
  •   Ruan_Lopes    7 年前

    你能如下更新你的代码吗

    你单位

    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>
    

    更新

    如果您希望保留上述代码或返回到初始代码,则必须在Actuviy.My.xml中添加线性布局的方向,如下所示。

    <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
  •  0
  •   Ahmed El Saeed    7 年前

    更改Android:ListSub项目中的LayOutSub宽度从MatCHYPad到WrAPHY内容,因为方向是水平的。

        3
  •  0
  •   Gobu CSG    7 年前

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

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