为啥 ListFragment 在没有 ListView 标签的情况下工作?

Posted

技术标签:

【中文标题】为啥 ListFragment 在没有 ListView 标签的情况下工作?【英文标题】:Why is ListFragment working without the ListView tag?为什么 ListFragment 在没有 ListView 标签的情况下工作? 【发布时间】:2019-07-03 05:43:18 【问题描述】:

我有一个 ListFragment 在 XML 文件资源中没有 ListView 标记的情况下工作。

根据以下参考,ListFragment 需要一个带有ListView 标签的 XML 文件:

https://developer.android.com/reference/android/app/ListFragment

但是,下面的代码在没有代码的情况下也能正常工作。这个帖子解释了原因吗?

Difference between android.app.Fragment and android.support.v4.app.Fragment

另外,我正在使用 android-support-v4-app-fragmentandroid-app-fragment

谢谢。

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:orientation="vertical">    
    <FrameLayout
        android:id="@+id/newFrame"
        android:layout_
        android:layout_
        android:background="@color/colorPrimary">    
    </FrameLayout>    
</LinearLayout>

主活动:

public class MainActivity extends AppCompatActivity 

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        Fragment fragmentA = new FragmentA();
        fragmentTransaction.add(R.id.newFrame, fragmentA, "fragmentA");
        fragmentTransaction.commit();
    

列表片段:

public class FragmentA extends ListFragment 

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);

        String dataArray[] = new String[]"One", "Two", "Three",;

        ListAdapter listAdapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, dataArray);

        setListAdapter(listAdapter);
        

【问题讨论】:

因为它有一个 ListView 作为android.R.id.list 我想。 【参考方案1】:

之所以起作用,是因为 ListFragment 中的 onCreateView 为你创建了一个简单的 ListView。

这是 ListFragment 的 Android 源代码:

/**
 * Provide default implementation to return a simple list view.  Subclasses
 * can override to replace with their own layout.  If doing so, the
 * returned view hierarchy <em>must</em> have a ListView whose id
 * is @link android.R.id#list android.R.id.list and can optionally
 * have a sibling view id @link android.R.id#empty android.R.id.empty
 * that is to be shown when the list is empty.
 * 
 * <p>If you are overriding this method with your own custom content,
 * consider including the standard layout @link android.R.layout#list_content
 * in your layout file, so that you continue to retain all of the standard
 * behavior of ListFragment.  In particular, this is currently the only
 * way to have the built-in indeterminant progress state be shown.
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
    return inflater.inflate(com.android.internal.R.layout.list_content,
            container, false);

Source code for ListFragment

文件 list_content.xml 包含以下 ListView:

<ListView android:id="@android:id/list"
    android:layout_ 
    android:layout_ />

但是,如果您的代码要覆盖 ListFragment 中的 onCreateView 以提供自定义布局,那么您的布局将需要显式声明一个 ListView。

注意:可能值得一提的是,ListView 目前被 Android Studio 归类为“旧版”视图。大多数新应用目前在大多数情况下都应该使用 RecyclerView。

以下 Android Studio 命令将为您创建一个完整的 RecyclerView 示例列表:

File -> New -> Fragment (List)  

[它当前创建一个从 Fragment 而不是 ListFragment 派生的列表]

【讨论】:

以上是关于为啥 ListFragment 在没有 ListView 标签的情况下工作?的主要内容,如果未能解决你的问题,请参考以下文章

wpf,我给一个textbox的text绑定到一个listv.selectionitem.path,

相当于 ListFragment 的 onContentChange?

IllegalStateException,指定的孩子已经有一个父母 - ListFragment

ListFragment(或 Fragment)中的 BaseAdapter 不起作用

如何在 ListFragment 中使用 setEmptyView() 和自定义列表布局

ListFragment OnListItemClick 未被调用