Question

I created an app in which there are two fragments and in both fragments there are listviews. The first listview in the fragment1 is getting scrolled and the items are also getting highlighted. But in the second fragment, the listview is not scrolled and even the items are not getting highlighted. Can someone tell me what is the problem? The thing here is I just checked this with putting the same fragment class to both fragments in xml. Either they both should work or the both shouldn't because one is no different from the other. But why this problem occurs?

My fragment class:

public class Fragment1 extends ListFragment{

    String[] countries = new String[] {
        "India",
        "Pakistan",
        "Sri Lanka",
        "China",
        "Bangladesh",
        "Nepal",
        "Afghanistan",
        "North Korea",
        "South Korea",
        "Japan"
    };

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.fragment1,container,false);
    }
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,countries);
        setListAdapter(adapter);
    }

    public void onListItemClick(ListView parent, View v,int position, long id)
    {
        Toast.makeText(getActivity(), "You have selected "+countries[position], Toast.LENGTH_SHORT).show();
    }

}

main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <fragment
        android:name="com.example.listfragmentexample.Fragment1"
        android:id="@+id/fragment1"
        android:layout_weight="0.5"
        android:layout_width="0dp"
        android:layout_height="200dp" />

    <fragment 
        android:name="com.example.listfragmentexample.Fragment1"
        android:id="@+id/fragment2"
        android:layout_weight="0.5"
        android:layout_width="0dp"
        android:layout_height="300dp"/>
</LinearLayout>

fragment1.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:drawSelectorOnTop="false"/>

</LinearLayout>
Was it helpful?

Solution

So, as per your code it seems that you are referring the same Fragment1 class to both fragments in your main.xml. I assume that your activity class just contains the setContentView() in the onCreate() method. As the both fragments are on a single activity, there is a possibility that only one view gets highlighted initially. I just checked this, but it is working fine. Just drag the listview in the second fragment, rather you might have been scrolling it. If you want your second listview to get highlighted, I am afraid that you might need separate xml files( say fragment1 and fragment2) and separate classes for the fragments and get the focus on which ever you need first by adding the following code.

listView1 = (ListView)findViewById(R.id.listView1);
listView1.requestFocus();

good luck.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top