实际工作中九宫格的一般使用

package com.momo2.momo2.adapter;

import java.util.List;

import com.momo2.momo2.MyApplication;
import com.momo2.momo2.R;
import com.momo2.momo2.utils.ScreenUtils;
import com.momo2.momo2.view.MyGridView;

import android.content.Context;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class BaskItemGridviewAdapter extends MyBaseAdapter<String> {

	public BaskItemGridviewAdapter(List<String> list, Context context) {
		super(list, context);
		// TODO Auto-generated constructor stub
	}

	@Override
	public View getView(int position, View converview, ViewGroup viewGroup) {

		ViewHolder holder;

		if (converview == null) {

			holder = new ViewHolder();

			converview = View.inflate(mContext, R.layout.item_bask_gridview,
					null);

			holder.imageView = (ImageView) converview
					.findViewById(R.id.item_bask_gridview_image);// 图片

			converview.setTag(holder);

		} else {

			holder = (ViewHolder) converview.getTag();

		}

		MyGridView myGridView = ((MyGridView) viewGroup);

		// 获取屏幕大小
		int screenWidth = ScreenUtils.getScreenWidth(mContext);

		// 设置九宫格里面图片的宽度
		int width = (screenWidth - myGridView.getPaddingLeft() - myGridView.getPaddingRight() - (myGridView
				.getNumColumns() - 1) * myGridView.getHorizontalSpacing())
				/ myGridView.getNumColumns();

		holder.imageView.setLayoutParams(new LinearLayout.LayoutParams(width,
				100));

		// String tuString=
		// Environment.getExternalStorageDirectory().toString()+"/automobile data recorder/momo20160226_2.jpg";
		// 填充图片
		/*
		 * MyApplication.getImageLoader().displayImage( tuString,
		 * holder.imageView, MyApplication.getFadeOptions(
		 * R.drawable.empty_photo, R.drawable.empty_photo,
		 * R.drawable.empty_photo));
		 */

		// 填充图片
		MyApplication.getImageLoader().displayImage(
				mList.get(position),
				holder.imageView,
				MyApplication.getFadeOptions(R.drawable.empty_photo,
						R.drawable.empty_photo, R.drawable.empty_photo));

		return converview;
	}

	class ViewHolder {
		ImageView imageView;
	}

}


使用九宫格的时候最好自定义九宫格:

package com.momo2.momo2.view;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;
/**
 * 防止显示不全*/
public class MyGridView extends GridView {     
    public MyGridView(Context context, AttributeSet attrs) {     
        super(context, attrs);     
    }     
    
    public MyGridView(Context context) {     
        super(context);     
    }     
    
    public MyGridView(Context context, AttributeSet attrs, int defStyle) {     
        super(context, attrs, defStyle);     
    }     
    
    @Override     
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {     
    
        int expandSpec = MeasureSpec.makeMeasureSpec(     
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);     
        super.onMeasure(widthMeasureSpec, expandSpec);     
    }     
}    





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值