十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
ViewPager代替Gallery效果,gallery弊端在于滑动的时候
成都创新互联是一家专业提供玉树企业网站建设,专注与网站建设、网站制作、H5建站、小程序制作等业务。10年已为玉树众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。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中的配置如下:
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。