【问题标题】:Android: Displaying a list in a fragmentAndroid:在片段中显示列表
【发布时间】:2015-07-19 01:02:08
【问题描述】:

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

为了显示电话号码列表,我在 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>

我的片段类:

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

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

【问题讨论】:

  • 完全发布你的布局xml文件
  • 并发布完整的 onCreateView。发布完整的布局

标签: android android-listview android-listfragment


【解决方案1】:

我想你忘了给View 充气到onCreateView(...)

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

【讨论】:

  • 这可能解释了运行时崩溃或编译时错误,而不是列表未显示错误,
  • 对不起,这是部分代码...充气机确实在那里(有问题的代码已更新),因为我的街道地址显示正确
  • @Jasper 也使用了getActivity() 而不是MyApp.getAppContext()
  • MD> 是的,在我使用 getActivity() 后,列表视图出现了 - 但我只看到列表中的一行.. 只是第一行。
  • 显然 ScrollView 中的 ListView 不会显示所有记录 - 所以我们现在只看到一条记录。
【解决方案2】:

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

【讨论】:

  • 感谢 Chandrakanth...确实解决了一个记录问题。如果您知道任何地址小部件或处理此问题的更好方法,请发布...!
【解决方案3】:

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

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

}

然后将listview传递给这个方法,如下所示--

phoneListView.setAdapter(arrayAdapter);

setListViewHeightBasedOnChildren(phoneListView);

希望这会完美运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    • 2013-07-27
    • 2012-12-30
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多