安卓+具有自定义视图层次结构的 ListFragment

发布于 2024-12-06 00:20:44 字数 2345 浏览 0 评论 0原文

我试图通过从 onCreateView(LayoutInflater, ViewGroup, Bundle) 返回我自己的视图层次结构来自定义片段布局。这会膨胀我的自定义视图,但似乎是堆叠视图而不是在内部膨胀,我可以立即看到所有内容。任何帮助表示赞赏。

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

ma​​in.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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ぇ气 2024-12-13 00:20:44

我没有在 ListFragment 类的 onCreateView 方法中返回新视图。例如:

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

现在在 Android 的土地上一切都运转良好!

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!

帅气尐潴 2024-12-13 00:20:44

我想你最好

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

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;
 }
爱的那么颓废 2024-12-13 00:20:44

优化方式和拍摄代码

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.list_alphabet, container, false);
    }

optimized way and shot code

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.list_alphabet, container, false);
    }
骄兵必败 2024-12-13 00:20:44
getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit();

列表不被接受,它正在寻找片段。

getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit();

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

陌上芳菲 2024-12-13 00:20:44

使用这个模式,效果完美!

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

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

AttachToRoot 膨胀的层次结构是否应附加到 root 参数?如果为 false,则 root 仅用于为 XML 中的根视图创建正确的 LayoutParams 子类。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文