Android + ListFragment with a custom view hierarchy
Asked Answered
S

5

17

I'm attempting to customize the fragment layout by returning my own view hierarchy from onCreateView(LayoutInflater, ViewGroup, Bundle). This inflates my custom view, but seems to stack the views instead of inflating within, I see everything at once. Any help is appreciated.

MyActivity.java:

public class MyActivity extends FragmentActivity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
            ArrayListFragment list = new ArrayListFragment();
            getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit();
        }
    }

    public static class ArrayListFragment extends ListFragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            inflater.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
            inflater.inflate(R.layout.main, container);
            return super.onCreateView(inflater, container, savedInstanceState);
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            String List[] = {"Larry", "Moe", "Curly"}; 
            setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, List));
        }
    }
}

main.xml

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

  <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="THIS IS A BUTTON" />

  <ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00FF00"
    android:layout_weight="1"
    android:drawSelectorOnTop="false" />

  <TextView
    android:id="@android:id/empty"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF0000"
    android:text="No data" />
</LinearLayout>
Sheeree answered 23/9, 2011 at 19:34 Comment(1)
Also to note, I'm using android-support-v4. From the android docs, it clearly states: "ListFragment has a default layout that consists of a single list view. However, if you desire, you can customize the fragment layout by returning your own view hierarchy from onCreateView(LayoutInflater, ViewGroup, Bundle). To do this, your view hierarchy must contain a ListView object with the id "@android:id/list" (or list if it's in code)."Sheeree
S
34

I wasn't returning the new view within the onCreateView method of the ListFragment class. For instance:

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
            View view = inflater.inflate(R.layout.main, null);
            return view;
        }

All works well now in the land of Android!

Sheeree answered 24/9, 2011 at 23:50 Comment(2)
The fist line in your onCreateView isn't necessary (it's not actually doing anything...) The inflater is already passed in (which you use).Valentinevalentino
Could you give more details?Biggin
D
2

I think you'd better

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle  savedInstanceState) {
 View root = inflater.inflate(R.layout.main, container, false);
 return root;
 }
Dauntless answered 10/10, 2013 at 8:47 Comment(0)
A
2

optimized way and shot code

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.list_alphabet, container, false);
    }
Azimuth answered 3/4, 2014 at 12:59 Comment(0)
O
1
getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit();

List is not accepted, it's looking for a fragment.

Overhaul answered 28/6, 2012 at 22:3 Comment(0)
L
0

use this pattern, works perfectly!

inflater.inflate(R.layout.list_layout, container, false);

public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)

attachToRoot Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

Lament answered 12/1, 2014 at 15:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.