您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
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.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<NearbyShopPageLinearLayout>(); 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中的配置如下:
<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" />
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。