关闭

实现滑动Tab页面

标签: 滑动切换tab切换TabwidgtViewPagerFragmentActivity
184人阅读 评论(0) 收藏 举报
分类:


主Activity:

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


import com.example.fragment.Fragment01;
import com.example.fragment.Fragment02;
import com.example.fragment.Fragment03;
import com.example.fragment.Fragment04;


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.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TabWidget;


public class MainActivity extends FragmentActivity {
private ViewPager vp;
private TabWidget tab;
private List<Fragment> list = new ArrayList<Fragment>();


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
initTab();
}


private void init() {

vp = (ViewPager) findViewById(R.id.vp);

//添加自定义frament

list.add(new Fragment01());
list.add(new Fragment02());
list.add(new Fragment03());
list.add(new Fragment04());
MyAdapter adapter = new MyAdapter(getSupportFragmentManager(), list);
vp.setAdapter(adapter);
vp.setOnPageChangeListener(onPageChangeListener);
}


private void initTab() {
tab = (TabWidget) findViewById(R.id.tab);
tab.setCurrentTab(0);
findViewById(R.id.button1).setOnClickListener(onClickListener);
findViewById(R.id.button2).setOnClickListener(onClickListener);
findViewById(R.id.button3).setOnClickListener(onClickListener);
findViewById(R.id.button4).setOnClickListener(onClickListener);
}


private class MyAdapter extends FragmentStatePagerAdapter {
List<Fragment> list;


public MyAdapter(FragmentManager fm, List<Fragment> list) {
super(fm);
this.list = list;
}


@Override
public Fragment getItem(int position) {
return list.get(position);
}


@Override
public int getCount() {
return list.size();
}


}


private OnPageChangeListener onPageChangeListener = new OnPageChangeListener() {


@Override
public void onPageSelected(int index) {
tab.setCurrentTab(index);
}


@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {


}


@Override
public void onPageScrollStateChanged(int arg0) {


}
};


private OnClickListener onClickListener = new OnClickListener() {


@Override
public void onClick(View v) {
System.out.println(v.getId());
switch (v.getId()) {
case R.id.button1:
vp.setCurrentItem(0);
break;
case R.id.button2:
vp.setCurrentItem(1);
break;
case R.id.button3:
vp.setCurrentItem(2);
break;
case R.id.button4:
vp.setCurrentItem(3);
break;
}
}
};

}


页面布局文件:

<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"
    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="match_parent"
        android:layout_height="match_parent" >
    </android.support.v4.view.ViewPager>


    <TabWidget
        android:id="@+id/tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >


        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/transparent"
            android:text="first"
            android:textColor="@drawable/button_text_color" />


        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/transparent"
            android:text="second"
            android:textColor="@drawable/button_text_color" />


        <Button
            android:id="@+id/button3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/transparent"
            android:text="third"
            android:textColor="@drawable/button_text_color" />
        <Button
            android:id="@+id/button4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/transparent"
            android:text="four"
            android:textColor="@drawable/button_text_color" />
    </TabWidget>


</RelativeLayout>
0
0

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