【问题标题】:How to know the y position of the Clicked Itemin a listview with regard to the screen in android如何知道单击项目在列表视图中相对于android中的屏幕的y位置
【发布时间】:2012-03-20 15:09:47
【问题描述】:

在我的应用程序中,我将 Web 视图膨胀为列表视图,并将长按单击事件设置为 Web 视图。长按我正在复制一个项目以显示在列表视图中。如图所示。

我想知道单击项目的位置,以便我可以使重复视图恰好显示在单击项目的上方,但现在它出现在列表中的第一个项目上。

灰色项目是重复项目,我点击了第 10 个项目,它出现在第 1 个项目上。我如何让它出现在第 10 个项目上 ![在此处输入图片说明][1]

wv.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                duplicateView.setVisibility(View.VISIBLE);
                Log.d("lvsectionTwo "+lvsectionTwo.getScrollY(),"log");
                WebView wv = (WebView) duplicateView.findViewById(R.id.wv1);
                wv.loadData("item "+position, "text/html", null);
                wv.setBackgroundColor(Color.DKGRAY);

                ![enter image description here][2]



                return true;
            }
        });
        return view;
    }

【问题讨论】:

  • 您的列表使用哪个适配器?

标签: android listview webview long-press


【解决方案1】:

通过以下方式获取视图的 y 位置: http://developer.android.com/reference/android/view/View.html#getLocationInWindow%28int[]%29

它需要一个长度为 2 的整数数组 和 x 和 y 坐标将存储在该数组对象中,现在使用该对象获取视图位置

私有 OnItemLongClickListener onlongpressed = new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View view,
                            int position, long id) {
                    duplicateView = ((LayoutInflater) getBaseContext().getSystemService(
                                    LAYOUT_INFLATER_SERVICE)).inflate(R.layout.dragview, null);
                    frlayout.addView(duplicateView);
                    int[] dimensions=new int[2];
                    getLocationInWindow (dimensions);
                    FrameLayout.LayoutParams layoutParams = frlayout.getLayoutParams();
                    layoutParams.setMargins(0, dimensions[1]-20, 0, 0);
                    duplicateView.setLayoutParams(layoutParams);
                    duplicateView.setVisibility(View.VISIBLE);
                    WebView wv = (WebView) duplicateView.findViewById(R.id.wv1);
                    wv.setBackgroundColor(Color.DKGRAY);
                    wv.loadData("Item "+position, "text/html", null);
                    return true;
            }
    };

【讨论】:

    【解决方案2】:

    我认为您要做的就是将元素插入到您的适配器在适当位置操作的底层数据结构中。不过,我会使用 OnItemLongClickListener 而不是 OnLongClickListener,因为它为您提供了位置:

    getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long id) {
            // add something to your structure at (position - 1) and refresh your ListView.
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-06
      • 2019-05-21
      相关资源
      最近更新 更多