ListView onItemClickListener在动画后返回错误的位置

时间:2014-11-06 06:40:24

标签: android listview

我有一个带有自定义BaseAdaptor的ListView,它没有做任何花哨的事情,只需选择要在每一行中显示的正确字符串。

我的问题是附加到ListView的onItemClickListener似乎正在选择/返回错误的索引。更具体地说,它选择实际尝试按下的行下面的索引。

所以当我尝试点击第一行时,我可以看到第二行突出显示,onItemClickListener返回位置1而不是0.同样,当我尝试按第二行时,第三行突出显示有趣的是,当我尝试点击ListView上方的区域时,第一行会突出显示,即使ListView显然没有扩展到我可以通过设置不同的背景颜色看到的上方。

然而,我正在做的一件奇怪的事情就是在FrameLayout中的另一个视图后面向上和向下动画这个listView。这似乎与问题有关,好像我直接在屏幕上渲染ListView,所有的水龙头似乎都正确注册。一旦我制作动画,水龙头就不再正确注册了。

这是我的动画代码:

Boolean shouldOpen = !isDropdownShowing;

RelativeLayout selectionView = (RelativeLayout) findViewById(R.id.selectedView); //The view that the list is being animated behind
int listHeight = dropdownListView.getHeight();
int heightOfSelectionView = selectionView.getHeight();

TranslateAnimation translateAnimation;
if (shouldOpen) {
    translateAnimation = new TranslateAnimation (0.0f, 0.0f, heightOfSelectionView - listHeight, heightOfSelectionView);
    dropdownListView.setY(heightOfSelectionView);
} else {
   translateAnimation = new TranslateAnimation (0.0f, 0.0f, heightOfSelectionView, heightOfSelectionView - listHeight);
   dropdownListView.setY(heightOfSelectionView - listHeight);
}

translateAnimation.setFillAfter(true);
translateAnimation.setDuration(300L);
translateAnimation.setRepeatCount(0);
dropdownListView.startAnimation(translateAnimation);

以下是我如何初始化此ListView:

dropdownListView = new ListView(getContext());
dropdownListView.setAdapter(new EventsSpinnerAdapter(getContext(), options));
dropdownListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.d("dropdown", "itemClicked at index " + position);
        selectOption(position);
        handleEventSelectionRowClicked();
    }
});

这是BaseAdapter:

private class EventsSpinnerAdapter extends BaseAdapter {
    LayoutInflater inflater;
    List<String> options;
    int selectedIndex;

    public EventsSpinnerAdapter(Context context, List data)
    {
        inflater = ((Activity) context).getLayoutInflater();
        options = data;
        selectedIndex = 0;
    }

    @Override
    public int getCount()
    {
        return options.size()-1;
    }

    @Override
    public Object getItem(int position)
    {
        return options.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        if (position >= selectedIndex) {
            return position+1;
        } else {
            return position;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView == null) {
            convertView = inflater.inflate(R.layout.listitem, null);
        }

        ((TextView)convertView).setText(options.get((int) getItemId(position)));
        return convertView;
    }


    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {

        if(convertView == null) {
            convertView = inflater.inflate(R.layout.listitem, null);
        }

        ((TextView)convertView).setText(options.get((int) getItemId(position)));
        return convertView;
    }
}

修改

事实证明这是动画的一个问题。我仍然不太明白为什么我的原始代码不起作用,但我发现添加了一个再次设置框架并清除动画效果的监听器。

translateAnimation.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        if (shouldOpen) {
            isDropdownShowing = true;
            dropdownListView.setY(heightOfSelectionView);
        } else {
            isDropdownShowing = false;
            dropdownListView.setY(heightOfSelectionView - listHeight);
        }
        dropdownListView.clearAnimation();
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
});

1 个答案:

答案 0 :(得分:0)

试试这个

dropdownListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Log.d("dropdown", "itemClicked at index " + position);
    selectOption(position);
    handleEventSelectionRowClicked();
}
});

它为我工作 我没有使用new AdapterView.OnItemClickListener()

这是我的代码

lvs.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long id) {
            // TODO Auto-generated method stub
            TextView names = (TextView) arg1.findViewById(R.id.from);
            TextView emails = (TextView) arg1.findViewById(R.id.date);

        }
    });