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

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

问题描述

在默认的列表视图,如果从深蓝色时才能注册LongClick的时代背景渐变为浅蓝色(上的Galaxy Tab,它的橙色至浅橙色一些其他设备)的一个项目,你长的preSS 。我假定这是一个选择某种方式完成,但到目前为止,我只看到了如何使用选择的国家:选择国家:专注,状态: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.

现在关于bubblelight1。 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天全站免登陆