package com.jereh.helloworld.activity.day12;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.jereh.helloworld.R;
import com.jereh.helloworld.activity.adapter.GuidePagerAdapter;
import com.jereh.helloworld.activity.entity.NewsType;
import java.util.ArrayList;
import java.util.List;
public class Day12Test3Activity extends Activity {
private List<NewsType> list;
private RadioGroup radioGroup;
private ViewPager viewPager;
private int[] imgs = {
R.drawable.wangfei1,
R.drawable.wangfei2,
R.drawable.wangfei3,
R.drawable.wangfei4,
R.drawable.wangfei5,
R.drawable.wangfei6,
R.drawable.wangfei7};
private List<ImageView> list1 = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_day12_test3);
viewPager = (ViewPager)findViewById(R.id.viewPager);
for (int i : imgs) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(i);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
list1.add(imageView);
}
viewPager.setAdapter(new GuidePagerAdapter(list1));
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
RadioButton radioButton = (RadioButton)radioGroup.getChildAt(position);
radioButton.setChecked(true);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
initNewsType();
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
viewPager.setCurrentItem(checkedId);
}
});
initNewsTypeUi();
}
private void initNewsTypeUi(){
LayoutInflater inflater = LayoutInflater.from(this);
radioGroup = (RadioGroup)findViewById(R.id.tabs);
for (int i = 0;i<list.size();i++) {
RadioButton radioButton =(RadioButton)
inflater.inflate(R.layout.news_type_radiobutton,null,false);
radioButton.setId(i);
radioButton.setText(list.get(i).getName());
if (i==0){
radioButton.setChecked(true);
}
radioGroup.addView(radioButton);
}
}
private void initNewsType(){
list = new ArrayList<>();
list.add(new NewsType(1,"推荐"));
list.add(new NewsType(2,"热点"));
list.add(new NewsType(3,"社会"));
list.add(new NewsType(4,"国际"));
list.add(new NewsType(5,"国内"));
list.add(new NewsType(6,"娱乐"));
list.add(new NewsType(7,"体育"));
list.add(new NewsType(8,"科技"));
list.add(new NewsType(9,"时尚"));
list.add(new NewsType(10,"生活"));
}
}