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

时间:2019-04-05 14:10:01

标签: java android

无论我单击哪个文本,为什么总是看到最后一个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>

1 个答案:

答案 0 :(得分:0)

您的代码应该这样工作。问题是您的布局。在FrameLayout中查看textviews。您定义paddingLeft和layout_width,以便您的textview Alternatives_option覆盖所有其他textview。这就是为什么在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" />