如何从 1 个 recyclerview 片段移动到其自己的活动?

发布于 2025-01-12 20:38:34 字数 3264 浏览 1 评论 0原文

我当前的回收器视图有 2 个片段。我使用了相对布局,为适配器使用的 xml 文件中的每个片段封装了 2 个文本视图。

我现在的问题是这样的;我如何做到这样,当我单击每个片段时,我可以移动到其独特的位置。就像选项 1 ->屏幕 1(活动),选项 2 ->屏幕 2(活动 2)。

请有人帮助并逐步向我解释。我四天前刚刚开始学习 Android 应用程序开发。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLay"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="20dp">


        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/black" />

        <TextView
            android:id="@+id/nuts"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Nutrients" />

        <TextView
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/nuts"
            android:text="Description" />


</RelativeLayout>

上面是我用于适配器的 xml 文件。该视图仅用于行分隔符。

 public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.food_names, parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.names.setText(info.get(position).getName());
        holder.desc.setText(info.get(position).getDescription());
        holder.parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "Hello", Toast.LENGTH_SHORT).show();
            }
        });

    }
 public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView names, desc;
        private RelativeLayout parent;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            parent = itemView.findViewById(R.id.relativeLay);

            names = itemView.findViewById(R.id.nuts);
            desc = itemView.findViewById(R.id.description);

        }
    }

目前,这是我的适配器方法,没有发布构造函数(它只需要上下文)和 getItemCount() 方法,因为我认为它们不会影响太大?如果他们让我知道,我也会发布它们。

如您所见,由于我使用 onClickListener 设置了整个相对布局(父级),因此一旦我单击它,它就会显示 toast 消息(这是出于尝试目的)。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nutrient_source);
        recyclerViewNuts = findViewById(R.id.recyclerViewNuts);

        ArrayList<Information> info = new ArrayList<>();
        info.add(new Information("Protein", "Makes you strong!"));
        info.add(new Information("Carbohydrates", "Provides Glycogen for energy"));

        nutsAdapter adapter = new nutsAdapter(this);
        adapter.setInfo(info);

        recyclerViewNuts.setAdapter(adapter);
        recyclerViewNuts.setLayoutManager(new LinearLayoutManager(this));

这就是这些相对布局所在的活动。我有 2 个选择,蛋白质和碳水化合物。当我按下蛋白质时,我需要它转到其蛋白质相关页面,将碳水化合物转到其自己的碳水化合物相关页面。

My current recycler view has 2 fragments. i used a relative layout encapsulating 2 textviews for each fragment within the xml file used by the adapter.

My problem now is this; how do i make it such that when i click on each fragment, i can move to its unique location. Like from option 1 -> screen 1(activity), option 2 -> screen 2(activity 2).

Someone please help and explain to me step by step. i just started learning android app development 4 days ago.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLay"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="20dp">


        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/black" />

        <TextView
            android:id="@+id/nuts"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Nutrients" />

        <TextView
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/nuts"
            android:text="Description" />


</RelativeLayout>

The above is the xml filed i used for the adapter. The view is just for a line separator.

 public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.food_names, parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.names.setText(info.get(position).getName());
        holder.desc.setText(info.get(position).getDescription());
        holder.parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "Hello", Toast.LENGTH_SHORT).show();
            }
        });

    }
 public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView names, desc;
        private RelativeLayout parent;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            parent = itemView.findViewById(R.id.relativeLay);

            names = itemView.findViewById(R.id.nuts);
            desc = itemView.findViewById(R.id.description);

        }
    }

Currently, this are my adapter methods, did not post the constructor(it just takes in context) and getItemCount() method since i think they wont affect much? If they would then let me know, i would post them too.

As you can see, since i set the entire relativelayout(parent) with an onClickListener, once i click on it, it shows the toast message(this is for trying purposes).

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nutrient_source);
        recyclerViewNuts = findViewById(R.id.recyclerViewNuts);

        ArrayList<Information> info = new ArrayList<>();
        info.add(new Information("Protein", "Makes you strong!"));
        info.add(new Information("Carbohydrates", "Provides Glycogen for energy"));

        nutsAdapter adapter = new nutsAdapter(this);
        adapter.setInfo(info);

        recyclerViewNuts.setAdapter(adapter);
        recyclerViewNuts.setLayoutManager(new LinearLayoutManager(this));

This is the activity in which these relativelayouts reside in. i have 2 options, protein and carbs. When i press on protein, i need it to go to its protein related page, and carbs to its own carb related page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杀手六號 2025-01-19 20:38:34

好吧,我设法找到了方法。对于那些好奇或有类似问题的人,我将recyclerview的位置传递给一个int变量,并编写了IF语句。

if(position == 0){
//go to activity1
}
if(position ==1){
//go to activity2
}

Alright i managed to find a way. For those who are curious or have similar problems, i passed the position of the recyclerview into a int variable, and wrote IF statements.

if(position == 0){
//go to activity1
}
if(position ==1){
//go to activity2
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文