如何在列表视图中获得一行的高度

时间:2019-02-22 14:17:16

标签: android-listview ontouchlistener

当用户触摸列表视图时,我试图在列表视图中获取行的高度和scrollY,以便我可以计算出在OnTouchListener()中单击的项目。我的列表视图中的每一行都有一个imageview和两个textview

这是我在onTouchListener中的代码

    battleshipList.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            ListView bs = (ListView) v;
            TextView txt = findViewById(R.id.txt_gridInfo);
            int action = event.getAction();
            if(action == MotionEvent.ACTION_DOWN){
                int firstVisibleRow = bs.getFirstVisiblePosition();
                int lastVisibleRow = bs.getLastVisiblePosition();
                View view2 = (View) bs.getItemAtPosition(firstVisibleRow);
                float view2Y = view2.getY();
                int view2ScrollY = view2.getScrollY();
                int view2Height = view2.getHeight();
                View view3 = (View) bs.getItemAtPosition(lastVisibleRow);
                float view3Y = view3.getY();
                int view3LastY = view3.getScrollY();
                int view3Height = view3.getHeight();
                txt.setText(String.valueOf(event.getY()) + " "+ String.valueOf(firstVisibleRow) + " " + String.valueOf(lastVisibleRow)+ " FY:" + String.valueOf(view2Y)
                        + " FSY:" + String.valueOf(view2ScrollY) + " FH:" + String.valueOf(view2Height)+ " LY:" + String.valueOf(view3Y)
                        + " LSY:" + String.valueOf(view3LastY) + " LH:" + String.valueOf(view3Height));
            }
            return false;
        }
    });

问题是,当我尝试获取被单击的视图(或行)时,出现以下错误:

E / MessageQueue-JNI:java.lang.ClassCastException:java.util.HashMap无法转换为android.view.View

感谢您的帮助。

0 个答案:

没有答案