以下是我的主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"?>

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">

android:id="@+id/chat_tab_menu"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="#FF0066"

android:orientation="horizontal" >

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" />

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" />

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" />

android:id="@+id/view_pager"

android:layout_width="match_parent"

android:layout_height="3dp"

android:background="#77A0DD"/>

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" />

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐