Android的初学者:android的gridview的触摸事件 [英] Android beginner: Touch events in android gridview
问题描述
我使用下面的code做的GridView的事情(从的 http://developer.android.com/resources/tutorials/views/hello-gridview.html )。我想更换onClicklistener和的onClick()方法,用他们的亲密接触等同,即touchlistener和onTouch(),所以,当我抚摸的元素变化的元素在GridView的形象和在相同的元素双触摸需要它回到原单的状态。
我如何做到这一点?我不能让我的code做到这一点。所述clicklistener工作在一定程度上,但在触摸不是。请大家帮帮忙。
公共类ImageAdapter扩展了BaseAdapter {
私人语境mContext;
公共ImageAdapter(上下文C){
mContext = C;
}
公众诠释getCount将(){
返回mThumbIds.length;
}
公共对象的getItem(INT位置){
返回null;
}
众长getItemId(INT位置){
返回0;
}
//创建由适配器引用的每个项目的新的ImageView
公共查看getView(INT位置,查看convertView,ViewGroup中父){
ImageView的ImageView的;
如果(convertView == NULL){//如果它不回收,初始化一些属性
ImageView的=新ImageView的(mContext);
imageView.setLayoutParams(新GridView.LayoutParams(85,85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8,8,8,8);
imageView.setOnClickListener(新View.OnClickListener()
{
@覆盖
公共无效的onClick(视图查看)
{
如果(位置== 0)
{
//做这个
}
其他
{
//做这个
}
}
});
} 其他 {
ImageView的=(ImageView的)convertView;
}
imageView.setImageResource(mThumbIds [位置]);
返回ImageView的;
}
//引用到我们的形象
私人整数[] 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
};
}
使用了 OnTouchListener
以这种方式。阅读上MotionEvent类型,如 ACTION_UP
, ACTION_MOVE
和 ACTION_DOWN
这意味着,关键是pressed这里,老鼠很感动,或重点在这里pssed未$ P $ ...
公共无效addListenerToGrid(){
GridView控件=(GridView控件)findViewById(R.id.gridView1);
gridView.setOnTouchListener(新OnTouchListener(){
公共布尔onTouch(视图V,MotionEvent我){
INT行动= me.getActionMasked();
浮currentXPosition = me.getX();
浮currentYPosition = me.getY();
INT位置= gridView.pointToPosition((INT)currentXPosition,(INT)currentYPosition);
如果(动作== MotionEvent.ACTION_UP){
//重点是$ P $这里pssed
}
返回true;
}
I am using the following code to do things with gridview(slightly modified from http://developer.android.com/resources/tutorials/views/hello-gridview.html). I want to replace the onClicklistener and the onClick() method with their "touch" equivalents i.e. touchlistener and onTouch() so that when i touch an element in the gridview the image of the element changes and a double touch on the same element takes it back to the orginal state.
How do I do this? I can't get my code to do this. The clicklistener works to some extent but the touch isn't. Please help.
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);
imageView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
if(position==0)
{
//do this
}
else
{
//do this
}
}
});
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
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
};
}
Use the OnTouchListener
in this way. Read up on MotionEvent types like ACTION_UP
, ACTION_MOVE
, and ACTION_DOWN
which mean that the key was pressed here, mouse was moved, or key was unpressed here...
public void addListenerToGrid() {
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent me) {
int action = me.getActionMasked();
float currentXPosition = me.getX();
float currentYPosition = me.getY();
int position = gridView.pointToPosition((int) currentXPosition, (int) currentYPosition);
if (action == MotionEvent.ACTION_UP) {
// Key was pressed here
}
return true;
}
这篇关于Android的初学者:android的gridview的触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!