本文介绍了PagerSlidingTabStrip实施指南的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用这个库在我的应用程序: https://github.com/astuetz/PagerSlidingTabStrip

我阅读文档,但我不明白anything.I有两个片段,所以我希望把两个标签以我app.Where做我把viewpager XML?

我在哪里可以把这个code座:

  //初始化ViewPager并设置一个适配器
 ViewPager寻呼机=(ViewPager)findViewById(R.id.pager);
 pager.setAdapter(新TestAdapter(getSupportFragmentManager()));

 //绑定的选项卡的ViewPager
 PagerSlidingTabStrip标签=(PagerSlidingTabStrip)findViewById(R.id.tabs);
 tabs.setViewPager(寻呼机);
 

只是主要活动或所有碎片和主要活动? (我有同样的问题对viewpager XML)任何人都可以向我解释,我怎么能实现这一步我的应用程序一步?

诗: https://github.com/astuetz/PagerSlidingTabStrip/tree/master/样本这就是例子code。

解决方案

我只是使它的两个选项卡,你问!

0)添加到库构建路径

1)创建两个片段

 公共类碎裂扩展片段{
    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
        返回inflater.inflate(R.layout.fragment_a,集装箱,假);
    }
}
 

 公共类FragmentB扩展片段{
    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
        返回inflater.inflate(R.layout.fragment_b,集装箱,假);
    }
}
 

及其布局例如可以是:

 < XML版本=1.0编码=UTF-8&GT?;

< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent机器人:layout_height =match_parent
    机器人:后台=#FFFF00>

    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:textAppearance =机器人:ATTR / textAppearanceLarge
        机器人:文本=这是片段A
        机器人:ID =@ + ID / TextView的
        机器人:重力=中心
        机器人:layout_alignParentTop =真
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentRight =真
        机器人:layout_alignParentBottom =真/>
< / RelativeLayout的>
 

2)创建MainActivity布局:

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>

    < com.astuetz.PagerSlidingTabStrip
        机器人:ID =@ + ID /标签
        机器人:layout_width =match_parent
        机器人:layout_height =48dip
        />

    < android.support.v4.view.ViewPager
        机器人:ID =@ + ID /寻呼机
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID /标签
        工具:上下文=。MainActivity/>

< / RelativeLayout的>
 

3)创建viewpager适配器

 公共类MyPagerAdapter扩展FragmentPagerAdapter {

    公共MyPagerAdapter(FragmentManager FM){
        超(FM);
    }
    @覆盖
    公共CharSequence的getPageTitle(INT位置){
        返程(位置== 0)? 表一:TAB2;
    }
    @覆盖
    公众诠释getCount将(){
       返回2;
    }
   @覆盖
   公共片段的getItem(INT位置){
      回报收益(位置== 0)?新碎裂():新FragmentB();
   }
}
 

3)指定适配器将viewpager和viewpager到 PagerSlidingTabStrip 在MainActivity

  @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

         ViewPager寻呼机=(ViewPager)findViewById(R.id.pager);
         pager.setAdapter(新MyAdapter(getSupportFragmentManager()));

         //绑定的选项卡的ViewPager
         PagerSlidingTabStrip标签=(PagerSlidingTabStrip)findViewById(R.id.tabs);
         tabs.setViewPager(寻呼机);
    }
 

4)运行

I am trying to use this library in my app:https://github.com/astuetz/PagerSlidingTabStrip

I read the documents but I didn't understand anything.I have two fragments so I want to place two tabs to my app.Where do I put the viewpager xml ?

Where do I put the this code block:

// Initialize the ViewPager and set an adapter
 ViewPager pager = (ViewPager) findViewById(R.id.pager);
 pager.setAdapter(new TestAdapter(getSupportFragmentManager()));

 // Bind the tabs to the ViewPager
 PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
 tabs.setViewPager(pager); 

Just main activity or all fragments and main activity ? (I have same question for viewpager xml) Can anyone explain to me how can I implement this to my app step by step ?

Ps:https://github.com/astuetz/PagerSlidingTabStrip/tree/master/sample This is the example code.

解决方案

I just make it for two tabs as you asked!

0) Add the library to your build path

1) Create your two fragments

public class FragmentA extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_a,container,false);
    }
}

and

public class FragmentB extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_b,container,false);
    }
}

and their layouts for example can be:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="#FFFF00">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="This is Fragment A"
        android:id="@+id/textView"
        android:gravity="center"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true" />
</RelativeLayout>

2) Create MainActivity layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.astuetz.PagerSlidingTabStrip
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="48dip"
        />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tabs"
        tools:context=".MainActivity" />

</RelativeLayout>

3) Create your viewpager adapter

public class MyPagerAdapter extends FragmentPagerAdapter {

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public CharSequence getPageTitle(int position) {
        return (position == 0)? "Tab 1" : "Tab2" ;
    }
    @Override
    public int getCount() {
       return 2;
    }
   @Override
   public Fragment getItem(int position) {
      return return (position == 0)? new FragmentA() : new FragmentB() ;
   }
}

3) Assign adapter to your viewpager and the viewpager to the PagerSlidingTabStrip at the MainActivity

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

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

         // Bind the tabs to the ViewPager
         PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
         tabs.setViewPager(pager); 
    }

4) Run

这篇关于PagerSlidingTabStrip实施指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 07:19