ModelClass
public enum ModelObject {
RED(R.string.red, R.layout.view_red),
BLUE(R.string.blue, R.layout.view_blue);
private int mTitleResId;
private int mLayoutResId;
ModelObject(int titleResId, int layoutResId) {
mTitleResId = titleResId;
mLayoutResId = layoutResId;
}
public int getTitleResId() {
return mTitleResId;
}
public int getLayoutResId() {
return mLayoutResId;
}
}
此活动
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
RadioGroup radioGroup;
MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager)findViewById(R.id.viewpager1);
radioGroup = (RadioGroup)findViewById(R.id.bottomRadioGroup);
myAdapter = new MyAdapter(this);
viewPager.setAdapter(myAdapter);
RadioButton btn =(RadioButton)(radioGroup.getChildAt(0));
btn.setChecked(true);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
if(checkedId == R.id.Radio1) {
viewPager.setCurrentItem(0);
}else if(checkedId == R.id.Radio2){
viewPager.setCurrentItem(1);
}
}
});
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
RadioButton btn =(RadioButton)(radioGroup.getChildAt(position));
btn.setChecked(true);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private class MyAdapter extends PagerAdapter{
Context context;
public MyAdapter(Context context) {
super();
this.context = context;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ModelObject modelObject = ModelObject.values()[position];
LayoutInflater inflater = LayoutInflater.from(context);
ViewGroup layout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), container, false);
// TextView textView=(TextView) itemView.findViewById(R.id.textView);
// TextView textView9=(TextView) itemView.findViewById(R.id.textView9);
// TextView textView3=(TextView) itemView.findViewById(R.id.textView3);
// textView.setText("dsjhfkjsdhfjkdshfjkdshfjkdshfjkhdskjfhdskjr");
// textView9.setText("sjkabdjkashdjksahdjksahdjksahdk");
// textView3.setText("sadhasjkdhjkashdjkashdjksa");
container.addView(layout);
return layout;
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
@Override
public int getCount() {
return ModelObject.values().length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public CharSequence getPageTitle(int position) {
ModelObject customPagerEnum = ModelObject.values()[position];
return context.getString(customPagerEnum.getTitleResId());
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.satti.radiogroupviewpager.MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager1"
android:layout_width="match_parent"
android:layout_height="200dp" />
<RadioGroup
android:id="@+id/bottomRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:gravity="center_vertical"
android:layout_marginBottom="2dp">
<RadioButton
android:id="@+id/Radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="5dp"/>
<RadioButton
android:id="@+id/Radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false" />
</RadioGroup>
</LinearLayout>
view_red.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
view_blue.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
这是我使用此代码的代码,我能够设置视图,我正在尝试以编程方式在TextView中打印数据InstantiateItem方法,但我正在获取异常char null实例,并且应用程序崩溃了,请建议我如何在Textview中以编程方式设置数据view_red布局和view_red布局TextView的PagerAdapter。我编写了以编程方式打印数据的代码,但是它无法正常工作,所以我对代码进行了注释。