选择器的 ListView 项 LongClick 状态 [英] ListView item LongClick state for selector

查看:19
本文介绍了选择器的 ListView 项 LongClick 状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在默认的 ListViews 上,如果您长按某个项目,则在注册 LongClick 期间,背景会从深蓝色变为浅蓝色(在 Galaxy Tab 上.在某些其他设备上,它是橙色到浅橙色).我假设这是通过选择器以某种方式完成的,但到目前为止,我只看到了如何将选择器用于 state:selected、state:focused 和 state:pressed.

On the default ListViews if you long press on an item the background fades from dark blue to light blue(on Galaxy Tab. Its orange to light orange on some other devices) during the time it takes to register a LongClick. I am assuming this is done somehow with a selector but thus far I've only seen how to use selectors for state:selected, state:focused, and state:pressed.

此页面似乎没有显示关于 LongClick 状态的任何信息,所以也许我认为这是通过选择器完成的假设是不正确的?

This page doesn't seem to show anything about a LongClick state so perhaps my assumption that this is accomplished with a selector is incorrect?

谁能告诉我默认 ListView 如何获得这种效果以及如何将其应用于其他视图?

Can anyone fill me in on how the default ListView gets this effect and how I can apply it to other views?

推荐答案

结果比我想象的要困难一些,但我现在几乎可以正常工作了.

So it turned out to be a little bit more difficult than I had thought but I have it working almost correctly now.

这是我最终使用的 OnTouchListener:

Here is the OnTouchListener I ended up using:

listOnTouchListener = new OnTouchListener() {
         public boolean onTouch(View v, MotionEvent me){
             if (me.getAction() == MotionEvent.ACTION_DOWN){
                 //This means a finger has come down on top of our view
                 //We are going to start the animation now.
                 Log.i(myTag, "Action Down");
                 Context mContext = getApplicationContext();
                 Resources res = mContext.getResources();
                 TransitionDrawable transition = (TransitionDrawable) res.getDrawable(R.drawable.listtransition);
                 v.setBackgroundDrawable(transition);
                 //LongClick took approx. 510-530 milliseconds to register after OnTouch. So I set the duration at 500 millis.
                 transition.startTransition(500);
             }else if (me.getAction() == MotionEvent.ACTION_UP){
                 //This means we didn't hold long enough to get a LongClick
                 //Set the background back to the normal one.
                 v.setBackgroundResource(R.drawable.bubblelight);

             }else if (me.getAction() == MotionEvent.ACTION_MOVE){
                 //Do Nothing
             }else if (me.getAction() == MotionEvent.ACTION_CANCEL){
                 Log.i(myTag, "Action Cancel");
                 //This means we are scrolling on the list, not trying to longpress
                 //So set the background back to the normal one.
                 v.setBackgroundResource(R.drawable.bubblelight);
             }
             return false;

         }
     };

我还在其中使用了一个 OnLongClickListener 我将背景设置回正常的.

I also used an OnLongClickListener inside this I set the background back to the normal one.

这是过渡 XML:

<transition xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/bubblelight1" />
  <item android:drawable="@drawable/bubbledark" />
</transition>

您可能会问,bubblelight1 是怎么回事?稍后会详细介绍.

You may be asking whats with the bubblelight1? More on that in a moment.

这是我在适配器中使用的 getView() 方法,用于返回列表中显示的视图:

Here is the getView() Method I use inside my adapter to return the views that get displayed in the List:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }
        Status s = items.get(position);
        if (s != null) {
            v.setOnTouchListener(listOnTouchListener);
            v.setOnLongClickListener(listOnLongClickListener);
            tweetTxt = (TextView) v.findViewById(R.id.tweetTxt);
            timeTxt = (TextView) v.findViewById(R.id.timeTxt);
            if (tweetTxt != null) {
                tweetTxt.setText(s.getText());
                tweetTxt.setOnTouchListener(gestureListener);
            }
            if(timeTxt != null){
                timeTxt.setText(getTimeFromNow(s.getCreatedAt().getTime()));
                //timeTxt.setText(s.getCreatedAt().toLocaleString());
            }
        }
        LinkifyWithTwitter.addLinks(tweetTxt, Linkify.ALL);
        return v;
    }

v.setOnTouchListener(listOnTouchListener);和v.setOnLongClickListener(listOnLongClickListener);是我在上面显示的使用侦听器设置视图的行.

v.setOnTouchListener(listOnTouchListener); and v.setOnLongClickListener(listOnLongClickListener); are the lines that set up the view with the Listeners that I've shown above.

现在关于泡泡灯1.bubblelight 和 bubbledark 是我第一次尝试时使用的九个补丁图像,每当过渡开始而不是背景从bubblelight 过渡到bubbledark 时,bubblelight 会变大并且bubbledark 会出现在bubblelight 内部.所以我有一个亮的大气泡,一个深色的小气泡,然后是里面的文字.为了解决这个问题,我制作了一个bubblelight 的副本,并完全填充了ninepatch 的底部和右侧边缘.不过,我只需要在过渡中制作第一张图像,而不是第二张.如果我以这种方式制作第二张图片,那么我的文本会从气泡中跳出,并且一些文本会显示在气泡的顶部和两侧.我不完全确定为什么会发生这种情况,或者为什么这个修复会起作用.但它现在可以完成这项工作.

Now about the bubblelight1. bubblelight and bubbledark are the nine patch images I am using when I tried this the first time whenever the transition started instead of the background transitioning from bubblelight to bubbledark, bubblelight would grow bigger and bubbledark would appear inside of bubblelight. So I had a big bubble that was light, a smaller bubble that was dark, then the text inside that. To fix this issue I made a copy of bubblelight and made the bottom and right edges of the ninepatch completely filled in. I just had to do the first image in the transition though, not the second. If I did the second image that way then my text would jump out of the bubble and some it would get shown over the top and along the sides of the bubble. I am not entirely sure why this was happening, or why this fix happened to work. But It does the job for now.

这篇关于选择器的 ListView 项 LongClick 状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆