【问题标题】:How to programmatically implement a custom ListView in an instantiated fragment from ViewPager如何以编程方式在 ViewPager 的实例化片段中实现自定义 ListView
【发布时间】:2016-06-04 08:04:28
【问题描述】:

在尝试使用选项卡式视图创建 Activity 时,我使用了 Android Studio/IntelliJ IDEA 中的 Tabbed Activity 模板。

我设法实例化了 3 个不同的选项卡,其中包含在 XML 资源中定义的它们自己的片段。 其中一个片段有一个 ListView,为了填充它,我通过创建自定义 ArrayAdapter 找到了解决方案,找到了 here

我的问题是将 ListView 设置为该适配器,以便显示我的数据。

嗯,比我预期的要容易..

【问题讨论】:

    标签: android listview android-fragments android-viewpager android-listfragment


    【解决方案1】:

    要在其中一个选项卡中实例化片段,Tabbed 模板的流程如下:

    public static class FragmentExample extends Fragment {
        public FragmentExample() {}
    
        public static FragmentExample newInstance() {
            FragmentExample fragment = new Fragment();
            return fragment;
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_example, container, false);
            return rootView;
        }
    }
    

    片段被这一行夸大了,其中 fragment_example 是它的 XML 资源:

    View rootView = inflater.inflate(R.layout.fragment_example, container, false);
    

    我尝试简单地使用相同的方法来扩充包含我的 ListView 的 XML,然后以编程方式填充数据,最后将适配器设置为它。但它总是在我脸上抛出一个引用适配器对象的 NullPointerException。
    我承认我不知道为什么,直到我读到这个答案:

    MainActivity 使用 R.layout.activity_main 作为包含 ViewPager 而不是 ListView 的参数调用 setContentView。由于您没有在该布局中声明 ListView,因此 findViewById(android.R.id.list) 返回 null,并且当您尝试访问它(调用 setAdapter)时,会引发 NullPointerException。 Awswer source here

    我意识到我当然必须更改那个特定的片段类,但不仅仅是其中的代码..
    通过做一些研究,我找到了 ListFragment 类并对其进行了分析。很有趣。

    为了让我的 ListView 与我的实例化片段一起膨胀,我只需要将它扩展到 ListFragment 类而不是 Fragment 类并像这样覆盖它的 onCreateView(我实际上是从超类本身改编的):

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            final Context context = getActivity();
    
            FrameLayout root = new FrameLayout(context);
    
            FrameLayout lframe = new FrameLayout(context);
    
            ListView lv = new ListView(getActivity());
            lv.setId(android.R.id.list);
            lv.setDrawSelectorOnTop(false);
            lframe.addView(lv, new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
    
            root.addView(lframe, new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
    
            // ------------------------------------------------------------------
    
            // BEFORE SETTING YOUR CUSTOM ADAPTER TO THE LISTVIEW YOU MUST FIRST CREATE AN OBJECT
            // OF IT AND POPULATE YOUR DATA. THIS IS SHOWN IN THE CUSTOM ARRAYADAPTER LINK ABOVE //
    
            setListAdapter(customAdapter);
    
            // ------------------------------------------------------------------
    
            root.setLayoutParams(new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
    
            return root;
        }
    

    这里发生的是它以编程方式设置片段和 ListView,所以我不需要它的 XML 资源文件。
    然后它使用 ListFragment 类中定义的 setListAdapter 方法设置适配器。

    我强烈建议您仔细阅读该特定课程以更好地理解它。

    我是 Android 开发新手。我希望我能在我所学的东西上帮助别人。
    请随时发布对 cme​​ts 的任何改进。 :]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-11
      • 1970-01-01
      • 1970-01-01
      • 2015-10-02
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 2020-12-29
      相关资源
      最近更新 更多