如何在多个片段中的ActionTab和片段中实现滑动视图?

时间:2015-05-26 08:02:13

标签: android android-fragments

我正在处理Actionbar.TabListenerFragments 标签。

片段包含多个Fragments,这些Fragments是通过每个标签MainActivity上的可点击按钮生成的。

我的import android.app.ActionBar; import android.app.Fragment; import android.os.Bundle; import android.app.Activity; public class MainActivity extends Activity { // Declare Tab Variable ActionBar.Tab Tab1, Tab2, Tab3; Fragment fragmentTab1 = new FragmentTab1(); Fragment fragmentTab2 = new Mcx(); Fragment fragmentTab3 = new FragmentTab3(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ActionBar actionBar = getActionBar(); // Hide Actionbar Icon actionBar.setDisplayShowHomeEnabled(false); // Hide Actionbar Title actionBar.setDisplayShowTitleEnabled(false); // Create Actionbar Tabs actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); // Set Tab Icon and Titles Tab1 = actionBar.newTab().setText("A"); Tab2 = actionBar.newTab().setText("B"); Tab3 = actionBar.newTab().setText("C"); // Set Tab Listeners Tab1.setTabListener(new TabListener(fragmentTab1)); Tab2.setTabListener(new TabListener(fragmentTab2)); Tab3.setTabListener(new TabListener(fragmentTab3)); // Add tabs to actionbar actionBar.addTab(Tab1); actionBar.addTab(Tab2); actionBar.addTab(Tab3); } } 和其他文件如下所示:

我的主要活动如下:

TabListener

extends ActionBar.Tablistener文件import android.app.ActionBar.Tab; import android.app.Fragment; import android.app.FragmentTransaction; import android.app.ActionBar; public class TabListener implements ActionBar.TabListener { Fragment fragment; public TabListener(Fragment fragment) { // TODO Auto-generated constructor stub this.fragment = fragment; } @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { // TODO Auto-generated method stub ft.replace(R.id. fragment_container, fragment); } @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { // TODO Auto-generated method stub ft.remove(fragment); } @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { // TODO Auto-generated method stub } }

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" /> 配置如下:

ActionTab

问题:

鉴于我上面已实现的内容,我如何才能将Fragment Viewfor(int j = 0; j < NUM_DAYS; j++){ if(bookings[i][j] == null){ days++; if(daysWanted >= days && this.bookings[i] == this.bookings[i]){ this.displayCell (i, j, Color.red); } } } 作为滑动视图实施?

1 个答案:

答案 0 :(得分:0)

经过对谷歌的一些研究后发现,我们可以通过使用ViewPager实现Tab片段的滑动视图

转到Android Studio并选择TabActivity(使用ViewPager模板滑动)

首先我们必须使用MainActivity扩展fragmentAcitvity,这应该用ActionBar.TabListener实现

守则如下......

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

@SuppressWarnings("ALL")
public class MainActivity extends FragmentActivity implements ActionBar.TabListener{
    ActionBar actionbar;
    public static ViewPager viewpager;
    FragmentPageAdapter ft;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewpager = (ViewPager) findViewById(R.id.pager);
        ft = new FragmentPageAdapter(getSupportFragmentManager());
        actionbar = getActionBar();
        viewpager.setAdapter(ft);

        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionbar.addTab(actionbar.newTab().setText("TAB1").setTabListener(this));
        actionbar.addTab(actionbar.newTab().setText("TAB2").setTabListener(this));
        actionbar.addTab(actionbar.newTab().setText("TAB3").setTabListener(this));
        actionbar.addTab(actionbar.newTab().setText("TAB").setTabListener(this));
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int arg0) {
                actionbar.setSelectedNavigationItem(arg0);
            }
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }
            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub
            }
        });
    }
    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
    }
    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        viewpager.setCurrentItem(tab.getPosition());
    }
    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
    }
}

MainActivity的XML .... activity_mail.xml将是......

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

FragmentpageAdapter

public class FragmentPageAdapter extends FragmentPagerAdapter {

    public FragmentPageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int arg0) {
        switch (arg0){
            case 0: return new Tab1Fragment();

            case 1: return new Tab2Fragment();

            case 2: return new Tab3Fragment();

            case 3: return new Tab4Fragment();

            default:break;
        }
        return null;
    }

    @Override
    public int getCount() {
        return 4;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return getPageTitle(4);
    }
}

如果您按照上面提到的在Android Studio中使用默认模板,那么还会有一个fragment_main文件...

    <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:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".MainActivity$PlaceholderFragment">

        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/toolbar"
            android:minHeight="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            android:popupTheme="@style/ThemeOverlay.AppCompat.Light">
        </android.support.v7.widget.Toolbar>

    </RelativeLayout>

之后,使用您在FragmentPagerAdapter和MainActivity中声明并添加的相关选项卡实现所有片段。

您必须在Androidmenifest.xml中设置活动主题

"@android:style/Theme.Holo.Light"

以及style.xml

<style name="AppTheme" parent="Theme.AppCompat">