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

标签 java android

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

这是我的 Activity 文件:

        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 中的 TextView 。您定义了 paddingLeft 和layout_width,以便您的 TextView alternatives_option 覆盖所有其他 TextView 。这就是为什么在 onclick 中你总是能得到该 View 的 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" />

关于java - 为什么我总是在 View.OnClickListener 中看到最后一个 id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55537281/

相关文章:

java - Dynamic Code Evolution VM 崩溃

java - 如何用Java生成透明的GIF?

android - 将数据列表推送到 firebase 不起作用

android - 将项目迁移到 Android Studio 时显示的旧式 Android 菜单

java - 获取用户输入中所有可能的奇数

java - 实现多项 Activity 的最佳方式是什么?

java - Checkmarx:在 Java 中使用 Mybatis resultMap 进行不正确的异常处理

android - 广播接收器双卡

java - 进度条 setVisibility VISIBLE

android - 设置 AsyncHttpClient 以使用 HTTPS