我在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>
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
布局,已经阅读了与此类似的问题,但我看不到该问题。
与此相关的其他问题通常具有较复杂的卡片,而我的布局只有两个嵌套的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
更新日志:
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
答案 0 :(得分:2)
您可以按以下方式更新代码
您的活动
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>
答案 1 :(得分:0)
将list_item中的android:layout_width从match_parent更改为wrap_content,因为方向是水平的。
答案 2 :(得分:0)
您首先需要将布局管理器设置为recycler view,然后设置适配器,因为您通过构造函数传递了数据...那时候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);