GridView控件,获取项目的父视图 [英] GridView, Get Item's parent View

查看:221
本文介绍了GridView控件,获取项目的父视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发送点击我的孩子元素 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 with LinearLayout's who have onClick events defined. These layouts contain TextViews where TextView.setText("Some data from my dataset")

          我这是目前听触及 GridView控件事件,并成功找到的TextView 根据手指的位置。

          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.

          我如何获得的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆