主Fragment嵌套三个Fragment,怎么实现滑动切换fragment

下面是我的主Fragment代码
package textview.exam;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

public class FragmentChat extends Fragment {

private RadioGroup main_chat_newsRedio;
private FragmentMainchatnews main_chat_news;
private FragmentMainchattrends main_chat_trends;
private FragmentMainchatweather main_chat_weather;

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater,
                         @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view = inflater.inflate(R.layout.main_chat, null);
    GroupChang(view);

    return view;
}







//主页面点击按钮Fragment改变
public void GroupChang(View view){
    //在chat页面中嵌套fragment
    main_chat_news = new FragmentMainchatnews();
    getChildFragmentManager().beginTransaction().replace(R.id.main_chat_content, main_chat_news).commit();
    main_chat_newsRedio = (RadioGroup) view.findViewById(R.id.chat_tab_menu);
    main_chat_newsRedio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {


        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub
            switch (checkedId) {
                case R.id.main_chat_news:
                    main_chat_news = new FragmentMainchatnews();
                    getChildFragmentManager().beginTransaction().replace(R.id.main_chat_content, main_chat_news)
                            .commit();
                    break;
                case R.id.main_chat_trends:
                    if (main_chat_trends==null) {
                        main_chat_trends =new FragmentMainchattrends();
                    }
                    Log.i("MyFragment", "FragmentMainchattrends");
                    getChildFragmentManager().beginTransaction().replace(R.id.main_chat_content, main_chat_trends).commit();
                    break;
                case R.id.main_chat_weather:
                    main_chat_weather = new FragmentMainchatweather();
                    getChildFragmentManager().beginTransaction().replace(R.id.main_chat_content, main_chat_weather)
                            .commit();
                    break;
                default:
                    break;
            }

        }
    });

}

}


下面是主Fragment的布局文件

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="vertical"
    tools:context="com.exam.Activity">

    <RadioGroup
        android:id="@+id/chat_tab_menu"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FF0066"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/main_chat_news"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_weight="1"
            android:button="@null"
            android:checked="true"
            android:height="140px"

            android:gravity="center"
            android:paddingTop="2dp"
            android:text="新闻"
            android:textColor="@color/tab_selector_tv_color" />

        <RadioButton
            android:id="@+id/main_chat_trends"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_weight="1"
            android:button="@null"
            android:height="140px"
            android:gravity="center"
            android:paddingTop="2dp"
            android:text="动态"
            android:textColor="@color/tab_selector_tv_color" />

        <RadioButton
            android:id="@+id/main_chat_weather"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_weight="1"
            android:button="@null"
            android:height="140px"
            android:gravity="center"
            android:paddingTop="2dp"
            android:text="天气"
            android:textColor="@color/tab_selector_tv_color"
            android:checked="false" />

    </RadioGroup>
    <View
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:background="#77A0DD"/>
    <include layout="@layout/line"/>

    <FrameLayout
        android:id="@+id/main_chat_content"
        android:layout_width="fill_parent"
        android:layout_height="500dp"
        android:layout_weight="1"
        android:flipInterval="30"
        android:layout_below="@+id/chat_tab_menu" />


</RelativeLayout>

2个回答

用viewpager把fragment添加到里面就可以实现滑动了!

a179583637
a179583637 谢谢
3 年多之前 回复

使用viewpager啊

a179583637
a179583637 谢谢
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!