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

如何解决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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?