【问题标题】:How to get parent view of checkbox in listview items如何在列表视图项目中获取复选框的父视图
【发布时间】:2014-05-26 05:23:12
【问题描述】:

我的列表视图项有一个文本视图、图像视图和复选框。当我单击复选框时,我会收到 setonclick 侦听器的事件,我需要使用动画删除已删除的行。

因此,为此,如果我使用复选框视图,我只会为复选框获得动画外观,而不是其他视图,如 textview、imageview。

这里是动画代码

 OnClickListener CheckboxListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            CheckBox cb = (CheckBox) v ; 

             final Animation animation = AnimationUtils.loadAnimation(v.getContext(), R.anim.splashfadeout);
                v.startAnimation(animation);
                Handler handle = new Handler();
                handle.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                       mcompletecallback.completePressedItem(cursorHelper.primaryKey);
                        notifyDataSetChanged();
                        animation.cancel();
                    }
                }, 1000);
}
}

在这个v 中是复选框视图,但我想知道包含复选框的视图,如果我在其上应用动画,它工作顺利。但我想知道如何在这里获得它?

这是我的动画 xml 代码

<?xml version="1.0" encoding="utf-8"?>

<scale
    android:duration="500"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:interpolator="@android:anim/linear_interpolator"
    android:toXScale="1.0"
    android:toYScale="0.0" />

我的适配器代码

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

    String todayDate = Utilities.getTodayDate();
    String tomorrowDate = Utilities.getTomorrowDate();


    ViewHelper viewholder = null;
    DividerHelper dividerviewholder = null;


    int view = getItemViewType(position);

    if(view == TYPE_HEADER)
    {
        //perform header actions
    }
    else
    {
        //Cursor c = (Cursor) getItem(position-cursorPosition);

        Cursor c = (Cursor) getItem(position);

        if(convertView == null)
        {
            viewholder = new ViewHelper();

            convertView = mLayoutInflater.inflate(R.layout.layout_item, parent, false);

            viewholder.title = (TextView)convertView.findViewById(R.id.ID_item_title);
            viewholder.Notes = (TextView)convertView.findViewById(R.id.ID_item_notes);
            viewholder.Status = (CheckBox)convertView.findViewById(R.id.ID_item_is_completed_checkbox);
            convertView.setTag(viewholder);
            viewholder.Status.setTag(viewholder);
            viewholder.Status.setOnClickListener(completedCheckboxListener);

        }
        else
        {
            viewholder = (ViewHelper)convertView.getTag();

        }

        viewholder.primaryKey = c.getInt(0);
        viewholder.Notes = c.getString(9);
        viewholder.Title = c.getString(3);




    }

    return convertView;
}

【问题讨论】:

  • 那个选项不存在,我想我试过了。如果我们点击复选框,有没有办法获得点击的视图,即整行?
  • 每个视图都应该有 getParent() 来获取视图的父级。否则,您可以将 onClickListener() 写入 listadapter 的 getView 中,您可以将动画设置为列表的整个 rowView(在 getView() 中)。
  • 尝试将父引用设置为 setTag() 到 CheckBox 并尝试在 onClick 使用 getTag() 访问此引用...
  • 我需要动画只在单击复选框时执行,而不是在整个视图上执行。
  • 嗨,harish,我尝试了这种方法,但如果我这样做,我会得到正确的动画,但它会留下一些空白的视图。整个列表视图的行为正在改变:(。我认为这也不是最好的尝试方式

标签: android checkbox android-listview android-animation android-listfragment


【解决方案1】:

在该列表视图上应用 onItemClickListener,当您单击该行时,您将获得

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
    //here "view" is your complete row that you clicked where you can find your checkbox

}

【讨论】:

  • 嗨 Navajot,感谢您的回复,我希望动画仅在单击复选框时执行,而不是在单击列表视图时执行
  • 嗨火影忍者,那么您可以将完整行视图的标签设置为复选框视图“checkbox.settag(row)”,每当您单击复选框时,您都会从标签“( View)checkbox.gettag()" 被点击的复选框。
  • 为了进行测试,我尝试将整个视图对象放入标记中,并在复选框 clicklistner 中访问标记对象并应用动画,它工作顺利。但是这里 listview 的行为是有线的,我在删除项目后得到了 balk 视图。而且这样做也不是个好主意。
  • 你需要处理你的listview适配器的getView,而不是填充通过维护arraylist或其他东西删除的视图。
  • 你可以查看这个库的源代码github.com/timroes/SwipeToDismissUndoList,这样就解决了你的问题
【解决方案2】:

感谢Sripathi &amp; Navjot Bedi

我会想办法,

我们需要调用View v1 = (View) cb.getParent();来获取checkbox的父视图,然后

在 V1 对象上调用动画,

动画动画 = AnimationUtils.loadAnimation(v1.getContext(), R.anim.splashfadeout); CompleteUponCompletionofAnimation(动画, cursorHelper.key); v1.startAnimation(动画);

public void CompleteUponCompletionofAnimation(Animation fadeout, final int pk)
    {
        fadeout.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) { }
            @Override
            public void onAnimationRepeat(Animation animation) { }

            @Override
            public void onAnimationEnd(Animation animation) {
                //your change at end of animation
               notifyDataSetChanged();
            }
        });
    }

同样在convertview中,通过设置NULL来移除动画

if(convertView == null)
{
}
else
{
if(convertView.getAnimation() != null)
convertView.setAnimation(null);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 2016-08-04
    • 1970-01-01
    • 2012-05-03
    相关资源
    最近更新 更多