Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android fragments 如何在ViewPager中设置页面?_Android Fragments_Android Viewpager - Fatal编程技术网

Android fragments 如何在ViewPager中设置页面?

Android fragments 如何在ViewPager中设置页面?,android-fragments,android-viewpager,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(

我在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;

}
}
页面适配器:

  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;
}

}
进入此链接:
然后再次编写片段和适配器。

我不认识的解决方案。也许我表达错了。我想安装这两个函数,通过滑动OnClickListener来调用指定的页面。

源代码是什么样子的?