ViewPager(含代替gallery)

ViewPager代替Gallery效果,gallery弊端在于滑动的时候

专注于为中小企业提供成都网站建设、网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业芦淞免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了近1000家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

ViewPager item需要一个布局Layout;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class NearbyShopPageLinearLayout extends LinearLayout {
    public NearbyShopPageLinearLayout(final Context context,final AroundShop shop) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View inflate = inflater.inflate(R.layout.shop_around_item, this, true);
        if (BaseApp.getInstance().canLoadPic()) {
            String trimImageUrl = BitmapUtils.trimImageUrl(shop.getImg_url(),
                    200);
            Constans.p_w_picpathLoader.displayImage(trimImageUrl,
                    (ImageView) inflate.findViewById(R.id.iv_aroud_shop),
                    Constans.options);
        } else {
            ((ImageView) inflate.findViewById(R.id.iv_aroud_shop))
                    .setImageResource(R.drawable.ic_stub);
        }
        ((TextView) inflate.findViewById(R.id.tv_shop_name)).setText(shop
                .getShop_name());
        this.setOnClickListener(new OnClickListener() {
                                                                                                                                                      
            @Override
            public void onClick(View v) {
                int shopId = shop.getShop_id();
                Intent it = new Intent();
                it.putExtra(IntentKeys.SHOP_ID, shopId);
                it.setClass(context, ShopDetailActivity.class);
                context.startActivity(it);
            }
        });
    }
}

需要再设置一个PagerAdapter

class ImageNearbyAdapter extends PagerAdapter {
                                                     
        @Override
        public float getPageWidth(int position) {
            // TODO Auto-generated method stub
            return (float) 0.3; //这个属性用来控制一面显示多少个item,默认是1,这个属性和weight差不多每个item占的比例
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // TODO Auto-generated method stub
            ((ViewPager) container).removeView(mNearbyPageArr.get(position));//滑动的时候释放layout
        }
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // TODO Auto-generated method stub
            ((ViewPager) container).addView(mNearbyPageArr.get(position), 0);
            return mNearbyPageArr.get(position);//viewpager添加item
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mNearbyPageArr.size();//item个数
        }
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == arg1; //对象是否一样
        }
    }

//其中 初始化 设置viewpager 和adapter

ViewPager pager = (ViewPager) findViewById(R.id.viewpager11);
        mNearbyPageArr = new ArrayList();
        for (int i = 0; i < around.size(); i++) {
            NearbyShopPageLinearLayout nspLL = new NearbyShopPageLinearLayout(
                    ShopDetailActivity.this, around.get(i));
            mNearbyPageArr.add(nspLL);
            // ImageView p_w_picpathView = new ImageView(ShopDetailActivity.this);
            // p_w_picpathView.setBackgroundResource(R.drawable.ic_error);
            // mNearbyPageArr2.add(p_w_picpathView);
        }
        ImageNearbyAdapter inAdapter = new ImageNearbyAdapter();
        pager.setAdapter(inAdapter);
        pager.setOffscreenPageLimit(5);
        pager.setPageMargin(10);

xml中的配置如下:



分享标题:ViewPager(含代替gallery)
分享链接:http://hbruida.cn/article/jeooeg.html