2011-11-04 65 views
1

在Stackview,似乎OnItemSelectedListener(从超 “适配器视图”)永远不会被调用...... 我怎样才能触发某些事件时,堆栈顶部的视图 改变由用户?StackView和OnItemSelectedListener(Android 3.0以上版本)

我想显示一些文本来显示堆栈中当前项目 的位置,所以我需要找到一种方式来在用户浏览堆栈时更新textview。

感谢,

回答

0

我所做的是写扩展StackView并编写一些代码来获取OnItemSelected逻辑工作的新类。当onTouchEvent给我一个MotionEvent.getAction()== ACTION_UP时,我开始一个自称为'直到StackView.getDisplayedChild()改变的线程“。当它改变时,我开始OnItemSelected逻辑,所以我总是可以得到第一个显示的孩子。

public boolean onTouchEvent(MotionEvent motionEvent) { 
    if (motionEvent.getAction() == MotionEvent.ACTION_UP && this.getAdapter() != null) { 
     mPreviousSelection = this.getDisplayedChild(); 
     post(mSelectingThread); 
    } 
    return super.onTouchEvent(motionEvent); 
} 

这个线程循环,直到他自己,他将获得新displayedChild:

private class SelectingThread implements Runnable { 
    CustomStackView mStackView; 

    public SelectingThread(CustomStackView stackView) { 
     this.mStackView = stackView; 
    } 

    @Override 
    public void run() { 
     if(mStackView.getAdapter() != null) { 
      if (mPreviousSelection == CustomStackView.this.getDisplayedChild()) { 
       mThisOnItemSelectedListener.onItemSelected(mStackView, mStackView.getAdapter().getView(mPreviousSelection, null, mStackView), 
        mStackView.mPreviousSelection, mStackView.getAdapter().getItemId(mPreviousSelection)); 
       return; 
      } else { 
       mPreviousSelection = mStackView.getDisplayedChild(); 
       mStackView.post(this); 
      } 
     } 
    } 
} 

这个监听器,而不是设置选定标志设置为true取消选择所有这些之后。

​​

我测试过这一点,它的工作原理..

1

一个党,但为乡亲来到这里,从谷歌有点晚了。幸运的是我找到了一个更简单的解它仍然涉及到扩展StackView。

import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.StackView; 

public class StackViewAdv extends StackView 
{ 
    public StackViewAdv(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
    } 

    public StackViewAdv(Context context, AttributeSet attrs, int defStyleAttr) 
    { 
     super(context, attrs, defStyleAttr); 
    } 

    @Override 
    public void setDisplayedChild(int whichChild) 
    { 
     this.getOnItemSelectedListener().onItemSelected(this, null, whichChild, -1); 

     super.setDisplayedChild(whichChild); 
    } 
} 

请注意,此解决方案仅给予听者和视图(上onItemSelected第二个参数)选定的视图的索引是

使用this.getCurrentView()而不是null不幸的是不工作,因为它返回一个StackView的子类。也许有人找到解决方案。

相关问题