我有一个列表视图。显示为卡片视图。在卡片视图中,我有一个图像,我想根据对象类型动态更改。因此,我创建了两个不同的布局,包含小图像和大图像。我已经创建了一个适配器并试图在onCreateViewHolder中膨胀两个不同的布局。我在这些代码行上得到一个Null指针异常。不知道我错过了什么......请帮助..
itemViewHolder.big.setImageDrawable(drawable1);
itemViewHolder.small.setImageDrawable(drawable);
public class IAdapter extends RecyclerView.Adapter<IAdapter.ItemViewHolder> {
适配器代码
public class ItemAdapter extends RecyclerView.Adapter<IAdapter.ItemViewHolder> {
List<Expense> items;
public static final int TYPE1 = 1;
public static final int TYPE2 = 2;
IAdapter(List<Expense> items) {
this.items = items;
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public int getItemViewType(int position) {
Expense e = new Expense();
if (TYPE1 == 1) {
int type = e.getExpenseType();
return type;
} else {
return TYPE2;
}
}
@Override
public void onBindViewHolder(ItemViewHolder itemViewHolder, int i) {
itemViewHolder.amount.setText(items.get(i).amount);
itemViewHolder.expense.setText(items.get(i).expense);
TextDrawable drawable = TextDrawable.builder()
.beginConfig()
.withBorder(4)
.textColor(Color.BLACK)
.useFont(Typeface.DEFAULT)
.fontSize(25)
.bold()
.toUpperCase()
.endConfig()
.buildRound("11:00", Color.GRAY);
itemViewHolder.small.setImageDrawable(drawable);
TextDrawable drawable1 = TextDrawable.builder()
.beginConfig()
.withBorder(4)
.textColor(Color.BLACK)
.useFont(Typeface.DEFAULT)
.fontSize(25)
.bold()
.toUpperCase()
.endConfig()
.buildRound("10 Jan", Color.CYAN);
itemViewHolder.big.setImageDrawable(drawable1);
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
if (viewType == TYPE1) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.cardlayout, viewGroup, false);
return new ItemViewHolder(itemView, viewType);
} else {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.bigcircle, viewGroup, false);
ItemViewHolder ivh = new ItemViewHolder(itemView, viewType);
return ivh;
}
}
public static class ItemViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView amount;
TextView expense;
ImageView small;
ImageView big;
ItemViewHolder(View itemView, int viewType) {
super(itemView);
amount = (TextView) itemView.findViewById(R.id.txtAmount);
expense = (TextView) itemView.findViewById(R.id.txtexpense);
cv = (CardView) itemView.findViewById(R.id.card_view);
small = (ImageView) itemView.findViewById(R.id.small);
big=(ImageView)itemView.findViewById(R.id.big);
// cv1 = (CardView) itemView.findViewById(R.id.card_view);
//TextDrawable drawable1 = TextDrawable.builder()
// .buildRound("20 Jan", Color.RED);
}
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
卡片布局代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="vertical"
android:measureWithLargestChild="false"
android:background="@android:color/white">
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="80dp"
card_view:cardCornerRadius="4dp"
android:background="@android:color/white">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relativeLayout"
android:background="@android:color/white"
android:layout_alignParentTop="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/txtAmount"
android:text="text1"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/linearLayout"
android:layout_toStartOf="@+id/linearLayout"
android:layout_marginRight="24dp"
android:layout_marginEnd="24dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:weightSum="1"
android:id="@+id/linearLayout"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="40dp"
android:layout_height="25dp"
android:id="@+id/imageView2"
android:background="@drawable/line" />
<ImageView
android:id="@+id/small"
android:focusableInTouchMode="false"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/circle"/>
<ImageView
android:layout_width="40dp"
android:layout_height="25dp"
android:id="@+id/imageView3"
android:background="@drawable/line" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/txtexpense"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/linearLayout"
android:layout_toEndOf="@+id/linearLayout"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
bigcircle卡布局代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="vertical"
android:measureWithLargestChild="false">
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view1"
android:layout_width="match_parent"
android:layout_height="80dp"
card_view:cardCornerRadius="4dp"
android:background="@android:color/white">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relativeLayout"
android:background="@android:color/white"
android:layout_alignParentTop="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/txtAmount"
android:text="text1"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/linearLayout"
android:layout_toStartOf="@+id/linearLayout"
android:layout_marginRight="24dp"
android:layout_marginEnd="24dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:weightSum="1"
android:id="@+id/linearLayout"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="50dp"
android:layout_height="25dp"
android:id="@+id/imageView2"
android:background="@drawable/line" />
<ImageView
android:id="@+id/big"
android:focusableInTouchMode="false"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/circle1"/>
<ImageView
android:layout_width="50dp"
android:layout_height="25dp"
android:id="@+id/imageView3"
android:background="@drawable/line" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/txtexpense"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/linearLayout"
android:layout_toEndOf="@+id/linearLayout"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
我缺少什么?
答案 0 :(得分:1)
这是因为您使用的是从ItemViewHolder
方法中的onCreateViewHolder
方法传递的onBindViewHolder
。它失败的原因是,您有2个不同的膨胀视图,其中一个包含标识为big
的视图元素,另一个标识为small
。因此,itemViewHolder.big或itemViewHolder.small变为null。这会引发NullPointerException
我认为你接近这个的方式是正确的。
选项一: 这是实现这一目标的另一种方式。
如果这两种布局完全不同,你必须这样做。但如果卡片的布局相同,只有那些大小图像都在变化,我建议您保留一个布局并用语法更改图像视图。
选项二
你的方法。我建议你坚持下去,因为这是做到这一点的优雅方式。但你必须做一些小的调整。
@Override
public int getItemViewType(int position) {
return items.get(position).getExpenseType();// Assume that this return 1 0r 2
}
我建议您在此处设置值之前检查类型,假设您有一个TYPE类变量来保留每个项目的类型。
@Override
public void onBindViewHolder(ItemViewHolder itemViewHolder, int i) {
itemViewHolder.amount.setText(items.get(i).amount);
itemViewHolder.expense.setText(items.get(i).expense);
if(getItemViewType(i) == 1){
TextDrawable drawable = TextDrawable.builder()
.beginConfig()
.withBorder(4)
.textColor(Color.BLACK)
.useFont(Typeface.DEFAULT)
.fontSize(25)
.bold()
.toUpperCase()
.endConfig()
.buildRound("11:00", Color.GRAY);
itemViewHolder.small.setImageDrawable(drawable);
}else{
TextDrawable drawable1 = TextDrawable.builder()
.beginConfig()
.withBorder(4)
.textColor(Color.BLACK)
.useFont(Typeface.DEFAULT)
.fontSize(25)
.bold()
.toUpperCase()
.endConfig()
.buildRound("10 Jan", Color.CYAN);
itemViewHolder.big.setImageDrawable(drawable1);
}
}
这并没有改变我的答案。但这会给你一个好主意。你缺少的腔体是这个,
public static class ItemViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView amount;
TextView expense;
ImageView small;
ImageView big;
ItemViewHolder(View itemView, int viewType) {
super(itemView);
amount = (TextView) itemView.findViewById(R.id.txtAmount);
expense = (TextView) itemView.findViewById(R.id.txtexpense);
cv = (CardView) itemView.findViewById(R.id.card_view);
small = (ImageView) itemView.findViewById(R.id.small);
big=(ImageView)itemView.findViewById(R.id.big);
// cv1 = (CardView) itemView.findViewById(R.id.card_view);
//TextDrawable drawable1 = TextDrawable.builder()
// .buildRound("20 Jan", Color.RED);
}
}
即使你做了
small = (ImageView) itemView.findViewById(R.id.small);
big=(ImageView)itemView.findViewById(R.id.big);
小变量或大变量将被分配null
。但是你必须在调用它之前检查它。否则它会给出NullPointerException
编辑:
或者你可以做到,
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup,int viewType) {
View itemView;
if (getItemViewType(i)==1) {
itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.cardlayout, viewGroup, false);
} else {
itemView = LayoutInflater.from(viewGroup.getContext()).
inflate(R.layout.bigcircle, viewGroup, false);
}
return new ItemViewHolder(itemView,viewType);
}
答案 1 :(得分:0)
@覆盖 public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup,int viewType){
if (getItemViewType(i)==1) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.cardlayout, viewGroup, false);
return new ItemViewHolder(itemView,viewType);
} else {
View itemView = LayoutInflater.from(viewGroup.getContext()).
inflate(R.layout.bigcircle, viewGroup, false);
ItemViewHolder ivh = new ItemViewHolder(itemView, viewType);
return ivh;
}
}
这是对的吗?
答案 2 :(得分:0)
public class IAdapter extends RecyclerView.Adapter<IAdapter.ItemViewHolder> {
List<Expense> items;
public static final int TYPE1=1;
IAdapter(List<Expense> items) {
this.items = items;
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public int getItemViewType(int position) {
return items.get(position).getExpenseType();// Assume that this return 1 0r 2
}
@Override
public void onBindViewHolder(ItemViewHolder itemViewHolder, int i) {
itemViewHolder.amount.setText(items.get(i).amount);
itemViewHolder.expense.setText(items.get(i).expense);
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup,int viewType) {
if (viewType==TYPE1) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.cardlayout, viewGroup, false);
return new ItemViewHolder(itemView,viewType);
} else {
View itemView = LayoutInflater.from(viewGroup.getContext()).
inflate(R.layout.bigcircle, viewGroup, false);
ItemViewHolder ivh = new ItemViewHolder(itemView, viewType);
return ivh;
}
}
public static class ItemViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView amount;
TextView expense;
ImageView small;
ImageView big;
ItemViewHolder(View itemView, int viewType) {
super(itemView);
amount = (TextView) itemView.findViewById(R.id.txtAmount);
expense = (TextView) itemView.findViewById(R.id.txtexpense);
cv = (CardView) itemView.findViewById(R.id.card_view);
if(viewType==TYPE1) {
small = (ImageView) itemView.findViewById(R.id.small);
TextDrawable drawable = TextDrawable.builder()
.beginConfig()
.withBorder(4)
.textColor(Color.BLACK)
.useFont(Typeface.DEFAULT)
.fontSize(25)
.bold()
.toUpperCase()
.endConfig()
.buildRound("11:00", Color.GRAY);
small.setImageDrawable(drawable);
}else{
big=(ImageView)itemView.findViewById(R.id.big);
TextDrawable drawable = TextDrawable.builder()
.beginConfig()
.withBorder(4)
.textColor(Color.BLACK)
.useFont(Typeface.DEFAULT)
.fontSize(25)
.bold()
.toUpperCase()
.endConfig()
.buildRound("10Jan", Color.CYAN);
big.setImageDrawable(drawable);
}
// cv1 = (CardView) itemView.findViewById(R.id.card_view);
//TextDrawable drawable1 = TextDrawable.builder()
// .buildRound("20 Jan", Color.RED);
}
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
这给出了期望的结果..