Android交换标签

时间:2016-03-12 12:36:13

标签: android tabs

我正在尝试实施3个交换标签。我正在关注YouTube的一些教程。但我的代码运行不正常。我找到了很多解决方案,试过那些解决方案,做了完全相同的代码,但在运行我的项目时,它会崩溃。我的代码如下:

主java类

package io.github.hackimti.curiousity;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity implements TabListener {

    ActionBar actionBar;
    ViewPager viewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = (ViewPager)findViewById(R.id.main_pager1);
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        actionBar.addTab(actionBar.newTab().setText("Tab A").setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("Tab B").setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("Tab C").setTabListener(this));
    }


    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {

    }
}
class MyAdapter extends FragmentPagerAdapter{

    public MyAdapter(android.support.v4.app.FragmentManager fm) {
        super(fm);
    }
    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        if(position == 0){
            fragment = new FA();
        }
        if(position == 1){
            fragment = new FB();
        }
        if(position == 2){
            fragment = new FC();
        }
        return fragment;
    }
    @Override
    public int getCount() {
        return 3;
    }
}

主xml文件

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main_pager1">
</android.support.v4.view.ViewPager>

1 个答案:

答案 0 :(得分:0)

我认为你试图实现这个目标的方式是一种陈旧的方式。

对于带标签的滑动页面,请执行以下操作。

在github上下载或复制以下两个文件并粘贴您的项目。这与developers.google.com相同,但setDistributeEvenly方法除外。

https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/SlidingTabLayout.java

https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/SlidingTabStrip.java

activity_main.xml中

<your.package.name.SlidingTabLayout
    android:clickable="true"
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
 </your.package.name.SlidingTabLayout>

 <android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    />

MyAdapter.java(这里我只用了两页)

class MyPagerAdapter extends FragmentPagerAdapter 
{ 
    String[] title = {"All","Favourites"};
    public MyPagerAdapter(FragmentManager fm) { 
        super(fm); 
    } 
    @Override 
    public Fragment getItem(int position) {
        Fragment fragment=null; 
         if (position==0)
            fragment= new All(); 
             if (position==1)
                fragment= new Favourites(); 
        return fragment; 
    } 
    @Override 
     public int getCount() { 
        return 2; 
    } 
       @Override 
       public CharSequence getPageTitle(int position) {
             return title[position];
    } 
 } 

tab_view.xml(仅限标签视图,如果您需要,您也可以在此处使用ImageView)

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <TextView
        android:id="@+id/tab_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text=""
        android:padding="15dp"
        android:textStyle="bold"
        android:textSize="25dp"
        />
   </FrameLayout>

MainActivity.java

 private SlidingTabLayout tabLayout;
 private ViewPager pager;
 tabLayout= (SlidingTabLayout) findViewById(R.id.tabs); 
 pager = (ViewPager) findViewById(R.id.pager); 
 tabLayout.setCustomTabView(R.layout.tab_view,R.id.tab_title); 
 MyPagerAdapter adapter =  new MyPagerAdapter(getSupportFragmentManager()); 
 pager.setAdapter(adapter); 
 tabLayout.setDistributeEvenly(true); 
 tabLayout.setViewPager(pager); 

以上代码很好。但最新的方法是通过Android支持设计库来实现用页面滑动选项卡。