我看网上有很多关于此问题的解决办法,我看都是写了一半,不够具体,今天我就给童鞋们,详细的说一下步骤。
首先重写GridView的 onMeasure()方法,首先提醒一句构造方法一定要用有两个参数的那个
<span style="font-family:Courier New;color:#666666;"><strong>package com.example.photomange;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewParent;
import android.widget.GridView;
public class MyGridView extends GridView {
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
Log.i("app", "调用了MyGridView(Context context, AttributeSet attrs)");
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
</strong></span>
改xml 文件 把原来的GridView 改成你重写的MyGridView
<span style="font-family:Courier New;color:#666666;"><strong><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="500dp" >
<TextView
android:id="@+id/title_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="2016-11-03" />
<TextView
android:id="@+id/line"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/title_date"
android:text=" | " />
<TextView
android:id="@+id/title_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/line"
android:text="北京" />
<com.example.photomange.MyGridView
android:id="@+id/gridId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title_date"
android:gravity="center"
android:horizontalSpacing="1dp"
android:numColumns="4"
android:scrollbars="none"
android:verticalSpacing="1dp" />
</RelativeLayout></strong></span>
然后在ListView自定义的适配器里调用就行了
<span style="font-family:Courier New;color:#666666;"><strong> @Override
public View getView(int position, View convertView, ViewGroup parent) {
final int po = position;
View v = View.inflate(context, resouse, null);
TextView tv = (TextView) v.findViewById(R.id.title_date);
TextView city = (TextView) v.findViewById(R.id.title_location);
MyGridView mv = (MyGridView) v.findViewById(R.id.gridId);
String tite = title[position];
//当前时间 - position 即为前一天(或者大前天)
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance(); // 当时的日期和时间
int day = c.get(Calendar.DAY_OF_MONTH) - position;
c.set(Calendar.DAY_OF_MONTH, day);
String date = df.format(c.getTime());
tv.setText(date);
//判断城市
List<Map<String, Object>> PhotoWhere = chooseCity(position, city);
//为gridview填充数据
SimpleAdapter gridAdapter = new SimpleAdapter
(context, PhotoWhere, R.layout.gridphoto_items,
new String []{"image"}, new int []{R.id.imageid});
mv.setAdapter(gridAdapter);
mv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (po) {
case 0:
AlertDialog.Builder builder = new Builder(context)
.setTitle("北京")
.setIcon(R.drawable.b1);
builder.create();
builder.show();
if(position==0){
position=1;
}
break;
}
//Toast.makeText(context, "点击的坐标"+po+","+position, 0).show();
}
});
return v;
}</strong></span>
当然我的自定义适配器里还有其他代码,我就不全展示了,相信到这里你就肯定能成功了
以下是效果截图