2011-01-22 114 views
4

在默认的ListViews上,如果您长时间按住某个项目,背景将从深蓝色变为浅蓝色(在Galaxy Tab上,其橙色变为其他设备上的橙色)注册一个LongClick。我假设这是用选择器以某种方式完成的,但到目前为止,我只看到如何使用状态选择器:selected,state:focused和state:pressed。ListView项目选择器的LongClick状态

This page似乎没有显示任何关于LongClick状态,所以也许我的假设,这是用选择器完成是不正确的?

任何人都可以填写我默认的ListView如何得到这种效果,以及如何将它应用到其他视图?

+0

这样做http://stackoverflow.com/questions/6513301/android-how-to-achieve-the-glow-effect-when-的简单方法long-pressing-a-list-item – Sabre 2014-09-22 00:00:40

回答

4

所以结果比我想象的要困难一点,但现在我的工作几乎正确。

这里是我结束了使用OnTouchListener:

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这里面我设置的背景回到正常的一个。

这里是转变XML:

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

你可能会问与bubblelight1什么?更多关于这一点。

这是我用我的转接器内返回该得到显示在列表视图的getView()方法:

@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);是与上面显示的听众一起设置视图的线条。

现在关于bubblelight1。泡泡灯和泡泡堂是我在第一次尝试这个过程时使用的9个补丁图像,而不是从泡泡灯到泡泡堂的过渡,泡泡灯会变得更大,泡泡灯会出现在泡泡灯内。所以我有一个很大的泡沫,很小,泡沫很暗,然后是里面的文字。为了解决这个问题,我制作了一个bubblelight的副本,并且使得ninepatch的底部和右侧边缘完全被填充。我只需要在转换过程中完成第一个图像,而不是第二个。如果我这样做了第二张图像,那么我的文字就会从泡泡中跳出来,有些则会在泡泡的顶部和两侧显示出来。我不完全确定为什么会发生这种情况,或者为什么这种修复方法发生了工作。但它现在完成这项工作。

1

http://developer.android.com/guide/topics/ui/menus.html#context-menu

当用户在ListView上的项目执行长按和列表中登记,以提供上下文菜单,列表项的信号到一个上下文菜单可以由用户为其背景颜色设置动画 - 在打开上下文菜单之前,它将从橙色转换为白色。 (联系人应用程序演示此功能。)

所以它使用的动画。我相信它使用View的默认On ...()方法来显示它。您可能需要担心提供clickable =“true”或longClickable =“true”属性。

+0

所以我即将设置一个OnTouchListener来启动一个动画来转换视图的背景。有没有人碰巧知道需要多长时间才能成为LongClick的注册商标?所以我可以同步动画的持续时间,而无需在我的手上进行一些试验和错误 – FoamyGuy 2011-01-22 18:06:19

+0

经过第二次考虑后,我认为我可以在OnTouch中检查系统时间,然后在OnLongClick中再次检查系统时间,然后再次获取持续时间。如果有人碰巧知道系统默认使用的动画名称,这些名称对我来说仍然很有帮助。 – FoamyGuy 2011-01-22 18:23:16

0

除了您的OnTouchListener,您还可以使用Runnable将背景恢复为原始状态,以便您不需要在OnLongClick处理程序中明确执行此操作。

Handler myHandler = new Handler(); 
... 
transition.startTransition(ViewConfiguration.getLongPressTimeout()); 
ResetBG r = new ResetBG(transition); 
myHandler.postDelayed(r, ViewConfiguration.getLongPressTimeout()); 

其中ResetBG是:

class ResetBG implements Runnable { 
    protected TransitionDrawable myTran; 

    public Runnable(TransitionDrawable tran) { 
     myTran = tran; 
    } 

    public void run() { 
     myTran.resetTransition(); 
    } 
}