GridView控件,获取项目的父视图 [英] GridView, Get Item's parent View
问题描述
我试图发送点击我的孩子元素 GridView控件
用一个触摸事件。到目前为止,我可以检索子元素的数据,但不能在其上执行的点击。
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控件
- 触摸事件处理程序在这里...-
的LinearLayout
- 听众的onClick这里...-
的TextView
- 只是一些文字...
GridView
- Touch Event Handler here...LinearLayout
- onClick listeners here...TextView
- Just some text...
这是适配器填充
GridView控件
与的LinearLayout
的谁拥有定义的onclick事件。这些布局包括的TextView
s其中TextView.setText(从我的数据集的一些数据)
An adapter fills
GridView
withLinearLayout
's who have onClick events defined. These layouts containTextView
s whereTextView.setText("Some data from my dataset")
我这是目前听触及
GridView控件
事件,并成功找到的TextView $ C的数据的onTouch监听器$ C>根据手指的位置。
I have an onTouch listener which is currently listening to touch events on the
GridView
, and is successfully finding the data ofTextView
depending on the position of the finger.我如何获得
的TextView
的父视图,并在用户的手指位置调用performClick?How do I get the
TextView
's parent view, and call performClick at the user's finger position?这是当前点击处理程序,包含什么我这么远......
假设GridView控件
&安培;适配器
正确初始化...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; } } });
谢谢!
也想过提,这个
GridView控件
包含在一个片段中。我的疑问的它会改变世界,但可能会影响你们有。解决方案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.推荐答案
Aaahh ..东西,我们需要尝试,
Aaahh.. Something we need to try,
实现方法一样,
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); }
现在,调用此方法,
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; }
这篇关于GridView控件,获取项目的父视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-
-