如何在ViewPager中设置页面?

时间:2014-08-25 18:57:54

标签: android-fragments android-viewpager

我在ViewPager中有页面(片段)。我想从当前页面(例如第1页)更改为第3页。是否可以使用onClickListener更改页面?

这是我的代码:

package de.manuel.balancescoretraining;


 public class Balancescore extends Fragment {

 public Balancescore() {

 }


ViewPager viewPager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.start_page, container, false);


    BalancescorePageAdapter adapter = new BalancescorePageAdapter(this.getActivity());
    viewPager = (ViewPager) view.findViewById(R.id.viewPager);
    viewPager.setAdapter(adapter);

    viewPager.setCurrentItem(0);

    return view;

}
}

PageAdapter:

  package de.manuel.balancescoretraining;

   public class BalancescorePageAdapter extends PagerAdapter {

Context context;

public BalancescorePageAdapter(Context context) {
    this.context = context;
}

TextView titel;
TextView content;
TextView aContent;
TextView pageOf;
ImageView picture;
ImageView imageDirection;
CharSequence htmlTitle; 
CharSequence htmlContent; 

public int page;
int maxPage = 20;
TextView question;


public int getCount() {
    return 3;

}

public Object instantiateItem(View collection, int position) {

    LayoutInflater inflater = (LayoutInflater) collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    int resId = 0;

    switch (position) {
    case 0:
        resId = R.layout.a_titel_content;
        page = 1;
        break;
    case 1:
        resId = R.layout.a_titel_content;
        page = 2;
        break;
    case 2:
        resId = R.layout.a_content;
        page = 3;
        break;

    }

    View view = inflater.inflate(resId, null);

    if (page == 1) {
        titel = (TextView) view.findViewById(R.id.a_ti_titel);
        titel.setText("Balancescore");

        content = (TextView) view.findViewById(R.id.a_ti_inhalt);
        htmlTitle = Html.fromHtml("Some text...");

        content.setText(htmlTitle);

    } else if (page == 2) {

        titel = (TextView) view.findViewById(R.id.a_ti_titel);
        titel.setText("1. Header");

        content = (TextView) view.findViewById(R.id.a_ti_inhalt);
        htmlTitle = Html
                .fromHtml("Some text...");

        content.setText(htmlTitle);
        pageOf = (TextView) view
                .findViewById(R.id.a_ti_seite_von_bis);
        pageOf.setText("1 / " + maxPage);

    } else if (page == 3) {

        aContent = (TextView) view.findViewById(R.id.a_i_inhalt);
        htmlContent = Html
                .fromHtml("Some text...");

        aContent.setText(htmlContent);

        pageOf = (TextView) view

        .findViewById(R.id.a_i_inhalt_seite_von_bis);
        pageOf.setText("2 / " + maxPage);

    }


    ((ViewPager) collection).addView(view, 0);

    return view;

}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager) arg0).removeView((View) arg2);
}

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

@Override
public Parcelable saveState() {
    return null;
}

}

2 个答案:

答案 0 :(得分:0)

进入以下链接: http://developer.android.com/training/animation/screen-slide.html 并再次写你的片段和适配器。

答案 1 :(得分:0)

我不认识的解决方案。也许我表达自己错了。我想安装两个函数来刷一个OnClickListener来调用指定的页面。