MainActivity_xml <?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/frag" android:layout_width="match_parent" android:layout_weight="1" android:layout_height="match_parent"></FrameLayout> <RadioGroup android:id="@+id/rg" android:layout_weight="1" android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="100dp"> <RadioButton android:id="@+id/rb1" android:text="我的" android:layout_weight="1" android:drawableTop="@drawable/one" android:gravity="center" android:button="@null" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/rb2" android:text="哈哈" android:layout_weight="1" android:gravity="center" android:button="@null" android:drawableTop="@drawable/two" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/rb3" android:text="呵呵" android:layout_weight="1" android:gravity="center" android:button="@null" android:drawableTop="@drawable/three" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RadioGroup> </LinearLayout> 首页xml<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.TabLayout android:id="@+id/tab" android:layout_width="match_parent" android:layout_height="80dp"> </android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager android:id="@+id/viewpagers" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v4.view.ViewPager> </LinearLayout>
底部其他fragment<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv" android:text="标题一" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>package fengras.com.tupianxuanzeqi; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.RadioButton; import android.widget.RadioGroup; import fengras.com.tupianxuanzeqi.fragment.Fragment01; import fengras.com.tupianxuanzeqi.fragment.Fragment03; import fengras.com.tupianxuanzeqi.fragment.Fragment04; public class MainActivity extends AppCompatActivity { private RadioGroup rg; private RadioButton rb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rg = (RadioGroup) findViewById(R.id.rg); rb = (RadioButton) findViewById(R.id.rb1); rb.setChecked(true); getSupportFragmentManager().beginTransaction().add(R.id.frag,new Fragment01()).commit(); rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId){ case R.id.rb1: getSupportFragmentManager().beginTransaction().replace(R.id.frag,new Fragment01()).commit(); break; case R.id.rb2: getSupportFragmentManager().beginTransaction().replace(R.id.frag,new Fragment03()).commit(); break; case R.id.rb3: getSupportFragmentManager().beginTransaction().replace(R.id.frag,new Fragment04()).commit(); break; } } }); } }package fengras.com.tupianxuanzeqi.fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.ArrayList; import java.util.List; import fengras.com.tupianxuanzeqi.R; /** * Created by Administrator on 2017/3/30. */ public class Fragment01 extends Fragment { private List<String> list=new ArrayList<>(); private TabLayout tab; private ViewPager viewPager; private TextView textView; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=inflater.inflate(R.layout.index,null); textView = (TextView) view.findViewById(R.id.tv); tab = (TabLayout) view.findViewById(R.id.tab); viewPager = (ViewPager) view.findViewById(R.id.viewpagers); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); list.add("标题一"); list.add("标题二"); list.add("标题三"); //通过title 并且bundle传值 创建适配器 FragmentPagerAdapter frag=MyAdapter(list); tab.setTabMode(tab.MODE_SCROLLABLE ); viewPager.setAdapter(frag); tab.setTabsFromPagerAdapter(frag);//数据传给Tab tab.setupWithViewPager(viewPager); //关联ViewPager } private FragmentPagerAdapter MyAdapter(final List<String> list) { FragmentPagerAdapter fragmentPagerAdapter = new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) { @Override public Fragment getItem(int position) { Fragment02 fragment02=new Fragment02(); Fragment02 fragment01 = new Fragment02(); Log.e("ss",list.get(position)); Bundle bundle = new Bundle(); bundle.putString("title",list.get(position) ); fragment01.setArguments(bundle); return fragment01; } @Override public int getCount() { return list.size(); } @Override public CharSequence getPageTitle(int position) { return list.get(position); } }; return fragmentPagerAdapter; } }
复用fragmentpackage fengras.com.tupianxuanzeqi.fragment; 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 fengras.com.tupianxuanzeqi.R; /** * Created by Administrator on 2017/3/30. */ public class Fragment02 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view =inflater.inflate(R.layout.fragment,null); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Bundle arguments = this.getArguments(); String string = arguments.getString("title"); Log.e("ss",string); } }package fengras.com.tupianxuanzeqi.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 fengras.com.tupianxuanzeqi.R; /** * Created by Administrator on 2017/3/30. */ public class Fragment03 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment02,null); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } }package fengras.com.tupianxuanzeqi.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 fengras.com.tupianxuanzeqi.R; /** * Created by Administrator on 2017/3/30. */ public class Fragment04 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment,null); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } }
实现一个顶部和底部的导航功能
最新推荐文章于 2023-04-02 10:11:49 发布