Android: 在片段中显示列表

Android: Displaying a list in a fragment

我有一个现有片段,我想在其中显示典型地址类型的信息,例如街道、城市、PIN 和 Phone 号码列表。

为了显示 Phone 个数字的列表,我在 addressfragment.xml 中添加了列表视图:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp" >

        <TextView
            android:id="@+id/txtStreetAddr"
            style="?android:textAppearanceMedium"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:lineSpacingMultiplier="1.2"
            android:text="default" />

        <ListView
            android:id="@+id/phones"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0dp"
            android:paddingLeft="0dp" >
        </ListView>
        </LinearLayout>
</ScrollView>

我的片段class:

        public class AddressFragment extends Fragment {
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {

ViewGroup rootView = (ViewGroup) inflater
                .inflate(R.layout.fragment_screen_slide_page, container, false);

                ((TextView) rootView.findViewById(R.id.txtStreetAddr)).setText(
                        "123, Corner Street");

                ListView phoneListView = ((ListView) rootView.findViewById(R.id.phones));

                ArrayList<String> phNumbers = new ArrayList<String>();
                phNumbers.add("4343534343");
                phNumbers.add("6767566766");

                ArrayAdapter<String> arrayAdapter =      
                new ArrayAdapter<String>(MyApp.getAppContext(),android.R.layout.simple_list_item_1, phNumbers);
                // Set The Adapter
                phoneListView.setAdapter(arrayAdapter); 
    return rootView;
        }

phone 列表视图没有显示在屏幕上 - 为什么? 有没有更好的方法来处理这个问题?

我想你忘了在 onCreateView(...)

中给 View 充气
View rootView= inflater.inflate(R.layout.your_layout, container, false);

添加你的onCreateView(...)应该是

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

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

        ((TextView) rootView.findViewById(R.id.txtStreetAddr)).setText(
                "123, Corner Street");

        ListView phoneListView =((ListView) rootView.findViewById(R.id.phones);

        ArrayList<String> phNumbers = new ArrayList<String>();
        phNumbers.add("4343534343");
        phNumbers.add("6767566766");

        ArrayAdapter<String> arrayAdapter =      
        new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, phNumbers);
        // Set The Adapter
        phoneListView.setAdapter(arrayAdapter);

     return rootView;
  }

删除 ScrollView 并将 LinearLayout 作为父布局。

如果您将滚动视图作为父视图,那么您必须以编程方式设置列表视图高度,您可以创建一个方法,如下所示---

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {

        return;
    }

    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight
            + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();

}

然后将列表视图传递给此方法,如下所示 --

phoneListView.setAdapter(arrayAdapter);

setListViewHeightBasedOnChildren(phoneListView);

希望这能完美运行。