gpt4 book ai didi

java - 如何膨胀多张卡片 View 布局

转载 作者:行者123 更新时间:2023-11-30 10:58:24 25 4
gpt4 key购买 nike

我有一个 ListView 。显示为卡片 View 。在卡片 View 中,我有一个图像,我想根据对象类型动态更改它。因此,为此我创建了两种不同的布局,其中包含小图像和大图像。我已经创建了一个适配器并尝试在 onCreateViewHolder 中膨胀两个不同的布局。我在这些代码行上遇到空指针异常。不知道我错过了什么......请帮助..

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>

大圆卡布局代码

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

我错过了什么???

最佳答案

这是因为您在 onBindViewHolder 方法中使用了从 onCreateViewHolder 方法传递过来的 ItemViewHolder。它失败的原因是,您传递了 2 个不同的膨胀 View ,一个包含 id big 的 View 元素,另一个包含 id small 的 View 元素。因此,itemViewHolder.big 或 itemViewHolder.small 变为空。抛出一个 NullPointerException

我认为您处理此问题的方式是正确的。

选项一:这是一种不同的实现方式。

如果你对这两个有完全不同的布局,你必须这样做。但是,如果卡片的布局相同,只是那些大图像和小图像发生了变化,我建议您保持单一布局并在语法上更改 ImageView 。

方案二

你的方法。我建议你坚持下去,因为这是做到这一点的优雅方式。但是你将不得不做一些小的调整。

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

small 或 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);
}

关于java - 如何膨胀多张卡片 View 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32277124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com