1
以下は私のカスタムページのコードです。私は最初のページにラジオボタンを持っています。ラジオボタンが選択されているときに、2番目のページをユーザーに表示する方法。ここviewPagerのページ番号を変更してください
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());
}
}
おかげ。私は自分のアクティビティでゲッターとセッターを作成し、それを私のカスタムページビューアーで使用しました – LearningPhase