gpt4 book ai didi

java - 处理 RecyclerView 中的多种类型 View 单击

转载 作者:行者123 更新时间:2023-12-02 02:26:44 24 4
gpt4 key购买 nike

RecyclerView中显示的数据分为3部分: 1. 产品 2. 食谱 3. Collection

当我尝试在一个 RecyclerView 中显示多个 View 类型时。是的,这是工作。它可以显示所有这些。但是当我尝试根据其位置处理它们的点击列表时,我遇到了一些困难。它总是显示 java.lang.IndexOutOfBoundsException。

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

List<BaseModel> baseModels = new ArrayList<>();

Product product = new Product();
product.setTitle("BBQ Chicken");
Product product2 = new Product();
product2.setTitle("BBQ Chicken 2");
baseModels.add(product);
baseModels.add(product2);

Recipe recipe = new Recipe();
recipe.setTitle("Pulled Pork");
Recipe recipe2 = new Recipe();
recipe2.setTitle("Pulled Pork 2");
baseModels.add(recipe);
baseModels.add(recipe2);

Collection collection = new Collection();
collection.setTitle("Recipe Collection");
Collection collection2 = new Collection();
collection2.setTitle("Recipe Collection 2");
baseModels.add(collection);
baseModels.add(collection2);

RecyclerView list = (RecyclerView) findViewById(R.id.list);
list.setLayoutManager(new LinearLayoutManager(this));
list.setAdapter(new InventoryAdapter(baseModels, this));

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

InventoryAdapter.java

public class InventoryAdapter extends RecyclerView.Adapter<BaseViewHolder> {

private List<? extends BaseModel> mList;
private LayoutInflater mInflator;

public InventoryAdapter(List<? extends BaseModel> list, Context context) {
this.mList = list;
this.mInflator = LayoutInflater.from(context);
}

public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case ViewType.COLLECTION_TYPE:
return new CollectionHolder(mInflator.inflate(R.layout.type_collection, parent,false));
case ViewType.PRODUCT:
return new ProductHolder(mInflator.inflate(R.layout.type_product, parent,false));
case ViewType.RECIPE_TYPE:
return new RecipeHolder(mInflator.inflate(R.layout.type_recipe, parent,false));
}
return null;
}

@Override
@SuppressWarnings("unchecked")
public void onBindViewHolder(BaseViewHolder holder, int position) {
holder.bind(mList.get(position));
}


@Override
public int getItemViewType(int position) {
return mList.get(position).getViewType();
}

public int getItemCount() {
return mList.size();
}

public static class CollectionHolder extends BaseViewHolder<Collection> {
private TextView mItem;

public CollectionHolder(View itemView) {
super(itemView);
mItem = (TextView) itemView.findViewById(R.id.item);

itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context c = v.getContext();
Toast.makeText(c, "Dddd", Toast.LENGTH_SHORT).show();
}
});
}

@Override
public void bind(Collection object) {
mItem.setText(object.getTitle());
}
}

public static class RecipeHolder extends BaseViewHolder<Recipe> {
private TextView mItem;
private List<Recipe> recipeList = new ArrayList<>();

public RecipeHolder(View itemView) {
super(itemView);
mItem = (TextView) itemView.findViewById(R.id.item);

itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context c = v.getContext();
int position = getAdapterPosition();
Recipe click = recipeList.get(position);
int id = click.getId();

if (id == 0) {
Toast.makeText(c, "Selected: " + click.getTitle(), Toast.LENGTH_LONG).show();
}
else if(id == 1) {
Toast.makeText(c, "Selected: " + click.getTitle(), Toast.LENGTH_LONG).show();
}
}
});
}

@Override
public void bind(Recipe object) {
mItem.setText(object.getTitle());
}
}


public static class ProductHolder extends BaseViewHolder<Product> {
private TextView mItem;
private List<Product> storyList = new ArrayList<>();

public ProductHolder(View itemView) {
super(itemView);
mItem = (TextView) itemView.findViewById(R.id.item);

itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context c = v.getContext();
int position = getAdapterPosition();
Product click = storyList.get(position);
int id = click.getId();

if (id == 0) {
Toast.makeText(c, "Selected: " + click.getTitle(), Toast.LENGTH_LONG).show();
}
else if(id == 1) {
Toast.makeText(c, "Selected: " + click.getTitle(), Toast.LENGTH_LONG).show();
}
}
});
}

@Override
public void bind(Product object) {
mItem.setText(object.getTitle());
}
}


}

Logcat

2019-07-29 14:12:07.329 17223-17223/com.pollux.recyclerviewbinding E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.pollux.recyclerviewbinding, PID: 17223
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.get(ArrayList.java:437)
at com.pollux.recyclerviewbinding.InventoryAdapter$ProductHolder$1.onClick(InventoryAdapter.java:122)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
```

最佳答案

首先:你应该在OnBindViewHolder方法中处理onClick

第二:最好使用这样的界面:

public interface OnRecyclerItemClick{
void onRecyclerItemClick(int position)
}

并在 OnBindViewHolder 中使用它,如下所示:

 @Override
@SuppressWarnings("unchecked")
public void onBindViewHolder(BaseViewHolder holder, int position) {
holder.bind(mList.get(position));
listener.onRecyclerItemClick(position);
}

其中 listener 是一个 OnRecyclerItemClick,您可以在构造函数中初始化它或setter 方法:

public void setOnRecyclerItemClick(OnRecyclerItemClick listener){
this.listener = listener;
}

关于java - 处理 RecyclerView 中的多种类型 View 单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57248831/

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