android--fragment的使用
来源:互联网 发布:南京凶宅数据库 编辑:程序博客网 时间:2024/06/08 16:53
1通过FragmentAdapter适配器实现
1 主框架布局
<?xml version="1.0" encoding="utf-8"?><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="vertical"> <FrameLayout android:id="@+id/flMain" android:layout_width="match_parent" android:layout_height="@dimen/weight_setting" android:layout_weight="1" android:background="#00ff00"> </FrameLayout> <RadioGroup android:id="@+id/rdgMain" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/rbtFirst" android:layout_width="@dimen/weight_setting" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="首页" /> <RadioButton android:id="@+id/rbtShop" android:layout_width="@dimen/weight_setting" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="商家" /> <RadioButton android:id="@+id/rbtMy" android:layout_width="@dimen/weight_setting" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="我的" /> <RadioButton android:id="@+id/rbtMore" android:layout_width="@dimen/weight_setting" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="更多" /> </RadioGroup></LinearLayout>2 四个碎片的布局
2.1
<?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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="首页"/></LinearLayout>2.2
<?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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="商家"/></LinearLayout>
2.3
<?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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我的"/></LinearLayout>
2.4
<?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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="更多"/></LinearLayout>
3 java文件
3.1主框架
package com.example.joshua.meituan.activity;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.FrameLayout;import android.widget.RadioGroup;import com.example.joshua.meituan.R;import com.example.joshua.meituan.adapter.MyFragmentAdapter;import com.example.joshua.meituan.fragment.FirstFragment;import com.example.joshua.meituan.fragment.MoreFragment;import com.example.joshua.meituan.fragment.MyFragment;import com.example.joshua.meituan.fragment.ShopFragment;import com.example.joshua.meituan.utils.LogUtils;import java.util.ArrayList;import java.util.List;public class MainActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener { FrameLayout flMain; RadioGroup rdgMain; //碎片管理 FragmentManager fragmentManager; MyFragmentAdapter myFragmentAdapter; int fragmentIndex = 0; List<Fragment> fragmentDatas; FirstFragment firstFragment; ShopFragment shopFragment; MyFragment myFragment; MoreFragment moreFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); initOnClickListener(); } private void initOnClickListener() { rdgMain.setOnCheckedChangeListener(this); } private void init() { flMain= (FrameLayout) findViewById(R.id.flMain); rdgMain= (RadioGroup) findViewById(R.id.rdgMain); fragmentDatas = new ArrayList<>(); firstFragment = new FirstFragment(); shopFragment = new ShopFragment(); myFragment = new MyFragment(); moreFragment = new MoreFragment(); fragmentDatas.add(firstFragment); fragmentDatas.add(shopFragment); fragmentDatas.add(myFragment); fragmentDatas.add(moreFragment); fragmentManager = getSupportFragmentManager(); myFragmentAdapter = new MyFragmentAdapter(fragmentManager, fragmentDatas); } @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { switch (i) { case R.id.rbtFirst: fragmentIndex = 0; LogUtils.getLogInfo(this, fragmentIndex + ""); break; case R.id.rbtShop: fragmentIndex = 1; LogUtils.getLogInfo(this, fragmentIndex + ""); break; case R.id.rbtMy: fragmentIndex = 2; LogUtils.getLogInfo(this, fragmentIndex + ""); break; case R.id.rbtMore: fragmentIndex = 3; LogUtils.getLogInfo(this, fragmentIndex + ""); break; } Fragment fragment = (Fragment) myFragmentAdapter.instantiateItem(flMain,fragmentIndex); myFragmentAdapter.setPrimaryItem(flMain,fragmentIndex,fragment); myFragmentAdapter.finishUpdate(flMain); }}
3.2碎片1
package com.example.joshua.meituan.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 com.example.joshua.meituan.R;import com.example.joshua.meituan.utils.LogUtils;/** * Created by joshua on 2016/6/1. */public class FirstFragment extends Fragment { View view; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_first, container, false); return view; } @Override public void onDestroy() { super.onDestroy(); LogUtils.getLogError(getActivity(),"FirstFragment"); } @Override public void setMenuVisibility(boolean menuVisible) { super.setMenuVisibility(menuVisible); if (this.getView() != null) this.getView().setVisibility(menuVisible ? View.VISIBLE : View.GONE); }}
3.3碎片2
package com.example.joshua.meituan.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 com.example.joshua.meituan.R;import com.example.joshua.meituan.utils.LogUtils;/** * Created by joshua on 2016/6/1. */public class ShopFragment extends Fragment { View view; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_shop, container, false); return view; } @Override public void onDestroy() { super.onDestroy(); LogUtils.getLogError(getActivity(),"ShopFragment"); } @Override public void setMenuVisibility(boolean menuVisible) { super.setMenuVisibility(menuVisible); if (this.getView() != null) this.getView().setVisibility(menuVisible ? View.VISIBLE : View.GONE); }}
3.4碎片3
package com.example.joshua.meituan.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 com.example.joshua.meituan.R;import com.example.joshua.meituan.utils.LogUtils;/** * Created by joshua on 2016/6/1. */public class MyFragment extends Fragment { View view; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_my, container, false); return view; } @Override public void onDestroy() { super.onDestroy(); LogUtils.getLogError(getActivity(),"MyFragment"); } @Override public void setMenuVisibility(boolean menuVisible) { super.setMenuVisibility(menuVisible); if (this.getView() != null) this.getView().setVisibility(menuVisible ? View.VISIBLE : View.GONE); }}
3.5碎片4
package com.example.joshua.meituan.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 com.example.joshua.meituan.R;import com.example.joshua.meituan.utils.LogUtils;/** * Created by joshua on 2016/6/1. */public class ShopFragment extends Fragment { View view; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_shop, container, false); return view; } @Override public void onDestroy() { super.onDestroy(); LogUtils.getLogError(getActivity(),"ShopFragment"); } @Override public void setMenuVisibility(boolean menuVisible) { super.setMenuVisibility(menuVisible); if (this.getView() != null) this.getView().setVisibility(menuVisible ? View.VISIBLE : View.GONE); }}
4适配器
package com.example.joshua.meituan.adapter;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentPagerAdapter;import java.util.List;/** * Created by joshua on 2016/6/2. */public class MyFragmentAdapter extends FragmentPagerAdapter { List<Fragment> datas; public MyFragmentAdapter(FragmentManager fm, List<Fragment> datas) { super(fm); this.datas=datas; } @Override public Fragment getItem(int position) { return datas.get(position); } @Override public int getCount() { return datas.size(); }}
1 0
- Android Fragment的使用
- Android-Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- android Fragment 的使用
- Android Fragment的使用
- Android Fragment的使用
- Android---fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- JavaScript中逻辑运算符(&&、||、!)的优先级和运算顺序
- 模型自动创建
- 土豆肉丝做法
- Qt 获取Windows内存信息
- 解决VMware下64位Linux系统不支持全虚拟化(VMX)
- android--fragment的使用
- Web容器启动时加载Spring分析
- 安卓获取版本号
- Android SDK Manager目录解析
- c#面向对象特征(2)之多态
- 有一篇文章,共三行文字,每行有最多80个字符。要求分别统计出其中英文大写字母,小写字母,数字,空格及其他字符
- 10 款最佳剪贴板管理器
- C语言数据输入与输出
- cocos2d结合cocostudio适配