包含自定义ListView的片段

时间:2013-08-31 11:48:06

标签: android listview fragment

我被卡住了,我不知道如何实现片段内的ListView,我正在尝试聊天gui。

有可能做这样的事情,还是我全力以赴?

感谢您的帮助!关心新手! ^^

这是片段代码:

public static class ChatFragment extends Fragment {

        public ChatFragment() {
            // Empty constructor required for fragment subclasses
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_chat, container, false);

            /*FragmentManager fm =  getActivity().getFragmentManager();
            DataListFragment list = new DataListFragment();
            fm.beginTransaction().add(R.id.list_message_History, list).commit();
            */

            getActivity().setTitle(R.string.chat);
            return rootView;
        }
    }

但是,正如你所看到的,我不知道如何连接它。我有一个自定义的ListFragment,它使用ArrayAdapter来处理数据。

这是我的布局文件:

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

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:tools="http://schemas.android.com/tools"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:id="@id/android:list"
                  android:orientation="horizontal">
        <ListView android:id="@android:id/list"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:drawSelectorOnTop="false"/>
    </LinearLayout>


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:tools="http://schemas.android.com/tools"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="horizontal">
        <EditText android:id="@+id/edit_message"
                  android:layout_weight="1"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:hint="@string/edit_message" />
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/button_send" />
    </LinearLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:1)

您不必在ChatFragment中添加另一个Fragment来拥有ListView。从布局中获取ListView的参考,如下所示:

ListView list = (ListView)rootView.findViewById(R.id.list);

现在将适配器和数组/列表设置为它,然后就完成了。

不要在Fragment中创建Fragment事务。使用您的Activity和DrawerLayout完成这项工作。

答案 1 :(得分:0)

无需ChatFragment

让您的活动为FragmentActivity并使用Activity中的ListFragment直接夸大布局(R.layout.fragment_chat)。

class MainActivity extends FragmentActivity {
   onCreate(Bundle extra) {
   ...
   setContentView(R.layout.fragment_chat);
   ...
}
}

使用ListFragments检查this示例。

相关问题