当前位置: 代码迷 >> Android >> android 相仿网易首页的图片切换(viewPager)
  详细解决方案

android 相仿网易首页的图片切换(viewPager)

热度:60   发布时间:2016-05-01 12:02:32.0
android 类似网易首页的图片切换(viewPager)
1.activity调用
private View viewImage() {
LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
viewPager = null;
View view = getLayoutInflater().inflate(R.layout.news_lstview_header,
null);
viewPager = (ViewPager) view.findViewById(R.id.news_layout_viewPager);
viewPager.setLayoutParams(new ListView.LayoutParams(
ListView.LayoutParams.FILL_PARENT, 300));
List<View> list = new ArrayList<View>();
for (int i = 0; i < 3; i++) {
ImageView imgView = new ImageView(this);
imgView.setLayoutParams(mParams);
imgView.setImageResource(pics[i]);
list.add(imgView);
}
viewPager.setAdapter(new MyPagerAdapter(list));
viewPager.setCurrentItem(0);

return viewPager;
}
2.适配器
package com.and.netease.utils;

import java.util.List;

import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

public class MyPagerAdapter extends PagerAdapter {

List<View> views;
public MyPagerAdapter(List<View> v) {
super();
    views = v;
}
@Override
public void destroyItem(View v, int pos, Object arg2) {
((ViewPager) v).removeView(views.get(pos));
}

@Override
public void finishUpdate(View arg0) {

}

@Override
public int getCount() {
if (views != null)
        {
            return views.size();
        }
       
        return 0;
}

@Override
public Object instantiateItem(View v, int pos) {
((ViewPager) v).addView(views.get(pos));
return views.get(pos);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0==arg1;
}

@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {

}

@Override
public Parcelable saveState() {
return null;
}

@Override
public void startUpdate(View arg0) {

}
}
  相关解决方案