3

So, I have the NavigationView with this structure:

<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" />

It's on the main_activity.xml, the problem is when I try to make a TextView inside the layout within the headerLayout "@layout/nav_header" that has this structure:

<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> 
    [...]

The problem is that I can't make it to set any Clickable action when clicking on the TextView "locationSettings" inside that layout.

    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);


}

Any idea why? By the way, I'm able to retrieve the getText from the TextView via Toast for example, so I'm referencing correctly the TextView, but being unable to set a clickable event...

Thanks.

4

1 に答える 1

1

app:headerLayout="@layout/nav_header" 次のようにプログラムでヘッダー ビューを削除して追加します。

View headerView = getLayoutInflater().inflate(R.layout.nav_header_main, navigationView, false);
navigationView.addHeaderView(headerView);
于 2016-05-14T13:15:17.680 に答える