【发布时间】:2019-03-20 07:18:29
【问题描述】:
我曾经有一个显示 recyclerView 的主要活动,它运行良好。然后,我决定使用 recyclerView 而不仅仅是 recyclerView 创建一个片段列表,以允许在列表中显示一些很酷的项目。
适配器类
public class BeersAdapter extends RecyclerView.Adapter<BeersAdapter.BeersViewHolder> {
private List<Beer> beers;
public static class BeersViewHolder extends RecyclerView.ViewHolder {
public ImageView beerImage;
public TextView beerName;
public TextView beerDesc;
public BeersViewHolder (View itemView){
super(itemView);
beerImage = itemView.findViewById(R.id.beerImageLayout);
beerName = itemView.findViewById(R.id.beerNameLayout);
beerDesc = itemView.findViewById(R.id.beerDescLayout);
}
}
public BeersAdapter(List<Beer> b){
beers = b;
}
@Override
public BeersAdapter.BeersViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.beer, parent,false);
return new BeersViewHolder(itemView);
}
@Override
public void onBindViewHolder(BeersViewHolder holder, int pos) {
holder.beerName.setText(beers.get(pos).getName());
holder.beerDesc.setText(beers.get(pos).getShortDescription());
}
@Override
public int getItemCount() {
return beers.size();
}
}
片段类中的onCreateView
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_mainbeer_list, container, false);
RecyclerView rV = view.findViewById(R.id.BeerRV);
rV.setHasFixedSize(true);
rV.setLayoutManager(new LinearLayoutManager(getActivity()));
beers = Beer.getDummyBeers();
BeersAdapter mAdapter = new BeersAdapter(beers);
rV.setAdapter(mAdapter);
rV.setItemAnimator(new DefaultItemAnimator());
return view;
}
我知道如果我 return view = inflater.inflate(R.layout.fragment_mainbeer_list, container, false); 未使用它不会给出错误,但也没有任何改变。我的意思是我可以在 onCreateView 上运行代码,但是如果我返回我使用的视图(据我所知,它不应该被更改)它会导致错误。
我在运行调试器时遇到的错误
java.lang.NullPointerException:尝试调用虚拟方法 '布尔值 android.support.v7.widget.RecyclerView$ViewHolder.shouldIgnore()' 空对象引用
编辑: 我知道 NullPointer 是什么。我只是不知道为什么我得到它。因为我的应用程序运行正常,顺序相同,唯一不同的是我现在使用的是片段,并在片段而不是 MainActivity 中创建 RV。基于this post
编辑 2:
fragment_mainbeer_list.xml 的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/BeerRV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
对于片段所在的main_activy.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<fragment
android:id="@+id/ListFragment"
android:name="com.example.beers_fragmnet.MainBeerFragment"
android:layout_width="215dp"
android:layout_height="328dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:layout="@layout/beer" />
</android.support.constraint.ConstraintLayout>
附加信息
该应用程序仅显示啤酒列表,在单击啤酒时尝试添加片段之前,它会打开详细活动并显示有关啤酒的信息。
我现在想让它在平板电脑上的工作方式与许多电子邮件应用程序的工作方式相同,当您单击电子邮件时,它不会打开新活动,而是在同一活动中打开电子邮件(以防此处出现错误解释是我想说的photo)
【问题讨论】:
-
在将 Adapter 设置为 RecyclerView 后尝试移动
rV.setHasFixedSize(true);。 -
@Zoe 我可以看到这是一个 NullPointerException,我知道这意味着什么(我是 C/C++ 的主要程序)。我只是不知道为什么会得到它,尤其是当
onCreateView中的相同代码适用于 MainActivty 但现在抛出 NullPointer 时。我显然错过了一些东西,我花了几个小时试图看看是否有人有类似的问题,但我找不到任何东西,所以我在这里 -
@ישואוהבאותך 我试着移动它,并把它注释掉(因为这段代码只是为了提高性能,所以它可以计算 RV 的最大尺寸)。不过谢谢你的建议
标签: android android-fragments android-recyclerview