滑动时获取列表视图的行位置

发布于 2024-12-19 16:09:19 字数 717 浏览 3 评论 0原文

我知道在此处之前提出过此类问题。 我已经尝试过两种手势检测器:

 wholelistview.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        });

仅检测滑动(此处无法获取行位置)。

在 getview 中类似:

public View getView(int position, View convertView, ViewGroup parent) {
//other stuff

convertview.setOnTouchListener(same as above);

} 

无法检测滑动。

对此有任何解决方案吗??? 我只想要进行滑动的行位置。

I know this type of question is asked before here.
I have tried both gesture detector :

 wholelistview.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        });

which detects only swipe(cannot get row position here).

similarly in getview:

public View getView(int position, View convertView, ViewGroup parent) {
//other stuff

convertview.setOnTouchListener(same as above);

} 

but cannot detect swipe.

Any solution for this ???
I want only the row position on which swipe is made.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

征﹌骨岁月お 2024-12-26 16:09:19

hanry 你会遇到什么样的错误?我已经实现了它并且运行良好。但我不是存储元素的位置,而是存储视图持有者,其中其他元素也具有模型属性。请记住,您必须检查 ConvertView 是否不为空。看一下代码:

public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;

    Model m = filter.subItems.get(position);
    if(m != null)
    {
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.rowlayout, null);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.position = position; - here you can put your position.
            view.setOnTouchListener(this.listener);
            //assign whatever you like to the viewHolder - in most cases the model and inlated controls and then assign 
        } else {
            view = convertView;
        }
        view.setTag(viewHolder);
    }

然后

public boolean onTouch(View v, MotionEvent event) {
    ViewHolder viewHolder = ((ViewHolder) v.getTag());
}

希望有所帮助。
ps:如果您对视图持有者和标签一无所知,我建议您查看那里。弗兰肯斯坦给出的链接中描述了我的方法。

hanry what kind of errors do you get? I've implemented it and it works perectly. But instead of storing the position of the element I'm storing the view holder that among the others also have the model property. Remember that you have to check if the convertview isn't null. Take a look at the code:

public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;

    Model m = filter.subItems.get(position);
    if(m != null)
    {
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.rowlayout, null);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.position = position; - here you can put your position.
            view.setOnTouchListener(this.listener);
            //assign whatever you like to the viewHolder - in most cases the model and inlated controls and then assign 
        } else {
            view = convertView;
        }
        view.setTag(viewHolder);
    }

and then

public boolean onTouch(View v, MotionEvent event) {
    ViewHolder viewHolder = ((ViewHolder) v.getTag());
}

hop that helps.
ps: If you don't know anything about view holders and tags I suugest to see there. And my approach has been described in the link that Frankenstein gave.

记忆で 2024-12-26 16:09:19

要获取视图的行位置,请通过此函数获取列表视图的 onTouch 传递的视图位置

wholelistview.setOnTouchListener(new View.OnTouchListener() {         
 public boolean onTouch(View v, MotionEvent event) {                 
 if (gestureDetector.onTouchEvent(event)) {
    int position =  indexOfChild(View v);

     return true;                 
     }                 
     return false;             
                  }         }); 

。它返回子级的索引。
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.5_r1/android/view/ViewGroup.java#ViewGroup.indexOfChild% 28android.view.View%29

To get row position for your view, get position of view passed by onTouch for your listview by this function

wholelistview.setOnTouchListener(new View.OnTouchListener() {         
 public boolean onTouch(View v, MotionEvent event) {                 
 if (gestureDetector.onTouchEvent(event)) {
    int position =  indexOfChild(View v);

     return true;                 
     }                 
     return false;             
                  }         }); 

It returns index of child.
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.5_r1/android/view/ViewGroup.java#ViewGroup.indexOfChild%28android.view.View%29

物价感观 2024-12-26 16:09:19

您可以执行 getTag 和 setTag 来了解视图的标签,并使用它来确定已刷过哪一行

public View getView(int position, View convertView, ViewGroup parent) { 
//other stuff 
     convertview.setTag(position);
}  

,以及 从链接中滑动

 public boolean onTouch(View v, MotionEvent event) {
     int pos = (Integer)v.getTag();
     Log.i(TAG,pos+" is swiped");
 }

我没有实现这个..所以你必须测试错误..但根据我的说法,这可能是实现这一目标的好方法你想要什么 做。

you can do getTag and setTag to know the tag of the view and using that you can decide which row has been swiped

public View getView(int position, View convertView, ViewGroup parent) { 
//other stuff 
     convertview.setTag(position);
}  

and for swipe from the link

 public boolean onTouch(View v, MotionEvent event) {
     int pos = (Integer)v.getTag();
     Log.i(TAG,pos+" is swiped");
 }

I didn't Implemented this..so you will have to test for error..but According to me this can be a good way to achieve what you want to do.

只涨不跌 2024-12-26 16:09:19
private int position = -1;
@Override
protected void onListItemClick(ListView l, View v, int index, long id) {
position = index;
}

像这样?

private int position = -1;
@Override
protected void onListItemClick(ListView l, View v, int index, long id) {
position = index;
}

like this?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文