我创建了一个列表。在那里我显示费用金额,费用类型,费用类别和费用时间。
我使用了多个视图类型的RecyclerView
,对于视图类型1我显示的是一个小圆圈,而视图类型二则是一个大圆圈。
费用类型是显示两个不同的圆圈,一个是小时间,另一个是大日期。我都能看到。
但是现在我想在小圆圈上给出不同的颜色,即时间取决于时间。问题是我可以看到只有黄色的圆圈填充黄色,这是12:30并且显示时间,其他圆圈是空白的,没有颜色没有时间只有边框。
public class IAdapter extends RecyclerView.Adapter<IAdapter.ItemViewHolder> {
ArrayList<Expense> item;
public static final int TYPE1=1;
Context conext;
IAdapter(Context context,ArrayList<Expense> item) {
this.conext=context;
this.item=item;
}
@Override
public int getItemCount() {
return item.size();
}
@Override
public int getItemViewType(int position) {
return item.get(position).getExpenseType();// Assume that this return 1 0r 2
}
@Override
public void onBindViewHolder(ItemViewHolder itemViewHolder, int i) {
itemViewHolder.amount.setText(item.get(i).amount);
itemViewHolder.expense.setText(item.get(i).expense);
Expense expense = new Expense();
expense = item.get(i);
if(TYPE1==i) {
if(expense.getTime().equals("10:30")) {
TextDrawable drawable = TextDrawable.builder()
.beginConfig()
.withBorder(4)
.textColor(Color.BLACK)
.useFont(Typeface.DEFAULT)
.fontSize(25)
.bold()
.toUpperCase()
.endConfig()
.buildRound(expense.getTime(), Color.GREEN);
itemViewHolder.small.setImageDrawable(drawable);
}else if(expense.getTime()
.equals("12:30")) {
TextDrawable drawable = TextDrawable.builder()
.beginConfig()
.withBorder(4)
.textColor(Color.BLACK)
.useFont(Typeface.DEFAULT)
.fontSize(25)
.bold()
.toUpperCase()
.endConfig()
.buildRound(expense.getTime(), Color.YELLOW);
itemViewHolder.small.setImageDrawable(drawable);
}else if(expense.getTime().equals("4:30")) {
TextDrawable drawable = TextDrawable.builder()
.beginConfig()
.withBorder(4)
.textColor(Color.BLACK)
.useFont(Typeface.DEFAULT)
.fontSize(25)
.bold()
.toUpperCase()
.endConfig()
.buildRound(expense.getTime(), Color.BLUE);
itemViewHolder.small.setImageDrawable(drawable);
}else if(item.get(i).time.equals("7:30")){
TextDrawable drawable = TextDrawable.builder()
.beginConfig()
.withBorder(4)
.textColor(Color.BLACK)
.useFont(Typeface.DEFAULT)
.fontSize(25)
.bold()
.toUpperCase()
.endConfig()
.buildRound(expense.getTime(), Color.GRAY);
itemViewHolder.small.setImageDrawable(drawable);
}
}
}
@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);}
else{ big=(ImageView)itemView.findViewById(R.id.big);
TextDrawable drawable5 = TextDrawable.builder()
.beginConfig()
.withBorder(4)
.textColor(Color.BLACK)
.useFont(Typeface.DEFAULT)
.fontSize(25)
.bold()
.toUpperCase()
.endConfig()
.buildRound("10Jan", Color.CYAN);
big.setImageDrawable(drawable5);}
}
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
片段:
public class ItemFragment extends Fragment {
RecyclerView recyclerView;
IAdapter adapter;
ArrayList<Expense> items;
public ItemFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_list, container, false);
setHasOptionsMenu(true);
Expense e = new Expense();
recyclerView = (RecyclerView) view.findViewById(R.id.RecyclerView);
ImageButton imageButton = (ImageButton) view.findViewById(R.id.imgbtn_fab);
LinearLayoutManager llm = new LinearLayoutManager(this.getActivity());
items=new ArrayList<>();
recyclerView.setLayoutManager(llm);
recyclerView.setHasFixedSize(true);
initializeDataType1();
initRecyclerAdapter();
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), Main2Activity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("list", items);
intent.putExtras(bundle);
startActivityForResult(intent, 101);
}
});
return view;
}
private void initRecyclerAdapter() {
adapter = new IAdapter(getActivity(),items);
recyclerView.setAdapter(adapter);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 101 && resultCode == 102) {
Bundle bundle = data.getExtras();
Expense ex = (Expense) bundle.getSerializable("ex");
items.add(ex);
initRecyclerAdapter();
}
super.onActivityResult(requestCode, resultCode, data);
}
private void initializeDataType1() {
items.add(new Expense("1000", "2000", 1,"10:30"));
items.add(new Expense("2000", "5000", 1,"12:30"));
items.add(new Expense("3000", "400", 2));
items.add(new Expense("1000", "4000", 1,"4:30"));
items.add(new Expense("3000", "3000", 2));
items.add(new Expense("2000", "100", 1,"4:30"));
items.add(new Expense("2000", "3333", 2));
items.add(new Expense("3000", "shopping", 1,"7:30"));
items.add(new Expense("1000", "food", 1,"7:30"));
items.add(new Expense("1000", "food", 2));
items.add(new Expense("2000", "drink", 1,"7:30"));
items.add(new Expense("3000", "shopping", 2,"10:30"));
items.add(new Expense("2000", "3333", 1,"7:30"));
items.add(new Expense("3000", "shopping", 1,"7:30"));
items.add(new Expense("1000", "food", 1,"12:30"));
items.add(new Expense("1000", "food", 1,"7:30"));
items.add(new Expense("2000", "drink", 1,"10:30"));
items.add(new Expense("3000", "shopping", 1,"10:30"));
}
}
比较时间仅为12:30,其他人则被跳过。为什么会这样?
答案 0 :(得分:1)
你的状况
public void onBindViewHolder(ItemViewHolder itemViewHolder, int i)
:if(TYPE1==i)
仅在i==1
时有效。可能这不是你想要的。条件应如下:
if (TYPE1==getItemViewType(i))