Android CardView未显示在RecyclerView中
我有一个简单的Android CardView未显示在RecyclerView中,android,android-layout,android-recyclerview,android-cardview,Android,Android Layout,Android Recyclerview,Android Cardview,我有一个简单的CardView在RecyclerView中,但我看不到任何卡片出现 以下是我当前的代码: 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:t
CardView
在RecyclerView
中,但我看不到任何卡片出现
以下是我当前的代码:
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>
<?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>
我在整个应用程序中都进行了登录,以确保RecyclerView
的每个部分都被点击,并且我有可用的数据显示。我当前的数据库中有5
项,因此应该显示5
卡。这将登录到getItemCount()
我认为这个错误源于我的列表项目布局,我读过类似的问题,但我看不出问题所在
与此相关的其他问题通常有更复杂的卡片,而我有一个包含两个嵌套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
将列表项中的android:layout_宽度从match_parent更改为wrap_内容,因为方向是水平的 您需要首先将布局管理器设置为recycler视图,然后设置适配器,因为您通过构造函数传递数据。。。那时候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);
你能更新你的代码如下吗
您的活动
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);
}
适配器
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;
}
}
}
@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 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>
在Recylerview设置适配器后Layoutmanager@RajeshRajendiran不幸的是,还是一样。你能发帖吗Logcat@RajeshRajendiran哪一部分?全部或仅此应用程序logNothing已更改,在更改到list\u item.xml
后,仍在数据库中记录多个项目。我已经匹配了列表项
中最顶部的布局宽度
。仍然没有显示任何内容。就在您发表评论之前,我更新了setData
方法来记录xml元素的文本,它似乎就是这样工作的。我的XML是否适用于列表项目
?我不知道现在要找什么我之前从数据库查询中得到了列表项,我已经切换到硬编码列表,仍然是同一个问题。你能试试更新后的答案吗?我试过你的布局,那里没有什么问题,但我也做了一些小改动。我应该把代码上传到github repo上,这样你就可以看到发生了什么?可能更容易。好吧,做吧,让我看看
<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>