更改viewPager的页码

时间:2016-05-06 23:02:44

标签: android android-viewpager

以下是我的custompageadapter代码。我的第一页上有单选按钮。当选择任何单选按钮时,如何向用户显示第二页。

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class CustomPageAdapter extends PagerAdapter {

    private Context mContext;
    ViewGroup layout;

    public CustomPageAdapter(Context context) {
        mContext = context;
    }

    @Override
    public Object instantiateItem(ViewGroup collection, int position) {
        final CustomPagerEnum customPagerEnum = CustomPagerEnum.values()[position];
        LayoutInflater inflater = LayoutInflater.from(mContext);
        layout = (ViewGroup) inflater.inflate(customPagerEnum.getLayoutResId(), collection, false);
        switch (position) {
            case 0:
                final RadioGroup radio = (RadioGroup) layout.findViewById(R.id.radioOption);
                radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {

                        View radioButton = radio.findViewById(checkedId);
                        int index = radio.indexOfChild(radioButton);

                        // Add logic here

                        switch (index) {
                            case 0: // first button

                                Toast.makeText(mContext, "sdfdsfd"+index , Toast.LENGTH_LONG).show();


                            case 1: // secondbutton

                                Toast.makeText(mContext, "sdfsdds"+index , Toast.LENGTH_LONG).show();


                        }
                    }
                });
                break;
            case 1:

                break;
        }

        collection.addView(layout);

        return layout;
    }



    @Override
    public void destroyItem(ViewGroup collection, int position, Object view) {
        collection.removeView((View) view);
    }

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

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

    @Override
    public CharSequence getPageTitle(int position) {
        CustomPagerEnum customPagerEnum = CustomPagerEnum.values()[position];
        return mContext.getString(customPagerEnum.getTitleResId());
    }


}

1 个答案:

答案 0 :(得分:1)

以下是使用动态单选按钮作为viewpager中的指示符的示例: (此示例来自片段,因此只需调整您的使用情况)

final RadioGroup radioGroup = (RadioGroup) rootView.findViewById(R.id.radiogroup);

    final RadioButton[] rb = new RadioButton[items.length];

    for (int i = 0; i < items.length; i++) {
        final int j = i;
        rb[i] = new RadioButton(rootView.getContext());
        radioGroup.addView(rb[i]);
        rb[i].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(items.length - j - 1);
            }
        });
    }