Android viewpager pagertitlestrip按位置点击

时间:2014-08-15 12:01:42

标签: android android-viewpager android-support-library

我正在使用viewpager和pagertitlestrip.i成功创建它,我想在我的viewpager中添加视图。这是我的来源

 private ViewPager mViewPager;
 TextView text;
 private PagerTitleStrip mPagerTitleStrip;
 View view1;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_main);
    mViewPager = (ViewPager)findViewById(R.id.viewpager);
    mPagerTitleStrip = (PagerTitleStrip)findViewById(R.id.pagertitle);

    //View
    LayoutInflater mLi = LayoutInflater.from(this);
    view1 = mLi.inflate(R.layout.activity_main, null);
    text=(TextView)view1.findViewById(R.id.textView1);

    //Title




    final ArrayList<View> views = new ArrayList<View>();


    final ArrayList<String> titles = new ArrayList<String>();


    for (int i = 0; i < 10; i++) {
         view1 = mLi.inflate(R.layout.activity_main, null);
         text=(TextView)view1.findViewById(R.id.textView1);

         text.setText("Page"+i);
         views.add(view1);
          titles.add("title"+i);


    }
    mPagerTitleStrip.setTextColor(Color.BLUE);

    //ViewPager
    PagerAdapter mPagerAdapter = new PagerAdapter() {

           @Override
           public boolean isViewFromObject(View arg0, Object arg1) {
                return arg0 == arg1;
           }

           @Override
           public int getCount() {
                return views.size();
           }

           @Override
           public void destroyItem(View container, int position, Object object) {
                ((ViewPager)container).removeView(views.get(position));
           }

           @Override
           public CharSequence getPageTitle(int position) {
                return titles.get(position);
           }

           @Override
           public Object instantiateItem(View container, int position) {
                ((ViewPager)container).addView(views.get(position));
                return views.get(position);
           }
      };

      mViewPager.setAdapter(mPagerAdapter);
}

}

现在我尝试编写pagertitlestrip的onclicklistener.for更多信息,如果我点击4的标题我将开始(显示)第4个view.onclick听众按位置 我怎么能解决我的问题? 如果有人知道解决方案,请帮助我

1 个答案:

答案 0 :(得分:0)

如果您想通过点击某个指示器手动设置视图,可以使用此

mViewPager.setCurrentItem(num);

但如果您想在选择某个页面时执行某些操作,则可以使用

ViewPager.SimpleOnPageChangeListener

听众理解你可以遵循这个

ViewPager.SimpleOnPageChangeListener

希望这会有所帮助