有 四个 知识点 :
0:ViewPager的PagerAdapter,里面的 list 是使用<Fresco>的 SimpleDreweeView,同时 方法instantiateItem 是 实例化的意思
viewPager = (ViewPager) findViewById(R.id.viewPager); listUrl = new ArrayList<String>(); listUrl.add("http://fdfs.xmcdn.com/group16/M08/F1/13/wKgDbFal40bR7Uc6AAH3JpWhLiQ015_android_large.jpg"); listUrl.add("http://fdfs.xmcdn.com/group10/M07/F0/14/wKgDaVal9ZLTP5q1AAFIJeYaktQ092_android_large.jpg"); listUrl.add("http://fdfs.xmcdn.com/group12/M07/E8/35/wKgDXFacqEfReClFAAFvbZHe_mU331_android_large.jpg"); listUrl.add("http://fdfs.xmcdn.com/group9/M05/EE/15/wKgDZlagtF_yH9YXAAEyq6YSxDo657_android_large.jpg"); listUrl.add("http://fdfs.xmcdn.com/group11/M07/FC/B4/wKgDbValyzzy0fBpAAMdsEAuI-Q295_android_large.jpg"); listUrl.add("http://fdfs.xmcdn.com/group9/M01/EF/02/wKgDZlaiCqbTzvIzAAH_l7MCT-k503_android_large.jpg"); //获取屏幕的高度,取其三分之一 int height = getWindowManager().getDefaultDisplay().getHeight(); int viewPageHeight = height/3; ViewGroup.LayoutParams layoutParams = viewPager.getLayoutParams(); layoutParams.height=viewPageHeight; List<SimpleDraweeView> list = new ArrayList<>(); for(int i = 0; i< listUrl.size(); i++){ //创建SimpleDraweeView SimpleDraweeView simpleDraweeView = new SimpleDraweeView(this); simpleDraweeView.setImageURI(Uri.parse(listUrl.get(i))); simpleDraweeView.setAspectRatio(1.5f);//设置宽高比,记住了,这里是宽高比 simpleDraweeView.setMaxHeight(viewPageHeight); simpleDraweeView.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.FIT_CENTER);//设置图片居中显示。首先获取它的Hierarchy list.add(simpleDraweeView); } LunboPagerAdapter adapter= new LunboPagerAdapter(list,this); viewPager.setAdapter(adapter);---------------------------------------------------------------------------
public class LunboPagerAdapter extends PagerAdapter { private List<SimpleDraweeView> list; private Context context; public LunboPagerAdapter(List<SimpleDraweeView> list, Context context) { this.list = list; this.context = context; } @Override public int getCount() { return list.size(); } @Override public boolean isViewFromObject(View view, Object object) { // return false; return view==object; } @Override public Object instantiateItem(ViewGroup container, int position) { // return super.instantiateItem(container, position); // View inflate = LayoutInflater.from(context).inflate(R.layout.lunbo_item, container); container.addView(list.get(position)); Log.d("160125MY","instantiateItem: position="+position); return list.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { // super.destroyItem(container, position, object); container.removeView((View)object); Log.d("160125MY","destroyItem: position="+position+" object:"+object.toString()); } }
--------------------------------------------------------------------------
使用Fresco 还要创建BaseApplication,然后添加到清单文件中:
public class BaseApplication extends Application { @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); } }添加到清单文件中
<application android:name=".BaseApplication"
-------------------------------------------------------------------------
MainActivity:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.kodulf.lunbodemo.MainActivity" android:background="@android:color/holo_blue_dark"> <android.support.v4.view.ViewPager android:layout_alignParentTop="true" android:id="@+id/viewPager" android:background="@android:color/holo_purple" android:layout_width="wrap_content" android:layout_height="wrap_content"></android.support.v4.view.ViewPager> <RadioGroup android:layout_alignBottom="@id/viewPager" android:layout_alignRight="@id/viewPager" android:layout_marginBottom="20dp" android:layout_marginRight="50dp" android:id="@+id/radiogroup" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" > </RadioGroup> </RelativeLayout>
------------------------------------------------------------------------
1:ViewPager 和RadioGroup 的联动,前提是在创建radioButton的时候,添加一个id属性。
for (int i = 0; i < listUrl.size(); i++) { //第一种方式是在代码中创建 // RadioButton radioButton = new RadioButton(this); // radioButton.setId(i); // radioButton.setButtonDrawable(R.drawable.button_selector); //第二种方法是调用infalter来创建。其实是一个道理。 RadioButton radioButton = (RadioButton)LayoutInflater.from(this).inflate(R.layout.radio_item, null); radioButton.setId(i); radioGroup.addView(radioButton); } radioGroup.check(0); viewPager.addOnPageChangeListener(this); radioGroup.setOnCheckedChangeListener(this);
@Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { radioGroup.check(position); Log.d("160126MY","viewPager pageSelected position="+position); } @Override public void onPageScrollStateChanged(int state) { }
@Override public void onCheckedChanged(RadioGroup group, int checkedId) { viewPager.setCurrentItem(checkedId); Log.d("160126MY","readioGroupChecked checkedId="+checkedId); }
2:通过handler 实现轮播,就是开辟一个子线程,然后 子线程每sleep(1000)后就执行一次 handler.sentMessage 给主线程的 handler,
在主线程里面,radioGroup.check(i) 就行了
handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); int what = msg.what; switch (what){ case REFRESH: int i = msg.arg1; radioGroup.check(i); Log.d("160126MY","handler arg1 "+i); break; } } };
//开辟子线程来实现轮播 thread = new Thread(this); thread.start();
@Override public void run() { int i =1; while(true){ Message message = new Message(); message.what=REFRESH; message.arg1=i; handler.sendMessage(message); Log.d("160126MY","RUN i="+i); if(i<listUrl.size()) { i++; } else{ i=0; } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }
3:RadioButton 的动态生成,
首先需要创建drawable 的button_selector.xml文件
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_checked="true" android:drawable="@drawable/shape_bg"/><item android:drawable="@drawable/shape_bg_notclick" /></selector>