问题
I'm trying to send clicks to my child elements in GridView with a single touch event. So far, I can retrieve the data of the child element, but cannot perform a click on it.
Let me explain a bit more on the layout of things...
GridView- Touch Event Handler here...LinearLayout- onClick listeners here...TextView- Just some text...
An adapter fills GridView with LinearLayout's who have onClick events defined. These layouts contain TextViews where TextView.setText("Some data from my dataset")
I have an onTouch listener which is currently listening to touch events on the GridView, and is successfully finding the data of TextView depending on the position of the finger.
How do I get the TextView's parent view, and call performClick at the user's finger position?
This is the current click handler, containing what I've got so far...
Assume GridView & Adapter are initialised properly...
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = MotionEventCompat.getActionMasked(event);
switch(action)
{
case (MotionEvent.ACTION_DOWN) :
case (MotionEvent.ACTION_MOVE) :
int position = GridView.pointToPosition((int)event.getX(),(int)event.getY());
if (position != -1)
{
// I have looked at the other .get methods, and doing GridView.getXYZ (Where XYZ could be any method) and couldn't get anywhere...
String item = Adapter.getItem(position);
Log.d("TouchEvent","Action was DOWN/MOVE on " + item);
return true;
}
case (MotionEvent.ACTION_UP) :
Log.d("TouchEvent","Action was UP");
return true;
case (MotionEvent.ACTION_CANCEL) :
Log.d("TouchEvent","Action was CANCEL");
return true;
case (MotionEvent.ACTION_OUTSIDE) :
Log.d("TouchEvent","Movement occurred outside bounds " +
"of current screen element");
return true;
default :
return true;
}
}
});
Thanks!
Also thought to mention, this GridView is contained within a fragment. I doubt it would change the world, but might affect the solutions you guys have.
回答1:
Aaahh.. Something we need to try,
Implement method like,
private View getViewByPosition(GridView gridView, int position) {
int firstPosition = gridView.getFirstVisiblePosition();
int lastPosition = gridView.getLastVisiblePosition();
if ((position < firstPosition) || (position > lastPosition))
return null;
return gridView.getChildAt(position - firstPosition);
}
Now, call this method,
if (position != -1)
{
// I have looked at the other .get methods, and doing GridView.getXYZ (Where XYZ could be any method) and couldn't get anywhere...
GridView gridView = (GridView) v;
View touchedView = getViewByPosition(gridView, position);
String item = Adapter.getItem(position);
Log.d("TouchEvent","Action was DOWN/MOVE on " + item);
return true;
}
回答2:
Try this
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v,
int position, long id)
{
// Send intent to SingleViewActivity
Intent i = new Intent(getApplicationContext(), SingleViewActivity.class);
// Pass image index
i.putExtra("id", position);
startActivity(i);
}
});
After this create SingleViewActivity.java class
public class SingleViewActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.single_view);
// Get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.SingleView); imageView.setImageResource(imageAdapter.mThumbIds[position]);
}
}
来源:https://stackoverflow.com/questions/29899586/gridview-get-items-parent-view