<fragment android:id="@+id/myfragment" android:name="包名.Fragment类名" android:layout_width="match_parent" android:layout_height="match_parent" />
FragmentManager fragmentManager = getFragmentManager();//这里要注意是否是V4包的
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(Activity设置的布局中的ViewGroup组件id,需要替换的Fragment实例);
fragmentTransaction.commit();
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<fragment
android:id="@+id/main_frag1"
android:name="com.example.xmlfragment.FragmentA"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/main_frag2"
android:name="com.example.xmlfragment.FragmentB"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="音乐" />
<Button
android:id="@+id/sports"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="体育" />
<Button
android:id="@+id/arts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="美术" />
<Button
android:id="@+id/dance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="舞蹈" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/frag2_lv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
package com.example.xmlfragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
/**
* 碎片页面A的显示,这里显示四个按钮
*/
public class FragmentA extends Fragment implements View.OnClickListener {
//布局中的控件
Button music;
Button sports;
Button arts;
Button dance;
/**
* onCreate执行之后执行的方法,一般用于显示视图
*/
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//不要super实现的方法,它是返回空值的
return View.inflate(getActivity(), R.layout.fragment_a, null);
}
//这个方法不在Fragment的生命周期里面
//但是它会在onCreateView后面执行,里面的参数View就是上面传入的view对象
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//实例化布局上的控件
music = (Button) view.findViewById(R.id.music);
sports = (Button) view.findViewById(R.id.sports);
arts = (Button) view.findViewById(R.id.arts);
dance = (Button) view.findViewById(R.id.dance);
//给控件添加监听事件
music.setOnClickListener(this);
sports.setOnClickListener(this);
arts.setOnClickListener(this);
dance.setOnClickListener(this);
}
//实现监听的方法
@Override
public void onClick(View v) {
//获取页面碎片B的对象,来对页面碎片B进行操作,这里不能使用new的方法来创建对象
FragmentB fragmentB = (FragmentB) getActivity().getSupportFragmentManager().findFragmentById(R.id.main_frag2);
switch (v.getId()) {
case R.id.music:
fragmentB.list.add(0, "音乐");//给页面B的集合中添加数据
Toast.makeText(getActivity(), "music", Toast.LENGTH_SHORT).show();
break;
case R.id.sports:
fragmentB.list.add(0, "运动");//给页面B的集合中添加数据
Toast.makeText(getActivity(), "sports", Toast.LENGTH_SHORT).show();
break;
case R.id.arts:
fragmentB.list.add(0, "艺术");//给页面B的集合中添加数据
Toast.makeText(getActivity(), "arts", Toast.LENGTH_SHORT).show();
break;
case R.id.dance:
fragmentB.list.add(0, "跳舞");//给页面B的集合中添加数据
Toast.makeText(getActivity(), "dance", Toast.LENGTH_SHORT).show();
break;
}
fragmentB.adapter.notifyDataSetChanged();//刷新页面B的数据
}
}
package com.example.xmlfragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
/**
* 碎片页面B的显示,这里显示一个ListView数据
*/
public class FragmentB extends Fragment implements AdapterView.OnItemClickListener {
//定义集合、适配器、ListView
List<String> list;
ArrayAdapter<String> adapter;
ListView listView;
//数据源的其中一个字符串变量
String name = "music";
/**
* 最先执行,一般是处理于界面无关的数据
*/
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//实例化list集合
list = new ArrayList<>();
//实例化ListView
//添加数据源
for (int i = 1; i <= 100; i++) {
list.add(name + i);
}
//实例化Adapter对象
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_activated_1, list);
}
/**
* onCreate执行之后执行的方法,一般用于显示视图
*/
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//不要super实现的方法,它是返回空值的
return View.inflate(getActivity(), R.layout.fragment_b, null);
}
/***
* fragment创建前最后执行的方法
* 加载视图上面显示的数据和默认设置
*/
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//这里的View上面加载的ListView
listView = (ListView) view.findViewById(R.id.frag2_lv);
//给ListView添加适配器
listView.setAdapter(adapter);
//给ListView添加点击的监听事件
listView.setOnItemClickListener(this);
//获取碎片A的对象
fragmentA= (FragmentA) getActivity().getSupportFragmentManager().findFragmentById(R.id.main_frag1);
}
/**
* ListView中点击对应的条目的回调方法
*/
FragmentA fragmentA;
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//这里修改页面A中的第一个按钮的文本
//点击ListView哪个条目,这个条目的文本都会显示在第一个按钮上(实现Fragment之间的通信)
fragmentA.music.setText(list.get(position));
}
}
package com.example.xmlfragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.fragment.MainActivity">
<RadioGroup
android:background="@drawable/tab_bg"
android:id="@+id/main_rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<RadioButton
android:id="@+id/main_rb1"
style="@style/buttonstyle"
android:drawableTop="@drawable/contact"
android:textColor="@color/mytextcolors"
android:text="联系人" />
<RadioButton
android:id="@+id/main_rb2"
style="@style/buttonstyle"
android:drawableTop="@drawable/message"
android:textColor="@color/mytextcolors"
android:text="消息" />
<RadioButton
android:id="@+id/main_rb3"
style="@style/buttonstyle"
android:drawableTop="@drawable/news"
android:textColor="@color/mytextcolors"
android:text="动态" />
<RadioButton
android:id="@+id/main_rb4"
style="@style/buttonstyle"
android:drawableTop="@drawable/setting"
android:textColor="@color/mytextcolors"
android:text="设置" />
</RadioGroup>
<FrameLayout
android:id="@+id/main_fl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/main_rg"/>
</RelativeLayout>
package com.example.fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
*这是联系人碎片页面
*/
public class ContactFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setText("这是联系人页面");
textView.setTextSize(30);
return textView;
}
}
package com.example.fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* 这是消息页面的碎片
*/
public class MessageFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setText("这是消息页面");
textView.setTextSize(30);
return textView;
}
}
package com.example.fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
*这是动态页面的碎片
*/
public class NewsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setText("这是动态页面");
textView.setTextSize(30);
return textView;
}
}
package com.example.fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
*这是动态页面的碎片
*/
public class NewsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setText("这是动态页面");
textView.setTextSize(30);
return textView;
}
}
package com.example.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
//定义布局内的控件
RadioGroup radioGroup;
FrameLayout frameLayout;
//定义四个存放碎片的数组
Fragment[] fragment = new Fragment[4];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
radioGroup.check(R.id.main_rb1);
//显示第一个碎片页面
showFragment(0);
}
/**
* 显示碎片页面的方法
* 这里是要重点理解的地方
* 这里要添加和移除的操作都有,而且还有进行一定的判断
*/
//定义一个当前点击的游标值,默认是-1,说明还没有点
int currentIndex = -1;
private void showFragment(int i) {
//如果点击的页面是刚才显示的页面,就什么都不做
if (i == currentIndex) {
return;
}
//处理碎片,显示、移除等等
//这里要用碎片的事务来完成
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//如果用户打开已经打开过一个Fragment页面,再打开其他页面后,要先把原来的页面移除
if (currentIndex != -1) {
//移除碎片
transaction.hide(fragment[currentIndex]);
}
//显示新的碎片
if (fragment[i] == null) {
//创建碎片
CreateFragment(i);
//使用事务显示碎片
//第一个参数是碎片要显示的布局的位置的ID号
//第二个参数是显示的碎片的对象
transaction.add(R.id.main_fl, fragment[i]);
} else {
//如果碎片曾经显示过就显示出来就可以了
transaction.show(fragment[i]);
// transaction.addToBackStack(null);
}
//保存用户点击的游标值
currentIndex = i;
//最后提交事务,把碎片放置到位
transaction.commit();
}
//初始化数据
private void initView() {
//实例化数据
radioGroup = (RadioGroup) findViewById(R.id.main_rg);
frameLayout = (FrameLayout) findViewById(R.id.main_fl);
//给GroupButton设置监听事件
radioGroup.setOnCheckedChangeListener(this);
}
/**
* 按钮选择后触发的方法
*/
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//点击哪一个按钮就显示哪一个碎片
//这里的checkedID不是0、1、2、3这种数值,而是布局里面对应的控件的ID值
switch (checkedId) {
case R.id.main_rb1:
showFragment(0);
break;
case R.id.main_rb2:
showFragment(1);
break;
case R.id.main_rb3:
showFragment(2);
break;
case R.id.main_rb4:
showFragment(3);
break;
}
}
/**
* 创建碎片页面对象的方法
*/
private void CreateFragment(int i) {
//如果碎片是第一次点开,就要创建碎片
switch (i) {
case 0:
fragment[i] = new ContactFragment();
break;
case 1:
fragment[i] = new MessageFragment();
break;
case 2:
fragment[i] = new NewsFragment();
break;
case 3:
fragment[i] = new SettingFragment();
break;
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有