繁体   English   中英

在滑动时获取Listview的子行的问题

[英]Issue In Getting Child Row of the Listview On Swipe

我试图在事件滑动发生时检索listview的子行。我创建了一个BaseAdapter,它设置了一个customView,下面是BaseAdapter的GetView方法:

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

    ModelClass model=modelList.get(position);
    if(convertView==null)
    {

        modelView=new ModelView(context,model);

    }
    else{
        Log.d("convertView","NotNUll");
        modelView=(modelView) convertView;

    }
    modelView.setModel(diet);


    return modelView;       
}

我的ModelView类看起来像这样:

public class ModelView extends LinearLayout {
Context context;
TextView text1,text2,text3;
ImageView img1;
Model d;
public ModelView(Context context,Model d) {
    super(context);
    this.context=context;
    this.d=d;
    this.setTag(d);
    HookUp();

}

public void HookUp() {

    this.setLayoutParams(new ListView.LayoutParams(
            android.view.ViewGroup.LayoutParams.MATCH_PARENT,
            android.view.ViewGroup.LayoutParams.MATCH_PARENT));
    this.setOrientation(LinearLayout.VERTICAL);

    LayoutInflater inflater = LayoutInflater.from(context);

    view = inflater.inflate(R.layout.lay, null);

    this.addView(view);

    textDietHeadLine=(TextView) view.findViewById(R);
    text=(TextView) view.findViewById(R.id.txt1);
    text=(TextView) view.findViewById(R.id.txt2);

}

}

在我在TouchListener上的我的主要活动中,当我检索Tag时,它总是让我为空,我不知道我在哪里出错...任何帮助将不胜感激,尽管我在此问题上进行了大量搜索,但无济于事,我是android的初学者,现在就安静了一下,请helppp :(

以下是touchListener代码:

     public boolean onTouch(View v, MotionEvent e)
     {
         if (gestureDetector.onTouchEvent(e)){

            // Log.d("Diet d","v.gettag()); 

             return true;
         }else{
             return false;
         }

这有点复杂,我希望我能做对,如果您要获取用户要刷过的特定行,则需要提取一种方法,该方法在ListView调用findMotionRow中带有很深的含义 ,该方法声明为扩展程序的实际私有可见性,因此我们必须使该方法在我们自己的ListView中可用,并学习如何从ListView的源代码中调用该方法:

public class YourListView extends ListView {
    ...Constructors...

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mMotionPosition = findMotionRow(ev.getY());
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }

    private int mMotionPosition;

    public int getMotionPosition() {
        return mMotionPosition;
    }

    /**
     * Find which position is motion on.
     * Note : this method copy into public from 4.0 source code.
     * @param y Y coordinate of the motion event.
     * @return Selected index (starting at 0) of the data item.
     */
    private int findMotionRow(float y) {
        int childCount = getChildCount();
        if (childCount > 0) {
            if (!isStackFromBottom()) {
                for (int i = 0; i < childCount; i++) {
                    View v = getChildAt(i);
                    if (y <= v.getBottom()) {
                        return getFirstVisiblePosition() + i;
                    }
                }
            } else {
                for (int i = childCount - 1; i >= 0; i--) {
                    View v = getChildAt(i);
                    if (y >= v.getTop()) {
                        return getFirstVisiblePosition() + i;
                    }
                }
            }
        }
        return INVALID_POSITION;
    }
}

在适当的位置,即Activity.onCreate(),我们可以监听onTouch事件,并能够使用mMotionPosition获取行的View:

mYourListView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int hoverChildIndex =
            mYourListView.getMotionPosition() - mYourListView.getFirstVisiblePosition();
        ModelView hoveringView = (ModelView) mYourListView.getChildAt(hoverChildIndex);

        return false;
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM