0

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

user2661518
  • 2,677
  • 9
  • 42
  • 79

3 Answers3

0

The simple answer is: you can't.

Any fragments you define in the layout file can't be replaced. You can only replace fragments that were added dynamically, i.e. created through a FragmentTransaction.

  • I created it as getFragmentManager().beginTransaction().add...Is this not the right way? – user2661518 Apr 06 '15 at 21:13
  • It's the right way if you're going to use the fragment tag from your layout file. You don't actually *need* to do it that way. ...with this in your Fragment's class... @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.your_fragments_layout_file, container, false); } You merely dump the tag from your layout, assign an id to the RelativeLayout that was it's parent, and then call add() on that id. – Dagmar d'Surreal Apr 06 '15 at 21:36
0

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>
user2661518
  • 2,677
  • 9
  • 42
  • 79
0

Look at code sample @ Google webpage Fragments. Search text code for "public static class DetailsFragment extends Fragment". You may override method onCreateView. It seems you have a problem with the View.

The Original Android
  • 6,147
  • 3
  • 26
  • 31