Android Studio 水平回收器视图 OnClick - 多个类别,我该如何处理?

问题描述

我一直在为一个将使用水平回收器视图的应用程序做一个小项目。我已经被完全停止了很长时间,我似乎无法弄清楚我做错了什么。

我通常是游戏开发者 (C#) 或网站开发者 (PHP),应用程序和 Java 对我来说是一个新世界


发生什么事了?

当我点击我的回收商项目时,他们会按照我的意愿打开另一个活动 - 到目前为止一切顺利。

问题是,如果我转到第二行(或任何其他行),getAdapterPosition() 再次从 0 开始,我似乎无法从匹配的第二行中打开任何内容他们打算打开什么。

this is what is happening

所以我有多个类别,每个类别里面都有这样的东西

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

//java classes used
RecyclerView mainCategoryRecycler;
MainRecyclerAdapter mainRecyclerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String first= getString(R.string.first);
    String second= getString(R.string.second);
    String third= getString(R.string.third);

     // added in 1st category
     List<CategoryItem> categoryItemList = new ArrayList<>();
    categoryItemList.add(new CategoryItem(1,R.drawable.first));
    categoryItemList.add(new CategoryItem(2,R.drawable.second));
    categoryItemList.add(new CategoryItem(3,R.drawable.third));

    // added in second category
    List<CategoryItem> categoryItemList2 = new ArrayList<>();
    categoryItemList2.add(new CategoryItem(4,R.drawable.forth));
    categoryItemList2.add(new CategoryItem(5,R.drawable.fifth));

    // added in 3rd category
    List<CategoryItem> categoryItemList3 = new ArrayList<>();
    categoryItemList3.add(new CategoryItem(6,R.drawable.sixth));
    categoryItemList3.add(new CategoryItem(7,R.drawable.seventh));

    //Categories. Names taken from string.xml
    List<AllCategory> allCategoryList = new ArrayList<>();
    allCategoryList.add(new AllCategory(first,categoryItemList));
    allCategoryList.add(new AllCategory(second,categoryItemList2));
    allCategoryList.add(new AllCategory(third,categoryItemList3));

    //creates the recycler list
    setMainCategoryRecycler(allCategoryList);

private void setMainCategoryRecycler(List<AllCategory> allCategoryList){

    mainCategoryRecycler = findViewById(R.id.main_recycler);
    RecyclerView.LayoutManager layoutManager = new linearlayoutmanager(this);
    mainCategoryRecycler.setLayoutManager(layoutManager);
    mainRecyclerAdapter = new MainRecyclerAdapter(this,allCategoryList);
    mainCategoryRecycler.setAdapter(mainRecyclerAdapter);

}

@Override
public void onClick(View view) {


}

这些是我正在使用的模型

所有类别模型

public class AllCategory {

String categoryTitle;
List<CategoryItem> categoryItemList;

public AllCategory(String categoryTitle,List<CategoryItem> categoryItemList) {
    this.categoryTitle = categoryTitle;
    this.categoryItemList = categoryItemList;
}

public List<CategoryItem> getCategoryItemList() {
    return categoryItemList;
}

public void setCategoryItemList(List<CategoryItem> categoryItemList) {
    this.categoryItemList = categoryItemList;
}

public String getCategoryTitle() {
    return categoryTitle;
}

public void setCategoryTitle(String categoryTitle) {
    this.categoryTitle = categoryTitle;
}

类别项目模型

public class CategoryItem {


int itemId;
Integer imageUrl;

public CategoryItem(int itemId,Integer imageUrl) {
    this.itemId = itemId;
    this.imageUrl = imageUrl;
}

public int getItemId() {
    return itemId;
}

public void setItemId(int itemId) {
    this.itemId = itemId;
}

public Integer getimageUrl() {
    return imageUrl;
}

public void setimageUrl(Integer imageUrl) {
    this.imageUrl = imageUrl;
}

这是运行应用程序本身的代码

MainRecyclerAdapter 代码

public class MainRecyclerAdapter extends RecyclerView.Adapter<MainRecyclerAdapter.MainViewHolder> implements CategoryItemRecyclerAdapter.CategoryItemViewHolder.OnNoteListener {

private Context context;
private List<AllCategory> allCategoryList;

public MainRecyclerAdapter(Context context,List<AllCategory> allCategoryList) {
    this.context = context;
    this.allCategoryList = allCategoryList;
}

@NonNull
@Override
public MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {
    return new MainViewHolder(LayoutInflater.from(context).inflate(R.layout.main_recycler_row_item,parent,false));
}

@Override
public void onBindViewHolder(@NonNull MainViewHolder holder,int position) {

    holder.categoryTitle.setText(allCategoryList.get(position).getCategoryTitle());
    setCatItemRecycler(holder.itemRecycler,allCategoryList.get(position).getCategoryItemList());

}

@Override
public int getItemCount() {
    return allCategoryList.size();
}

@Override
public void onNoteClick(int position) {

    final Intent intent;

    switch(position)
    {
        case 1 :
            intent = new Intent(context,first_activity.class);
            context.startActivity(intent);
            break;

        case 2 :
            intent = new Intent(context,second_activity.class);
            context.startActivity(intent);
            break;

        case 3 :
            intent = new Intent(context,third_activity.class);
            context.startActivity(intent);
            break;

        case 4 :
            intent = new Intent(context,fourth_activity.class);
            context.startActivity(intent);
            break;

        case 5 :
            intent = new Intent(context,fifth_activity.class);
            context.startActivity(intent);
            break;

        case 6 :
            intent = new Intent(context,sixth_activity.class);
            context.startActivity(intent);
            break;

        case 7 :
            intent = new Intent(context,seventh_activity.class);
            context.startActivity(intent);
            break;
    }



}


public static final class MainViewHolder extends RecyclerView.ViewHolder{

    TextView categoryTitle;
    RecyclerView itemRecycler;

    public MainViewHolder(@NonNull View itemView) {
        super(itemView);

        categoryTitle = itemView.findViewById(R.id.cat_title);
        itemRecycler = itemView.findViewById(R.id.item_recycler);

    }
}

private void setCatItemRecycler(RecyclerView recyclerView,List<CategoryItem> categoryItemList){

    CategoryItemRecyclerAdapter itemRecyclerAdapter = new CategoryItemRecyclerAdapter(context,categoryItemList,this);
    recyclerView.setLayoutManager(new linearlayoutmanager(context,RecyclerView.HORIZONTAL,false));
    recyclerView.setAdapter(itemRecyclerAdapter);

}

CategoryItemRecyclerAdapter 代码

public class CategoryItemRecyclerAdapter extends RecyclerView.Adapter<CategoryItemRecyclerAdapter.CategoryItemViewHolder> {

private static Context context;
private List<CategoryItem> categoryItemList;
private CategoryItemViewHolder.OnNoteListener mOnNoteListener;

public CategoryItemRecyclerAdapter(Context context,List<CategoryItem> categoryItemList,CategoryItemViewHolder.OnNoteListener onNoteListener) {
    this.context = context;
    this.categoryItemList = categoryItemList;
    this.mOnNoteListener = onNoteListener;
}

@NonNull
@Override
public CategoryItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {
    return new CategoryItemViewHolder(LayoutInflater.from(context).inflate(R.layout.category_row_items,false),mOnNoteListener);
}

@Override
public void onBindViewHolder(@NonNull CategoryItemViewHolder holder,int position) {

    holder.itemImage.setimageResource(categoryItemList.get(position).getimageUrl());
}

@Override
public int getItemCount() {
    return categoryItemList.size();
}

public static final class CategoryItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    ImageView itemImage;
    OnNoteListener onNoteListener;

    public CategoryItemViewHolder(@NonNull View itemView,OnNoteListener onNoteListener) {
        super(itemView);

        itemImage = itemView.findViewById(R.id.item_image);

        this.onNoteListener = onNoteListener;
        itemView.setonClickListener(this);

    }

    @Override
    public void onClick(View view) {

        onNoteListener.onNoteClick(getAdapterPosition());

        //debug show position
        Toast.makeText(context,String.valueOf(getAdapterPosition()),Toast.LENGTH_SHORT).show();

    }

    public interface OnNoteListener {
        void onNoteClick(int position);
    }
}

上面代码中的最后 2 个方法 (CategoryItemRecyclerAdapter) 似乎正在发挥所有作用,而这 2 个方法在我调整它们时会改变我的应用程序的功能。 >

然后将它传递给 MainRecyclerAdapter 到 switch 语句中,从这里我的意图是根据我的需要在何处显示活动。

事情是不是工作。第一行就像一个魅力,但如果我在第一行添加更多的东西,它就会开始打开错误的东西。

因此,如果我在第一行有 3 个而我添加了第 4 个,它会开始打开错误的东西,因为开关中的第 4 个是别的东西(如果你明白我的意思,有点难以解释)。

如果有人能帮助我,我会非常非常感谢,因为这让我很困惑,不知道我浏览了多少论坛、YouTube 视频和谷歌搜索

如果有人愿意帮助我,我愿意分享我的这个小项目的 Github Repo。

如果有更好的方法解决这个问题,也请分享。谢谢。

亲切的问候

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)