public class MainActivity extends AppCompatActivity{
private ViewPager viewPager;
private static final int FLAG = 0x123;
private EditText etname;
private RadioGroup radioGroup;
private ExpandableListView exlist;
private List<String> imgs;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case FLAG:
int code = (int) msg.obj;
viewPager.setCurrentItem(code);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewpager);
radioGroup = (RadioGroup) findViewById(R.id.radiog);
imgs = new ArrayList<String>();
imgs.add("http://img.juhe.cn/cookbook/s/1/45_d0170fbe236421f9.jpg");
imgs.add("http://img.juhe.cn/cookbook/s/1/45_639b12210745fa41.jpg");
imgs.add("http://img.juhe.cn/cookbook/s/1/45_c25e0cedd2012f45.jpg");
imgs.add("http://img.juhe.cn/cookbook/s/1/45_8e0cf83cb7306281.jpg");
addviewapger();
binner();
}
private void addviewapger() {
viewPager.setAdapter(new PagerAdapter() {
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.build();
@Override
public int getCount() {
return imgs.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(MainActivity.this);
ImageLoader.getInstance().displayImage(imgs.get(position), imageView, options);
container.addView(imageView);
return imageView;
}
});
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
for (int i = 0; i < imgs.size(); i++) {
RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
if (i == position) {
radioButton.setChecked(true);
} else {
radioButton.setChecked(false);
}
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void binner() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
private int index = 0;
@Override
public void run() {
if (index < imgs.size() - 1) {
index++;
} else {
index = 0;
}
Message message = new Message();
message.what = FLAG;
message.obj = index;
handler.sendMessage(message);
}
}, 0, 2000);
}
}
<?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.bwie.test.yuanyidong.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<RadioGroup
android:id="@+id/radiog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
</RelativeLayout>
<ExpandableListView
android:id="@+id/exlist"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</ExpandableListView>
</LinearLayout>