Search code examples
androidandroid-fragmentsfragmentandroid-fragmentactivityandroid-listfragment

How to replace current fragment view with another fragment view


I have MainActivity as

listViewFragment = new ListViewFragment();

    getFragmentManager().beginTransaction().add(R.id.fragmentSensors, listViewFragment).commit();

Then I have ListViewFragment and ListViewAdapter class defined which works fine.

 listViewSensors.setAdapter(new ListViewAdapter(getActivity(), sensorArray));

But when I define onClicklistener current view is not getting replaced by new fragment view.

Here are layout files. activity_main

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

    <fragment android:id="@+id/fragmentSensors" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.testapp.fragment.ListViewFragment"/>

</RelativeLayout>

listview_fragment

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"  tools:context=".MainActivity">

    <ListView android:id="@+id/listViewSensors" android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>

listview_adapter has imageView and textView.

And then I defined test_fragment as

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Hello Test"
        android:id="@+id/textViewTest"/>

</LinearLayout>

and called it through ListViewFragment as

@Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        accelFragment = new AccelFragment();
        testFragment = new TestFragment();

        getFragmentManager().beginTransaction().replace(R.id.fragmentSensors, testFragment).commit();


        Toast.makeText(getActivity(),"Ok", Toast.LENGTH_SHORT).show();
    }

But nothing happens, I see Toast pop-up though. If I use android.R.id.content. I see new text on top of current listView items. If I use R.id.listViewSensors I get error java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView


Solution

  • OnClickListener does not fire if you have focusable items inside view. I had to add android:focusable="false" in activity_main as below.

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent"  tools:context=".MainActivity"
        android:layout_weight="1"
        >
    
        <RelativeLayout android:id="@+id/fragmentSensors" android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.skakade.testsensorapp.fragment.ListViewFragment"
            android:focusable="false"/>
    
    </RelativeLayout>