关闭

Android开发——用viewpage和fragment实现菜单栏

标签: android开发
115人阅读 评论(0) 收藏 举报
分类:

  我们之前也做过一种菜单栏,也是用fragment实现的,不过之前我们实现的方式和现在还是有区别的,之前我们Android开发——Fragment简单示例(二)制作一个应用菜单导航栏 是用线性布局做的,我们现在用viewpage实现,具体用哪一种那就看需求吧。


activity_main.xml

<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.book.activity.MainActivity" >

    <android.support.v4.view.ViewPager
        android:id="@+id/main_vp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RadioGroup
        android:id="@+id/main_rg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="10dp"
        android:gravity="center_vertical"
        android:orientation="horizontal" 
        android:background="#cdcdcd">

        <RadioButton
            android:id="@+id/main_rb_addBook"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:gravity="center"
            android:button="@null"
            android:text="添加图书" 
            android:textColor="#FF0000"/>

        <RadioButton
            android:id="@+id/main_rb_deleteBook"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:gravity="center"
            android:button="@null"
            android:text="删除图书" />

        <RadioButton
            android:id="@+id/main_rb_changeBook"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:gravity="center"
            android:button="@null"
            android:text="修改图书" />

        <RadioButton
            android:id="@+id/main_rb_queryBook"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:gravity="center"
            android:button="@null"
            android:text="查询图书" />
    </RadioGroup>

</RelativeLayout>

fragment_change.xml(其他的几个和这个一样)

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

MainActivity.java

package com.book.activity;

import java.util.ArrayList;
import java.util.List;

import com.book.adapter.MainVpAdapter;
import com.book.fragment.AddFragment;
import com.book.fragment.ChangeFragment;
import com.book.fragment.DeleteFragment;
import com.book.fragment.QueryFragment;
import com.briup.bookManager.R;
import com.briup.bookManager.R.id;
import com.briup.bookManager.R.layout;
import com.briup.bookManager.R.menu;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends FragmentActivity {
	private ViewPager mVp_show;;
	private RadioGroup mRg_showRb;
	private RadioButton mRb_addBook, mRb_deleteBook, mRb_changeBook,
			mRb_queryBook;
	private Fragment mAddFragment, mDeleteFragment, mChangeFragment,
			mQueryFragment;
	private List<Fragment> mFragments;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置界面不显示
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);

		initView();
		initData();
		initAdapter();
		initListener();
	}

	private void initListener() {
		// TODO Auto-generated method stub
		mVp_show.addOnPageChangeListener(new OnPageChangeListener() {

			// 界面滑动状态改变监听
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}

			// 界面滑动监听
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			// 当界面被选中被选中时监听
			@Override
			public void onPageSelected(int arg0) {
				switch (arg0) {
				case 0:
					mRb_addBook.setChecked(true);

					break;
				case 1:
					mRb_deleteBook.setChecked(true);
					break;
				case 2:
					mRb_changeBook.setChecked(true);
					break;
				case 3:
					mRb_queryBook.setChecked(true);
					break;
				default:
					break;
				}
			}
		});
		mRg_showRb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			// 当RadioGroup中radiobutton改变时触发事件
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// jdk1.7后可以string
				switch (checkedId) {
				case R.id.main_rb_addBook:
					mVp_show.setCurrentItem(0);// 设置当前item
					reflash();
					mRb_addBook.setTextColor(Color.parseColor("#FF0000"));
					/*mRb_deleteBook.setTextColor(Color.parseColor("#000000"));
					mRb_changeBook.setTextColor(Color.parseColor("#000000"));
					mRb_queryBook.setTextColor(Color.parseColor("#000000"));*/
					break;
				case R.id.main_rb_deleteBook:
					mVp_show.setCurrentItem(1);
					reflash();
					//mRb_addBook.setTextColor(Color.parseColor("#000000"));
					mRb_deleteBook.setTextColor(Color.parseColor("#FF0000"));
					/*mRb_changeBook.setTextColor(Color.parseColor("#000000"));
					mRb_queryBook.setTextColor(Color.parseColor("#000000"));*/
					break;
				case R.id.main_rb_changeBook:
					mVp_show.setCurrentItem(2);
					reflash();
					/*mRb_addBook.setTextColor(Color.parseColor("#000000"));
					mRb_deleteBook.setTextColor(Color.parseColor("#000000"));*/
					mRb_changeBook.setTextColor(Color.parseColor("#FF0000"));
					/*mRb_queryBook.setTextColor(Color.parseColor("#000000"));*/
					break;
				case R.id.main_rb_queryBook:
					mVp_show.setCurrentItem(3);
					reflash();
					/*mRb_addBook.setTextColor(Color.parseColor("#000000"));
					mRb_deleteBook.setTextColor(Color.parseColor("#000000"));
					mRb_changeBook.setTextColor(Color.parseColor("#000000"));*/
					mRb_queryBook.setTextColor(Color.parseColor("#FF0000"));
					break;
				default:
					break;
				}
			}

			private void reflash() {
				mRb_addBook.setTextColor(Color.parseColor("#000000"));
				mRb_deleteBook.setTextColor(Color.parseColor("#000000"));
				mRb_changeBook.setTextColor(Color.parseColor("#000000"));
				mRb_queryBook.setTextColor(Color.parseColor("#000000"));
			}
			
		});
	}

	private void initAdapter() {
		mVp_show.setAdapter(new MainVpAdapter(getSupportFragmentManager(),
				mFragments));
	}

	private void initData() {
		mAddFragment = new AddFragment();
		mDeleteFragment = new DeleteFragment();
		mChangeFragment = new ChangeFragment();
		mQueryFragment = new QueryFragment();
		mFragments = new ArrayList<Fragment>();
		mFragments.add(mAddFragment);
		mFragments.add(mDeleteFragment);
		mFragments.add(mChangeFragment);
		mFragments.add(mQueryFragment);

	}

	private void initView() {
		mVp_show = (ViewPager) findViewById(R.id.main_vp);
		mRg_showRb = (RadioGroup) findViewById(R.id.main_rg);
		mRb_addBook = (RadioButton) findViewById(R.id.main_rb_addBook);
		mRb_deleteBook = (RadioButton) findViewById(R.id.main_rb_deleteBook);
		mRb_changeBook = (RadioButton) findViewById(R.id.main_rb_changeBook);
		mRb_queryBook = (RadioButton) findViewById(R.id.main_rb_queryBook);
	}

}


ChangeFragment.java(其他几个类似)

package com.book.fragment;

import com.briup.bookManager.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ChangeFragment extends Fragment{
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view=inflater.inflate(R.layout.fragment_change, container,false);
		return view;
	}
}
然后基本上就实现了,我们就可以在每个fragment里面写东西了。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:24651次
    • 积分:1391
    • 等级:
    • 排名:千里之外
    • 原创:114篇
    • 转载:24篇
    • 译文:0篇
    • 评论:3条
    文章分类
    最新评论