仅用ViewPager实现,下面的tab键用RadioGroup实现,在滑动的时候设置监听切换RadioButton的选中状态
布局代码
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<include layout="@layout/bottom_menu" />
</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="wrap_content"
android:background="@android:color/darker_gray"
android:orientation="vertical">
<RadioGroup
android:id="@+id/rg_bottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="false"
android:drawableTop="@mipmap/ic_launcher"
android:gravity="center"
android:padding="5dp"
android:text="item1"
android:textColor="@drawable/bottom_text_selector" />
<RadioButton
android:id="@+id/rb_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="false"
android:drawableTop="@mipmap/ic_launcher"
android:gravity="center"
android:padding="5dp"
android:text="item2"
android:textColor="@drawable/bottom_text_selector" />
<RadioButton
android:id="@+id/rb_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="false"
android:drawableTop="@mipmap/ic_launcher"
android:gravity="center"
android:padding="5dp"
android:text="item3"
android:textColor="@drawable/bottom_text_selector" />
</RadioGroup>
</LinearLayout>
实现代码:
package com.example.baidu.mydemos.demo1;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.TextView;
import com.example.baidu.mydemos.R;
import java.util.ArrayList;
/**
* Created by baidu on 15/12/24.
*/
public class DemoOneMenuOneActivity extends Activity {
private ArrayList<View> views;
private TextView tv_content;
private RadioGroup rg_bottom;
private android.support.v4.view.ViewPager vp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo1_1);
vp = (ViewPager) findViewById(R.id.vp);
rg_bottom = (RadioGroup) findViewById(R.id.rg_bottom);
initviews();
vp.setAdapter(new MyAdapter());
rg_bottom.check(R.id.rb_1);
rg_bottom.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rb_1:
vp.setCurrentItem(0);
break;
case R.id.rb_2:
vp.setCurrentItem(1);
break;
case R.id.rb_3:
vp.setCurrentItem(2);
break;
}
}
});
vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
switch (position) {
case 0:
rg_bottom.check(R.id.rb_1);
break;
case 1:
rg_bottom.check(R.id.rb_2);
break;
case 2:
rg_bottom.check(R.id.rb_3);
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
class MyAdapter extends PagerAdapter {
@Override
public int getCount() {
return views.size();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = views.get(position);
container.addView(view);
return view;
}
@Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(views.get(position));
}
}
public void initviews() {
views = new ArrayList<View>();
for (int i = 1; i <= 3; i++) {
View view = View.inflate(DemoOneMenuOneActivity.this, R.layout.demo1_content, null);
tv_content = (TextView) view.findViewById(R.id.tv_content);
tv_content.setText(i + "");
views.add(view);
}
}
}