2011-01-22 106 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(); 
    } 
}