NavigationView头部布局中的onClick出错问题(Android)

3

所以,我有一个NavigationView,它的结构如下:

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"

    app:itemIconTint="@color/drawer_item"
    app:itemTextColor="@color/drawer_item"
    app:itemBackground="@drawable/drawer_item"

    app:headerLayout="@layout/nav_header"
    app:menu="@menu/nav_menu" />

这个问题出现在main_activity.xml上,当我试图在"@layout/nav_header"中的布局内创建一个TextView时,遇到了问题,它的结构如下:

<LinearLayout
    android:orientation="vertical"
    android:background="@drawable/mat_bg1"
    android:layout_height="@dimen/header_height"
    android:clickable="true"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:orientation="vertical"
        android:background="@color/background_floating_material_light"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:paddingTop="@dimen/header_top_location_padding"
        android:clickable="true"
        android:id="@+id/nav_location"
        android:paddingLeft="@dimen/header_left_padding"
        android:paddingBottom="@dimen/header_left_padding"
        android:paddingRight="@dimen/header_left_padding"
        >

        <TextView
            android:id="@+id/locationSettings"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:text="VALENCIA"
            android:layout_gravity="center_horizontal"
            android:foregroundGravity="center_horizontal"
            android:gravity="center_horizontal"
            android:textColor="@color/accentColor"/>



    </LinearLayout> 
    [...]

问题在于我无法使TextView“locationSettings”内部的任何可点击操作生效。
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(com.nite.R.layout.activity_main);

    setToolbar();

    drawerLayout = (DrawerLayout) findViewById(com.nite.R.id.drawer_layout);

    final NavigationView menu = (NavigationView) findViewById(com.nite.R.id.nav_view);

    if (menu != null) {
        setupDrawerContent(menu);
    }

    final View headerView = getLayoutInflater().inflate(R.layout.nav_header, menu, false);

    final TextView tv = (TextView) headerView.findViewById(R.id.locationSettings);

    tv.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //DO you work here
            Toast.makeText(getApplicationContext(), "Hey There",Toast.LENGTH_LONG);
            tv.setText("AAA");
        }

    });

        Fragment fragment = new HomeF();
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager
                .beginTransaction()
                .replace(com.nite.R.id.main_content, fragment)
                .commit();

        //drawerLayout.closeDrawers();
        setTitle(HomeF.ARG_SECTION_TITLE);


}

有什么想法吗?顺便说一下,我可以通过Toast等方式检索TextView的getText,所以我正确地引用了TextView,但是无法设置可点击事件...
谢谢。

你能把所有的 "clickable=true" 都去掉吗?TextView 不需要设置这个。当你设置监听器时,它将是可点击的。很少有理由设置这些,周围的视图可能会吞噬点击事件导致问题。 - HannahMitt
"Toast.makeText(getApplicationContext(), "Hey There",Toast.LENGTH_LONG);"不起作用,因为你忘记调用".show()"方法来实际显示提示。除此之外,我没有看到任何明显的原因,为什么它不起作用(是的,如上所述 - 删除"clickable"属性)。” - Konstantin Loginov
1个回答

1

删除 app:headerLayout="@layout/nav_header" 并通过以下方式以编程方式添加标题视图:

View headerView = getLayoutInflater().inflate(R.layout.nav_header_main, navigationView, false);
navigationView.addHeaderView(headerView);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接