请帮我解决问题:
我的元素上有一个gridview和一个touchListener。问题是,当我触摸一个元素时,我想改变它的背景,当触摸完成时(动作向上或ouside)显示初始背景。问题是当我点击一个元素然后将我的手指拖到另一个元素上时,初始元素将其更改为背景,但是在我将手指拖过另一个元素并完成触摸后,触摸的第一个元素的背景不会更改为初步背景。我的想法是,当我从网格视图中触及一个元素以获得锁定时。这可能是代码:
package com.mines.gui.components;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import com.mines.globals.GlobalVariables;
public class MinesView extends GridView {
private int[] length = new int[81];
private final Bitmap grid0 = Bitmap.createBitmap(GlobalVariables.icons, 0, 88, 50, 50);
private final Bitmap grid1 = Bitmap.createBitmap(GlobalVariables.icons, 50, 88, 50, 50);
private int id;
public MinesView(Context context) {
super(context);
setAdapter(new MineAdapter(context));
setStretchMode(GridView.NO_STRETCH);
setNumColumns(9);
setHorizontalSpacing(40);
}
class MineAdapter extends BaseAdapter {
private Context context;
public MineAdapter(Context context) {
this.context = context;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public int getCount() {
return length.length;
}
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView imageView;
if(convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(40, 40));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
imageView.setBackgroundDrawable(new BitmapDrawable(grid0));
imageView.setId(position);
imageView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View arg0, MotionEvent arg1) {
switch (arg1.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
imageView.setBackgroundDrawable(new BitmapDrawable(grid1));
break;
case MotionEvent.ACTION_OUTSIDE:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
imageView.setBackgroundDrawable(new BitmapDrawable(grid0));
break;
}
return false;
}
});
imageView.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
return imageView;
}
}
}
答案 0 :(得分:0)
试用此代码...... 为gridview创建ontouchlistener。
public class HelloGridViewActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
imageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
v.setBackgroundResource(R.drawable.sample_0);
Log.v("log_tag", "MotionEvent.ACTION_DOWN");
return true;
}
else if(event.getAction()==MotionEvent.ACTION_UP)
{
v.setBackgroundDrawable(null);
Log.v("log_tag", "MotionEvent.ACTION_UP");
return true;
}
return false;
}
});
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
}