Android 에서 TabHost 와 ViewPager 를 결합 하여 첫 페이지 내 비게 이 션 효 과 를 실현 합 니 다.
먼저 효과 도 를 올 리 면 다음 과 같다.
코드 안에 주석 이 있 는데,단지 더 설명 할 뿐,몇 가지 주의해 야 할 문 제 를 말 할 뿐이다.
1:TabHost,TabWidget,FrameLayout 는 id 라 는 속성 을 반드시 추가 합 니 다.그렇지 않 으 면 오류 가 발생 합 니 다.
android:id=”@android:id/tabhost”
android:id=”@android:id/tabcontent”
android:id=”@android:id/tabs”
이 속성 은 고정 되 어 있다.
2:ViewPager 의 어댑터 는 Pager Adapter 를 계승 해 야 합 니 다.잘못 하지 마 세 요.
레이아웃 파일 xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.wgh.tabhostwithviewpager.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.0"
android:visibility="gone" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#0ff0f0" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="@drawable/img_draw_money_fail"
android:button="@null"
android:paddingLeft="10dp" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="@drawable/img_draw_money_fail"
android:button="@null" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="@drawable/img_draw_money_fail"
android:button="@null" />
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="@drawable/img_draw_money_fail"
android:button="@null" />
</RadioGroup>
</LinearLayout>
</TabHost>
Activity:
package com.example.wgh.tabhostwithviewpager;
import android.app.TabActivity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import java.util.ArrayList;
public class MainActivity extends TabActivity {
private TabHost tabHost = null;
private ViewPager viewPager = null;
private RadioGroup radioGroup = null;
private ArrayList<View> list = null;
private View view1 = null;
private View view2 = null;
private View view3 = null;
private View view4 = null;
private RadioButton radioButton1 = null;
private RadioButton radioButton2 = null;
private RadioButton radioButton3 = null;
private RadioButton radioButton4 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
//
radioGroup.check(R.id.radioButton1);
radioButton1.setBackgroundResource(R.drawable.img_draw_money_success);
viewPager.setCurrentItem(0);
tabHost.setCurrentTab(0);
//getViewPager
MyAdapter adapter = new MyAdapter(list);
viewPager.setAdapter(adapter);
/**
* viewPager , viewPager position, tabHost
*/
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == 0){
radioButton1.setBackgroundResource(R.drawable.img_draw_money_success);
radioButton2.setBackgroundResource(R.drawable.img_draw_money_fail);
radioButton3.setBackgroundResource(R.drawable.img_draw_money_fail);
radioButton4.setBackgroundResource(R.drawable.img_draw_money_fail);
}else if(position == 1){
radioButton1.setBackgroundResource(R.drawable.img_draw_money_fail);
radioButton2.setBackgroundResource(R.drawable.img_draw_money_success);
radioButton3.setBackgroundResource(R.drawable.img_draw_money_fail);
radioButton4.setBackgroundResource(R.drawable.img_draw_money_fail);
}else if(position == 2){
radioButton1.setBackgroundResource(R.drawable.img_draw_money_fail);
radioButton2.setBackgroundResource(R.drawable.img_draw_money_fail);
radioButton3.setBackgroundResource(R.drawable.img_draw_money_success);
radioButton4.setBackgroundResource(R.drawable.img_draw_money_fail);
}else if(position == 3){
radioButton1.setBackgroundResource(R.drawable.img_draw_money_fail);
radioButton2.setBackgroundResource(R.drawable.img_draw_money_fail);
radioButton3.setBackgroundResource(R.drawable.img_draw_money_fail);
radioButton4.setBackgroundResource(R.drawable.img_draw_money_success);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
/**
* radioGroup , ViewPager
*/
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
switch (checkedId){
case R.id.radioButton1:
viewPager.setCurrentItem(0);
radioButton1.setBackgroundResource(R.drawable.img_draw_money_success);
radioButton2.setBackgroundResource(R.drawable.img_draw_money_fail);
radioButton3.setBackgroundResource(R.drawable.img_draw_money_fail);
radioButton4.setBackgroundResource(R.drawable.img_draw_money_fail);
break;
case R.id.radioButton2:
viewPager.setCurrentItem(1);
radioButton1.setBackgroundResource(R.drawable.img_draw_money_fail);
radioButton2.setBackgroundResource(R.drawable.img_draw_money_success);
radioButton3.setBackgroundResource(R.drawable.img_draw_money_fail);
radioButton4.setBackgroundResource(R.drawable.img_draw_money_fail);
break;
case R.id.radioButton3:
viewPager.setCurrentItem(2);
radioButton1.setBackgroundResource(R.drawable.img_draw_money_fail);
radioButton2.setBackgroundResource(R.drawable.img_draw_money_fail);
radioButton3.setBackgroundResource(R.drawable.img_draw_money_success);
radioButton4.setBackgroundResource(R.drawable.img_draw_money_fail);
break;
case R.id.radioButton4:
viewPager.setCurrentItem(3);
radioButton1.setBackgroundResource(R.drawable.img_draw_money_fail);
radioButton2.setBackgroundResource(R.drawable.img_draw_money_fail);
radioButton3.setBackgroundResource(R.drawable.img_draw_money_fail);
radioButton4.setBackgroundResource(R.drawable.img_draw_money_success);
break;
}
}
});
}
/**
*
*/
private void initData() {
list = new ArrayList<View>();
list.add(view1);
list.add(view2);
list.add(view3);
list.add(view4);
}
/**
*
*/
private void initView() {
tabHost = getTabHost();
viewPager = (ViewPager) findViewById(R.id.viewPager);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
radioButton3 = (RadioButton) findViewById(R.id.radioButton3);
radioButton4 = (RadioButton) findViewById(R.id.radioButton4);
/**
* layout , list , return layout
*/
LayoutInflater inflater = LayoutInflater.from(this);
view1 = inflater.inflate(R.layout.layout_one,null);
view2 = inflater.inflate(R.layout.layout_two,null);
view3 = inflater.inflate(R.layout.layout_three,null);
view4 = inflater.inflate(R.layout.layout_four,null);
}
}
ViewPager 어댑터 MyAdapter:
public class MyAdapter extends PagerAdapter {
private ArrayList<View> list = null;
public MyAdapter(ArrayList<View> list) {
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(list.get(position));
return list.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(list.get(position));
}
}
위 에서 말 한 것 은 편집장 이 소개 한 Android 에서 TabHost 와 ViewPager 가 결합 하여 홈 페이지 내 비게 이 션 효 과 를 실현 하 는 것 입 니 다.여러분 에 게 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 메 시 지 를 남 겨 주세요.편집장 은 신속하게 답 해 드 리 겠 습 니 다.여기 서도 저희 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!