使用自定义适配器和扩展PagerAdapter的SmartTabLayout设置选项卡标题

时间:2015-12-01 09:12:59

标签: java android android-viewpager

我的ViewPager有一个自定义适配器,我想知道是否有任何方法可以使用SmartTabLayout来设置每个标签的设置标题?我已经在那里开了一张票,但我希望有人知道怎么做。以下是我的自定义适配器:

public class CustomPagerAdapter extends PagerAdapter {

    private int[] image_resources = {
            R.drawable.image1,
            R.drawable.image2,
            R.drawable.image3,
            R.drawable.image4,
            R.drawable.image5,
    };

    private Context ctx;
    private LayoutInflater layoutInflater;
    public CustomPagerAdapter(Context ctx) {
        this.ctx = ctx;
    }

    @Override
    public int getCount() {
        return image_resources.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return (view == (RelativeLayout) o);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View item_view = layoutInflater.inflate(R.layout.pager_item, container, false);
        ImageView imageview = (ImageView) item_view.findViewById(R.id.image_view);
        imageview.setImageResource(image_resources[position]);
        container.addView(item_view);
        return item_view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((RelativeLayout) object);
    }
}

这是我设置ViewPager的方式,也是我启动SmartTabLayout的地方:

 final ViewPager viewPager;
        CustomPagerAdapter adapter;
        viewPager = (ViewPager) findViewById(R.id.view_pager);
        adapter = new CustomPagerAdapter(context);
        viewPager.setAdapter(adapter);

        SmartTabLayout viewPagerTab = (SmartTabLayout) findViewById(R.id.viewpagertab);
        viewPagerTab.setViewPager(viewPager);

我想我也应该包括XML的相关部分:

<com.ogaclejapan.smarttablayout.SmartTabLayout
    android:id="@+id/viewpagertab"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="#fff"
    app:stl_indicatorAlwaysInCenter="false"
    app:stl_indicatorWithoutPadding="false"
    app:stl_indicatorInFront="false"
    app:stl_indicatorInterpolation="smart"
    app:stl_indicatorGravity="bottom"
    app:stl_indicatorColor="#33ffcc"
    app:stl_indicatorThickness="4dp"
    app:stl_indicatorCornerRadius="2dp"
    app:stl_overlineColor="#4D000000"
    app:stl_overlineThickness="0dp"
    app:stl_underlineColor="#4D000000"
    app:stl_underlineThickness="1dp"
    app:stl_dividerColor="#4D000000"
    app:stl_dividerThickness="1dp"
    app:stl_defaultTabBackground="?attr/selectableItemBackground"
    app:stl_defaultTabTextAllCaps="true"
    app:stl_defaultTabTextColor="#FC000000"
    app:stl_defaultTabTextSize="12sp"
    app:stl_defaultTabTextHorizontalPadding="16dp"
    app:stl_defaultTabTextMinWidth="0dp"
    app:stl_distributeEvenly="false"
    app:stl_clickable="true"
    app:stl_titleOffset="24dp"
    app:stl_drawDecorationAfterTab="false"
    />

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="500dp"
    android:clickable="false"
    android:layout_below="@id/viewpagertab"
    android:scaleType="centerCrop" />

它实际上工作正常,但我无法使用getPageTitle()以标准方式设置标题,因此它们显示为空白。我想继续使用SmartTabLayout,因为它具有美学上的吸引力。默认的寻呼机标题是可怕的!

2 个答案:

答案 0 :(得分:2)

据我了解文档,您需要FragmentPagerItemAdapter并致电.add("title", class)

FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
     getSupportFragmentManager(), FragmentPagerItems.with(this)
     .add(R.string.titleA, PageFragment.class) // I assume titles are set here.
     .add(R.string.titleB, PageFragment.class)
     .create());

那么,您只需要致电adapter.add(...)

编辑: 查看演示应用程序 - 我认为您需要扩展FragmentPagerItemAdapter而不是简单PagerAdapter

答案 1 :(得分:2)

显然它可以通过单独扩展PagerAdapter来工作。因此,在此上下文中的正确答案是覆盖自定义分页器适配器类CustomPagerAdapter中的getPageTitle():

@Override
public CharSequence getPageTitle(int position) {
    return title_resources[position];
}

您可以返回String []数组,以便为​​每个选项卡提供标题。只需确保您的标题数量等于ViewPager中的图像或视图数量:

private String[] title_resources = {
        "Title 1",
        "Title 2",
        "Title 3",
        "Title 4",
        "Title 5",
};