在项目中使用listview+gridview中遇到以下几个问题:
1.gridview数据显示不全,图片不能显示正常
在网上看到的解决办法都是自定义gridview
import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;
/**
* Created by Administrator on 2017/4/13.
*/
public class MyGridView extends GridView {
public MyGridView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public MyGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
但是并没有解决问题,因为我嵌套在listview使用,直接在listview的适配器中嵌套gridview适配器,图片不能正常显示,解决办法是要在gridview的适配器getview中添加下面代码
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.medicine_gridview_item, parent, false);
2.图片只显示一部分,要在gridview的适配器getview中添加以下代码
convertView.setLayoutParams(new GridView.LayoutParams("宽度", "高度"));
实现效果