GridView,获取项目的父视图

时间:2015-04-27 15:15:34

标签: android android-layout android-view android-adapter android-gridview

我尝试使用单个触摸事件向GridView中的子元素发送点击。到目前为止,我可以检索子元素的数据,但不能对其执行单击。

让我再解释一下事物的布局......

  • GridView - 在此触摸事件处理程序...
    • LinearLayout - 这里的onClick听众......
      • TextView - 只是一些文字......

适配器将GridView填入已定义onClick事件的LinearLayout。这些布局包含TextView,其中TextView.setText("Some data from my dataset")

我有一个onTouch监听器,它正在监听GridView上的触摸事件,并且根据手指的位置成功找到TextView的数据。

如何获取TextView的父视图,并在用户的手指位置调用performClick?

这是当前的点击处理程序,包含我到目前为止所获得的内容... 假设GridView& Adapter已正确初始化...

@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包含在一个片段中。我怀疑它会改变世界,但可能会影响你们的解决方案。

2 个答案:

答案 0 :(得分:0)

Aaahh ..我们需要尝试的东西,

实施方法,

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;
}

答案 1 :(得分:0)

试试这个

gridview.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView parent,View v, int position,long id)
{
//将意图发送给SingleViewActivity
意图i = new Intent(getApplicationContext(),SingleViewActivity.class);
//传递图像索引
i.putExtra(&#34; id&#34 ;, position);
 startActivity(ⅰ);
}
});



在此之后创建SingleViewActivity.java类

公共类SingleViewActivity扩展活动
{
@Override
 public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.single_view);

//获取意图数据
意图i = getIntent();
//选择的图片ID
int position = i.getExtras()。getInt(&#34; id&#34;);
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView =(ImageView)findViewById(R.id.SingleView); imageView.setImageResource(imageAdapter.mThumbIds [position]);
}
}