// 此方法直接照搬自网络上的一个下拉刷新的demo,此处是“估计”headView的width以及height
private void measureView(View child) {
ViewGroup.LayoutParams p = child.getLayoutParams();
if (p == null) {
p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
int lpHeight = p.height;
int childHeightSpec;
if (lpHeight > 0) {
childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight,
MeasureSpec.EXACTLY);
} else {
childHeightSpec = MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED);
}
child.measure(childWidthSpec, childHeightSpec);
}
public void setAdapter(BaseAdapter adapter) {
// {@ TChip ZJ:设置Header中最近更新时间 @}
lastUpdatedTextView.setText(“最近更新:” + new Date().toLocaleString());
super.setAdapter(adapter);
}
}
2.自定义可拖动的GridView
效果预览:
代码
MainActivity:
package com.example.draggridview;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import com.example.framework.DragGridBaseAdapter;
import com.example.framework.DragGridView;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private List<HashMap<String, Object>> dataSourceList = new ArrayList<HashMap<String, Object>>();
private DragGridView mDragGridView = null;
private Button btnRefresh;
private Button btnFull;
private Button btnFullCancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tvNum = (TextView) findViewById(R.id.tvNum);
btnRefresh = (Button) findViewById(R.id.btnRefresh);
btnFull = (Button) findViewById(R.id.btnFull);
btnFullCancel = (Button) findViewById(R.id.btnFullCancel);
mDragGridView = (DragGridView) findViewById(R.id.dragGridView);
int red = 0;
int blue = 0;
dataSourceList.clear();
for (int i = 1; i < 97; i++) {
HashMap<String, Object> itemHashMap = new HashMap<String, Object>();
itemHashMap.put(“item_text”, “Icon” + Integer.toString(i));
int randomNUm = new Random().nextInt(2);
if (randomNUm % 2 == 0) {
itemHashMap.put(“item_icon”, R.drawable.check);
blue++;
} else {
itemHashMap.put(“item_icon”, R.drawable.star);
red++;
}
dataSourceList.add(itemHashMap);
}
mDragGridView.setAdapter(new DragAdapter(this, dataSourceList));
tvNum.setText(“Red:” + red + " Blue" + blue);
btnRefresh.setOnClickListener(new onClickListenerImp());
btnFull.setOnClickListener(new onClickListenerImp());
btnFullCancel.setOnClickListener(new onClickListenerImp());
}
// OnClickListener
class onClickListenerImp implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == btnRefresh) {
mDragGridView = (DragGridView) findViewById(R.id.dragGridView);
onCreate(null);
} else if (v == btnFull) {
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
btnFullCancel.setVisibility(View.VISIBLE);
btnFull.setVisibility(View.GONE);
} else if (v == btnFullCancel) {
//isFull = getWindow().getAttributes().flags;
getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN);
btnFullCancel.setVisibility(View.GONE);
btnFull.setVisibility(View.VISIBLE);
}
}
}
// Adapter
class DragAdapter extends BaseAdapter implements DragGridBaseAdapter {
private List<HashMap<String, Object>> list;
private LayoutInflater mInflater;
private int mHidePosition = -1;
public DragAdapter(Context context, List<HashMap<String, Object>> list) {
this.list = list;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(R.layout.grid_item, null);
ImageView mImageView = (ImageView) convertView
.findViewById(R.id.item_icon);
TextView mTextView = (TextView) convertView