为什么我总是在 View.OnClickListener 中看到最后一个 id?

他们杀

为什么无论我点击哪个文本,我总是看到最后一个 id?我希望看到根据单击的相关文本在我创建的布局中显示的正确片段。我总是看到“替代品”片段。我知道标签可以提供帮助,但我不知道如何使用它们。此外,我尝试使用不同版本的 FragmentManager 和 FragmentTransaction 甚至删除开关并分别调用 textview 的每个 setOnClickListener 但没有任何帮助。

这是我的活动文件:

        IngredientsFragment ingredientsFragment;
        FavouritesFragment favouritesFragment;
        FeedbacksFragment feedbacksFragment;
        Write_Feedback_Fragment writeFeedbackFragment;
        AlternativesFragment alternativesFragment;
        TextView ingredients;
        TextView favourites;
        TextView feedbacks;
        TextView alternatives;
        TextView writeFeedback;


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

            window=this.getWindow();
            window.setStatusBarColor(this.getResources().getColor(R.color.colorPrimaryDark));

            ingredients= (TextView) findViewById(R.id.ingredients_option);
            favourites= (TextView) findViewById(R.id.favorites_option);
            feedbacks= (TextView) findViewById(R.id.watch_feedback_option);
            writeFeedback= (TextView) findViewById(R.id.add_feedback_option);
            alternatives= (TextView) findViewById(R.id.alternatives_option);




 ingredients.setOnClickListener(this);
    favourites.setOnClickListener(this);
    feedbacks.setOnClickListener(this);
    writeFeedback.setOnClickListener(this);
    alternatives.setOnClickListener(this);



        }

        @Override
        public void onClick(View v) {

            switch (v.getId())
            {
                case R.id.ingredients_option:
                    ingredientsFragment= new IngredientsFragment();
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,ingredientsFragment).commit();
                    break;
                case R.id.favorites_option:
                    favouritesFragment= new FavouritesFragment();
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,favouritesFragment).commit();
                    break;
                case R.id.watch_feedback_option:
                    feedbacksFragment= new FeedbacksFragment();
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,feedbacksFragment).commit();
                    break;
                case R.id.add_feedback_option:
                    writeFeedbackFragment= new Write_Feedback_Fragment();
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,writeFeedbackFragment).commit();
                    break;
                case R.id.alternatives_option:
                    alternativesFragment= new AlternativesFragment();
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,alternativesFragment).commit();
                    break;
            }
        }

    }

这是我的 XML 文件:

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ProductDetails">

        <RelativeLayout
            android:id="@+id/recommendation_template"
            android:layout_width="match_parent"
            android:layout_height="142dp"
            android:background="@android:color/white">

            <ImageView
                android:id="@+id/registartion_arrow"
                android:layout_width="45dp"
                android:layout_height="34dp"
                android:layout_alignParentRight="true"
                android:src="@drawable/ic_arrow_forward_black_24dp" />

            <TextView
                android:id="@+id/rec_tv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:paddingTop="10dp"
                android:text="Recommendation:"
                android:textSize="25dp"
                android:textStyle="bold"
                android:textColor="@android:color/black"/>

            <ImageView
                android:id="@+id/vi_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/rec_tv"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="26dp"
                android:paddingLeft="80dp"
                android:src="@drawable/ic_check" />

            <TextView
                android:id="@+id/vi_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/vi_image"
                android:layout_marginLeft="4dp"
                android:layout_marginTop="-29dp"
                android:layout_toRightOf="@id/vi_image"
                android:text="This is for you"
                android:textColor="@android:color/black"
                android:textSize="20dp" />

            <ImageView
                android:id="@+id/not_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/vi_image"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="3dp"
                android:paddingLeft="80dp"
                android:src="@drawable/ic_do_not" />

            <TextView
                android:id="@+id/not_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/not_image"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="-27dp"
                android:layout_toRightOf="@id/not_image"
                android:text="Not for you"
                android:textColor="@android:color/black"
                android:textSize="20dp" />

        </RelativeLayout>

        <HorizontalScrollView
            android:id="@+id/recommendation_scrollView"
            android:layout_width="match_parent"
            android:layout_height="83dp"
            android:layout_below="@id/recommendation_template"
            android:layout_marginTop="7dp">


            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/recommendation_template"
                style="?android:attr/borderlessButtonStyle">

                <TextView
                    android:id="@+id/ingredients_option"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left"
                    android:drawableTop="@drawable/ingredients"
                    android:text="Ingredients" />

                <TextView
                    android:id="@+id/favorites_option"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left"
                    android:drawableTop="@drawable/ic_star"
                    android:paddingLeft="80dp"
                    android:text="Favorites" />

                <TextView
                    android:id="@+id/watch_feedback_option"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left"
                    android:drawableTop="@drawable/customer_review"
                    android:paddingLeft="145dp"
                    android:text="Feedbacks" />

                <TextView
                    android:id="@+id/add_feedback_option"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left"
                    android:drawableTop="@drawable/write_feedback"
                    android:paddingLeft="220dp"
                    android:text="Write feedback" />

                <TextView
                    android:id="@+id/alternatives_option"
                    android:layout_width="408dp"
                    android:layout_height="90dp"
                    android:layout_gravity="left"
                    android:drawableTop="@drawable/ic_alternatives"
                    android:paddingLeft="320dp"
                    android:text="Alternatives" />


            </FrameLayout>


        </HorizontalScrollView>

        <ScrollView
            android:id="@+id/scrollView_container"
            android:layout_width="match_parent"
            android:layout_height="499dp"
            android:layout_below="@id/recommendation_scrollView">

            <RelativeLayout
                android:id="@+id/fragment_container"
                android:layout_width="match_parent"
                android:layout_height="463dp">

            </RelativeLayout>


        </ScrollView>


    </RelativeLayout>
零机器人

您的代码应该像这样工作。问题是你的布局。查看 FrameLayout 中的文本视图。您定义了 paddingLeft 和 layout_width ,以便您的 textview 替代方案_option 覆盖所有其他文本视图。这就是为什么在 onclick 中你总是得到那个视图的 id。

我建议做一个关于 XML 布局的教程。

<TextView
    android:id="@+id/alternatives_option"
    android:layout_width="408dp"
    android:layout_height="90dp"
    android:layout_gravity="left"
    android:drawableTop="@drawable/ic_alternatives"
    android:paddingLeft="320dp"
    android:text="Alternatives" />

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我如何看到最后一个弹出窗口?

为什么我只能引用传递给onClickListener的TextView数组中的最后一个元素

为什么我总是在 SparkJava 中获得一个新会话?

使用 innerHTML 在 div 中打印时,我只看到最后一个选项

为什么“ _”不能总是在交互式shell中给我最后的结果

为什么我只能在 swift 5 的表视图中看到数组的最后一个元素?

为什么文本文件总是在我关闭它的同一时刻打开?

我得到最后一个索引的总和,但初始索引给我 0 为什么?

为什么Guid.NewGuid总是在完全相同的位置有一个4?

当我使用LayoutInflater时出现按钮View.OnClickListener

为什么我的子图只绘制到最后一个斧头?

为什么我的选择只得到最后一个值?

为什么我的二分搜索实现找不到最后一个元素?

什么叫Android View.OnClickListener API方法?

RecyclerView.OnClickListener与View.OnClickListener

Android-setOnClickListener与OnClickListener与View.OnClickListener

所有`id'总是来自最后一个对象

一个HashMap中,增加了一个新的元素到桶的内部链接列表的总是在最后。为什么?

当我尝试使用ajax发送一些变量时,为什么我总是在PHP响应中得到NULL

在Scala的“ Future”的ForkJoinPool中,为什么工人的ID总是一个奇数?

为什么在Postgres / Django中,模型ID并不总是下一个递增的数字?

为什么此de Bruijn代码总是在最后几位返回0

为什么我的列表视图总是删除列表中的最后一个视图

View.OnClickListener()函数或接口

为什么总是在服务层和dao层中只有一个实现接口?

为什么我的标头混淆了onClicklistener

为什么我的onClickListener应该双击?

为什么调试器总是在此程序中跳过一行?

到最后一个孩子的边框不起作用。为什么?