2

行き詰まっています。フラグメント内にある 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;
        }
    }

しかし、ご覧のとおり、すべてを接続する方法がわかりません。ArrayAdapter を使用してデータを処理するカスタマイズされた ListFragment を作成しました。

ここに私のレイアウトファイルがあります:

<?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>
4

2 に答える 2

1

ListView だけを使用するために、ChatFragment 内に別の Fragment を追加する必要はありません。次のように、レイアウトから ListView の参照を取得します。

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

アダプターと配列/リストをそれに設定すれば完了です。

Fragment 内で Fragment トランザクションを行わないでください。このジョブには Activity と DrawerLayout を使用します。

于 2013-08-31T12:33:01.600 に答える
0

の必要はありませんChatFragment

アクティビティ aFragmentActivityを作成し、その中の ListFragment を使用してレイアウト(R.layout.fragment_chat) を直接インフレートしActivityます。

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

の使用については、この例を確認してくださいListFragments

于 2013-08-31T12:00:14.607 に答える