所以我想创建这样的东西: example
像在创建按类别分开的项目列表一样(我有一个Item类,其中包含每个项目的类别)
我设法使用arrayAdapter来创建它,这是代码:
public class ItemAdapter {
ArrayList<Object> list;
private static final int ITEM = 0;
private static final int HEADER = 1;
private ArrayList<Category> categories;
private boolean whiteBg = true;
public ItemAdapter(Context context , ArrayList<Object> list) {
super(context , list);
this.list = list;
categories = new ArrayList<>();
}
@Override
public int getItemViewType(int position){
if(list.get(position) instanceof Item){
return ITEM;
}
else{
return HEADER;
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null){
switch (getItemViewType(i)){
case ITEM:
view = LayoutInflater.from(getContext()).inflate(R.layout.item_list_view, viewGroup, false);
break;
case HEADER:
view = LayoutInflater.from(getContext()).inflate(R.layout.item_list_view_header, viewGroup, false);
break;
}
}
switch (getItemViewType(i)){
case ITEM:
Item item = ((Item) list.get(i));
String category = item.getCategory();
TextView name = (TextView) view.findViewById(R.id.itemNameTextView);
TextView quantity = (TextView) view.findViewById(R.id.itemNameTextView2);
name.setText(item.getName());
quantity.setText("quantity: " + item.getQuantity());
if(whiteBg){
name.setBackgroundColor(Resources.getSystem().getColor(android.R.color.white));
quantity.setBackgroundColor(Resources.getSystem().getColor(android.R.color.white));
whiteBg = false;
}
else{
name.setBackgroundColor(Resources.getSystem().getColor(android.R.color.darker_gray));
quantity.setBackgroundColor(Resources.getSystem().getColor(android.R.color.darker_gray));
whiteBg = true;
}
break;
case HEADER:
TextView title = (TextView) view.findViewById(R.id.itemListViewHeader);
title.setText(((String)list.get(i)));
break;
}
return view;
}
@Override
public void remove(@Nullable Object object) {
super.remove(object);
if(object instanceof Item) {
categoryCheckDown(((Item) object).getCategory());
}
}
public boolean categoryExistsInc(String catName){
if(categories.size() == 0){
categories.add(new Category(catName , 1));
return false;
}
for(Category it: categories){
if(it.getName().equals(catName)){
it.amountUp();
return true;
}
}
categories.add(new Category(catName , 1));
return false;
}
public boolean categoryCheckDown(String catName){
for(Category it: categories){
if(it.getName().equals(catName)){
it.amountDown();
if(it.getAmount() == 0){
categories.remove(it);
super.remove(catName);
return true;
}
}
}
return false;
}
public boolean categoryExists(String catName){
if(categories.size() == 0){
return false;
}
for(Category it: categories) {
if (it.getName().equals(catName)) {
return true;
}
}
return false;
}
@Override
public void clear() {
super.clear();
categories.clear();
}
@Override
public void add(@Nullable Object object) {
if (object instanceof Item) {
super.add(object);
return;
}
else{
if(categoryExistsInc((String) object)){
return;
}
else{
super.add(object);
return;
}
}
}
但是我改变了应用程序设计,现在我使用recycleView,适配器是FirestoreRecyclerAdapter。该项目显示就很好,但我不知道如何添加类别。相关viewHolder的代码:
public class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
Item item;
Context context;
TextView name;
TextView quantity;
private View mView;
FragmentManager fragmentManager;
public ItemViewHolder(View itemView){
super(itemView);
this.name = itemView.findViewById(R.id.itemNameTextView);
this.quantity = itemView.findViewById(R.id.quantityTextView);
mView = itemView;
itemView.setOnClickListener(this);
}
public void setItem(Context context , String userEmail , ShoppingList shoppingList, Item item , FragmentManager fragmentManager){
String quantity = String.format("quantity: %d" , item.getQuantity());
this.context = context;
this.item = item;
this.name.setText(item.getName());
this.quantity.setText(quantity);
this.fragmentManager = fragmentManager;
}
@Override
public void onClick(View view) {
goToEdit();
Toast.makeText(context, "CLACK", Toast.LENGTH_SHORT).show();
}
private void goToEdit() {
EditFragment editFragment = new EditFragment();
Bundle bundle = new Bundle();
Item item = this.item;
bundle.putSerializable("item", item);
editFragment.setArguments(bundle);
fragmentManager.beginTransaction().add(R.id.drawer , editFragment , "frag2").addToBackStack("what").commit();
}
}
建议?我的猜测是要创建一个扩展FirestoreRecyclerAdapter的新适配器,并覆盖某些方法,就像我对第一个适配器所做的那样……但是我无法从文档中找出哪个。
答案 0 :(得分:-1)
科特林
override fun getItemViewType(position: Int): Int {
if(firebaseUser.uid == options.snapshots[position].sender){
return 0
}
return 1
}