【问题标题】:Divider not working for TitlePageIndicator with viewpagerindicator & actionbarsherlock分隔线不适用于带有 viewpagerindicator 和 actionbarsherlock 的 TitlePageIndicator
【发布时间】:2013-02-19 18:49:45
【问题描述】:

我注意到最新版本的 ViewPagerIndicator 支持 ICS 样式分隔符,我尝试关注问题和解决方案,但无论我做什么,我都无法让分隔符显示在操作栏上的选项中TitlePageIndicator。我添加了 IcsLayout 作为容器,放置了分隔符、showDividers 和其他属性,但仍然一无所获。这是我的布局(奇怪的是,如果我将 IcsLayout 切换到 viewpager 指示器之一,应用程序就会崩溃):

<com.actionbarsherlock.internal.widget.IcsLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.sosick.android.brink"
    android:layout_width="match_parent"
    android:divider="#ffffff"
    android:showDividers="middle"
    android:dividerPadding="8dp"
    android:dividerHeight="10dp"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.viewpagerindicator.TitlePageIndicator
        android:id="@+id/tpi_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        textColor="@color/text_light"
        android:background="@drawable/ab_stacked_solid_brink"
        app:topPadding="10dp"
        app:footerPadding="15dp"
        app:footerColor="#a4ded7"
        app:footerIndicatorHeight="2dp"
        app:footerIndicatorStyle="underline"
        app:footerLineHeight="2dp"
        app:selectedBold="false" />

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

</com.actionbarsherlock.internal.widget.IcsLinearLayout>

【问题讨论】:

  • 阅读页面似乎他们声明它可以与 TabPageIndicator 一起使用,所以我不确定 TitleIndicator 是否可以...?
  • 请张贴草图或参考,以便更好地了解您想要实现的目标。谢谢。
  • 如果您查看此应用程序,请注意 Title Pager 上的垂直分隔线,这就是我想要实现的目标。 24.media.tumblr.com/8044b5ed0a7f8ad04468def5973275d7/…
  • 标签之间需要竖线吗?对吗?
  • 是的,这正是我正在寻找的,但在 ActionBarSherlock 和 ViewPager 的上下文中。

标签: android actionbarsherlock android-viewpager viewpagerindicator


【解决方案1】:

您需要在样式中声明分隔参数,而不是在布局 xml 中。

../res/values/styles.xml:

<style name="StyledIndicators" parent="@android:style/Theme.Light">
    <item name="vpiIconPageIndicatorStyle">@style/CustomIconIndicator</item>
</style>

<style name="CustomIconIndicator" parent="Widget.TabPageIndicator">
    <item name="android:divider">@drawable/custom_tab_indicator_divider</item>
    <item name="android:showDividers">middle</item>
    <item name="android:dividerPadding">10dp</item>
</style>

在您的活动清单中:

<activity
        android:name=".SampleIconsDefault"
        android:label="Icons/Default"
        android:theme="@style/StyledIndicators"> 
</activity>

【讨论】:

  • 此解决方案需要 API 级别 11,还有其他适用于 API 级别 8 的方法吗?
【解决方案2】:

以下代码sn-ps主要来自Android Developer站点。还有一个非常好的示例应用程序/项目。

使用ViewPage 作为布局的容器,例如res/layout/activity_main.xml:

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

在您的类中实现 ABS'ActionBar.TabListener,从支持库添加和扩展 FragmentPagerAdapterFragmentStatePagerAdapter,例如:

public class TheDesertFoxActivity extends SherlockFragmentActivity
        implements ActionBar.TabListener {

    private AppSectionsPagerAdapter mAppSectionsPagerAdapter;
    private ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());

        // Set up action bar
        final ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Set up the ViewPager, attaching the adapter
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mAppSectionsPagerAdapter);
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                // When swiping between different app sections, select the corresponding tab
                actionBar.setSelectedNavigationItem(position);
            }
        });

        // For each of the sections in the app, add a tab to the action bar
        for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
            // Create a tab with text corresponding to the page title defined by the adapter.
            // Also specify this Activity object, which implements the TabListener interface, as the
            // listener for when this tab is selected.
            actionBar.addTab(actionBar.newTab()
                    .setText(mAppSectionsPagerAdapter.getPageTitle(i))
                    .setTabListener(this));
        }
    }

    public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int i) {
            switch (i) {

                Fragment fragment = new MyTabsFragment();
                Bundle args = new Bundle();
                args.putInt(MyTabsFragment.TAB_ID, i);
                fragment.setArguments(args);
                return fragment;
            }
        }

        @Override
        public int getCount() {
            return <YOUR_TAB_COUNT>;
        }
    }

    public static class MyTabsFragment extends Fragment {

        public static final String TAB_ID = "tab_id";

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            int layoutId = -1;
            switch (getArguments(getInt(TAB_ID))) {
                case 0: 
                    layoutId = R.layout.fragment_first_tab;
                    break;
                case 1:
                    layoutId = R.layout.fragment_second_tab;
                    break;
                ...
                ...
                ...
                default:
                    layoutId = R.layout.fragment_default_tab;

            }
            View rootView = inflater.inflate(layoutId, container, false);
            return rootView;
        }
    }
}

就是这样。分隔线将由ViewPager 为您放置。您不必自己添加它们。

【讨论】:

    猜你喜欢
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多