ViewPager与RadioGroup制作导航栏
2016-05-26 15:18
507 查看
ViewPager
在我们使用微信的导航栏的时候,细心的朋友可能会注意到,每个页面既可以点击图标进入,也可以使用左右滑动页面进行切换。今天我们就来讲解一下这两个的功能的制作方法。这里引用网络图片。这是谷歌官方给我们提供的一个兼容低版本安卓设备的软件包,里面包囊了只有在安卓3.0以上可以使用的api。而viewpager就是其中之一利用它,我们可以做很多事情,从最简单的导航,到页面菜单等等。
(废话不多说,直接上代码!!)
首先在主页面上布局文件:
<?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" tools:context="com.healbury.radiogroupviewpager.MainActivity"> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/radio_groups" android:background="@android:color/holo_blue_light"/> <include android:id="@+id/radio_groups" layout="@layout/item_buttom_layout" android:layout_width="match_parent" android:layout_alignParentBottom="true" android:layout_height="50dp"/> </RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener { private ViewPager viewPager; private List<Fragment> fragments = new ArrayList<>(); private RadioGroup radioGroup; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initFragment(); initView(); initRadioGroup(); } private void initFragment(){ fragments.add(MyFragment.newInstance("微信")); fragments.add(MyFragment.newInstance("通讯录")); fragments.add(MyFragment.newInstance("发现")); fragments.add(MyFragment.newInstance("我")); } private void initRadioGroup(){ radioGroup = (RadioGroup) findViewById(R.id.radio_group); radioGroup.setOnCheckedChangeListener(this); //设置选中状态 ((RadioButton)radioGroup.getChildAt(0)).setChecked(true); } private void initView(){ viewPager = (ViewPager) findViewById(R.id.viewpager); MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager(),fragments); viewPager.setAdapter(adapter); viewPager.setOnPageChangeListener(this); } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { for (int i = 0; i < group.getChildCount(); i++) { if (checkedId == group.getChildAt(i).getId()){ viewPager.setCurrentItem(i,false); } } } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { ((RadioButton)radioGroup.getChildAt(position)).setChecked(true); } @Override public void onPageScrollStateChanged(int state) { } }
说明:在MainActivity中执行RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener两个接口,实现四个方法:
onCheckedChanged
onCheckedChanged
onPageSelected
onPageScrollStateChanged
为每个页面设定Fragment,我这里做一个就可以了。
MyFragment.java
public class MyFragment extends Fragment { private static final String ARG_PARAM1 = ""; private String mParam1; private View view; public MyFragment() { // Required empty public constructor } public static MyFragment newInstance(String param1) { MyFragment fragment = new MyFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_my, container, false); TextView textView = (TextView) view.findViewById(R.id.text); textView.setText(mParam1); return view; } }
适配器
public class MyPagerAdapter extends FragmentPagerAdapter { private List<Fragment> fragments; public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) { super(fm); this.fragments = fragments; } @Override public Fragment getItem(int position) { return fragments.get(position); } @Override public int getCount() { return fragments.size(); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <RadioGroup android:id="@+id/radio_group" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <RadioButton android:layout_width="0dp" android:layout_height="wrap_content" android:text="微信" android:gravity="center" android:button="@null" android:layout_weight="1" android:textColor="@color/radiogroup_select" android:id="@+id/weixin"/> <RadioButton android:layout_width="0dp" android:layout_height="wrap_content" android:text="通讯录" android:gravity="center" android:button="@null" android:layout_weight="1" android:textColor="@color/radiogroup_select" android:id="@+id/tongxunlu"/> <RadioButton android:layout_width="0dp" android:layout_height="wrap_content" android:text="朋友圈" android:gravity="center" android:button="@null" android:layout_weight="1" android:textColor="@color/radiogroup_select" android:id="@+id/pengyouquan"/> <RadioButton android:layout_width="0dp" android:layout_height="wrap_content" android:text="我" android:gravity="center" android:button="@null" android:layout_weight="1" android:textColor="@color/radiogroup_select" android:id="@+id/wo"/> </RadioGroup> </LinearLayout>
简单的介绍,至于UI的设计就不细说了,功能的话是可以实现微信的效果的。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories