2013-08-31 81 views
2

我被卡住了,我没有得到如何实现一个ListView里面的片段,我试图做一个聊天gui。包含自定义ListView的片段

是否有可能做这样的事情或我总是在风中?

感谢您的帮助!问候新手! ^^

这里是片段代码:

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

但正如你看到我不udnerstand如何连接这一切。我有一个自定义的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> 
+0

你必须为此制作自定义适配器 – Piyush

+0

你是什么意思? – westberg

+0

我有一个ListFragment类,它扩展了ListFragment,并且该类正在使用自定义的ArrayAdapter类。但是,我如何将它们连接到片段? – westberg

回答

1

您不必在ChatFragment中添加另一个片段即可拥有ListView。从这样的布局中获得ListView的参考:

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

现在设置适配器和阵列/列表并完成。

并且不要在片段内进行片段事务。使用你的Activity和DrawerLayout来完成这项工作。

+0

中完成了感谢,我有这个奇怪的想法,我必须使用fragmentListinside片段,但是我不需要做那个。再次感谢。 – westberg

0

不需要的ChatFragment

让你活动FragmentActivity和直接膨胀布局(R.layout.fragment_chat)与该Activity的ListFragment。

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

检查this使用示例ListFragments

+0

我已经试过这个例子^^但它不帮助我。我正在使用mDrawerLayout并显示不同的片段。我需要显示一个包含ListView +其他元素(如文本输入)的片段,因为我在布局文件 – westberg