为什么没有ListView标记的ListFragment可以工作?

时间:2019-02-08 23:27:56

标签: android android-fragments android-listfragment

更多的是怀疑而不是问题, 我有一个ListFragment在xml文件资源中没有标记的情况下工作。

根据以下参考,ListFragment需要带有标记的xml文件, https://developer.android.com/reference/android/app/ListFragment

因此,下面的代码可以正常工作,但我不知道它是否正确

也许是因为这个线程, Difference between android.app.Fragment and android.support.v4.app.Fragment

问题是我正在使用android-support-v4-app-fragment,而引用是针对android-app-fragment。

谢谢。-

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


    <FrameLayout
        android:id="@+id/newFrame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary">

    </FrameLayout>

</LinearLayout>


package com.example.android.myapplication;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

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

package com.example.android.myapplication;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

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

}

1 个答案:

答案 0 :(得分:0)

之所以起作用,是因为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_width="match_parent" 
    android:layout_height="match_parent" />

但是,如果您的代码将重写ListFragment中的onCreateView以提供自定义布局,则您的布局将需要显式声明ListView。

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

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

File -> New -> Fragment (List)  

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