ViewPager代替Gallery效果,gallery弊端在于滑动的时候
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.imageLoader.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<NearbyShopPageLinearLayout>(); for (int i = 0; i < around.size(); i++) { NearbyShopPageLinearLayout nspLL = new NearbyShopPageLinearLayout( ShopDetailActivity.this, around.get(i)); mNearbyPageArr.add(nspLL); // ImageView imageView = new ImageView(ShopDetailActivity.this); // imageView.setBackgroundResource(R.drawable.ic_error); // mNearbyPageArr2.add(imageView); } ImageNearbyAdapter inAdapter = new ImageNearbyAdapter(); pager.setAdapter(inAdapter); pager.setOffscreenPageLimit(5); pager.setPageMargin(10);
xml中的配置如下:
<android.support.v4.view.ViewPager android:id="@+id/viewpager11" android:background="#FFFFFF" android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="120dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="12dp" android:fadingEdge="none" android:clipChildren="false" />
本文出自 “绝对领域” 博客,请务必保留此出处http://lyp634959848.blog.51cto.com/7750498/1408932
ViewPager(含代替gallery),布布扣,bubuko.com
原文:http://lyp634959848.blog.51cto.com/7750498/1408932