我知道here之前会问这类问题。 我试过了两个手势探测器:
wholelistview.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
});
仅检测滑动(此处无法获取行位置)。
public View getView(int position, View convertView, ViewGroup parent) {
//other stuff
convertview.setOnTouchListener(same as above);
}
但无法检测到滑动。
任何解决方案??? 我只想要滑动的行位置。
答案 0 :(得分:3)
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:如果您对视图持有者和标签一无所知,我会看到there。我的方法已在弗兰肯斯坦给出的链接中描述。
答案 1 :(得分:1)
要获取视图的行位置,请通过此函数获取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;
} });
答案 2 :(得分:1)
你可以使用getTag和setTag来了解视图的标签,并使用它来判断哪一行被刷过
public View getView(int position, View convertView, ViewGroup parent) {
//other stuff
convertview.setTag(position);
}
以及 swipe 来自链接
public boolean onTouch(View v, MotionEvent event) {
int pos = (Integer)v.getTag();
Log.i(TAG,pos+" is swiped");
}
我没有实现这个......所以你必须测试错误..但根据我的说法,这可能是实现你想要做的事情的好方法。
答案 3 :(得分:0)
private int position = -1;
@Override
protected void onListItemClick(ListView l, View v, int index, long id) {
position = index;
}
像这样?