您好,登錄后才能下訂單哦!
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個數(shù) } @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" />
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。