2012-10-31 64 views
2

我有一個Horizo​​ntalListView https://github.com/vieux/Android-Horizontal-ListView裏面有幾個項目,每個這些項目都有一個相對的佈局,包含一些文本和圖像。我已經添加了一個選擇器到這個相對的佈局,它在新的Android設置版本中很好用,當按下每個項目時,白色的每個項目的相關佈局。Horizo​​ntalListView選擇器

問題是,在Android的舊版本中,Horizo​​ntalListview在我滾動時開始動作怪異,這是因爲我爲每個相對佈局添加了android:clickable =「true」。如果我刪除這個屬性,HLV開始正常工作,但選擇器不會工作。

這是我的佈局:

<RelativeLayout 
     android:id="@+id/marcoNota" 
     android:layout_width="@dimen/tile_width" 
     android:layout_height="@dimen/tile_height" 
     android:background="@drawable/tile_selector" 
     android:clickable="true" 
     android:paddingBottom="2dp" 
     android:paddingTop="2dp" 
     android:paddingLeft="2dp" 
     android:paddingRight="2dp"> 
</RelativeLayout> 

我甚至嘗試設置適配器獲得相同的結果上onTouchListener。

我想知道是否有可能使用選擇器缺省設置Clickable =「true」或任何解決此問題。

+0

如果我理解正確,你應該可以使用onItemClickListener,[docs](http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html),但我不知道如果它會給出任何不同的結果比onTouchListener – codeMagic

+0

不,它會給出相同的結果,謝謝你的回覆 – BigBen3216

回答

2

我解決了問題實現在Horizo​​ntalListView.java以下方法:

這是用於檢測Action_UP(當用戶提起手指關閉所選視圖和油漆的相對佈局保持回默認顏色) :

@Override 
    public boolean onTouchEvent(MotionEvent event) { 
     if(habilitar){ 
      if (event.getAction() == android.view.MotionEvent.ACTION_UP ||event.getAction() == android.view.MotionEvent.ACTION_CANCEL){ 
       for(int i=0;i<getChildCount();i++){ 
        View child = getChildAt(i); 
        if (isEventWithinView2(event, child)) { 
         if(mOnItemUnSelectedListener != null){ 
          mOnItemUnSelectedListener.onItemUnSelected(child); 
         } 
         break; 
        } 
        if(mOnItemUnSelectedListener != null && child != null){ 
         mOnItemUnSelectedListener.onItemUnSelected(child); 
        } 
       } 
      } 
     } 

     return true; 
    } 

mOnGesture gesturedetector聲明裏面我在onDown方法中添加了這個功能。這允許油漆所選擇的項目相對佈局保持的內容,當用戶在視圖上壓:

for(int i=0;i<getChildCount();i++){ 
        View child = getChildAt(i); 
        if (isEventWithinView2(e, child)) { 
         if(mOnItemSelected != null){ 
          mOnItemSelected.onItemSelected(HorizontalListView.this, child, mLeftViewIndex + 1 + i, mAdapter.getItemId(mLeftViewIndex + 1 + i)); 
         } 
         break; 
        } 
       } 

然後我加入一些聽衆以允許適配器漆按壓視圖,對於非選擇聽者我實現下面的接口:

private OnItemUnSelectedListener mOnItemUnSelectedListener; 

public void setOnItemUnSelectedListener(OnItemUnSelectedListener listener){ 
     mOnItemUnSelectedListener = listener; 

} 
public interface OnItemUnSelectedListener{ 
    public void onItemUnSelected(View v); 
} 

的項目中選擇收聽我用在類中已定義的:

private OnItemSelectedListener mOnItemSelected; 

終於在我的適配器I SE t爲聽衆:

HorizontalListView listview = (HorizontalListView) retval.findViewById(R.id.listviewH); 
listview.setOnItemSelectedListener(this); 
listview.setOnItemUnSelectedListener(this); 

,我設置佈局的顏色取決於如果用戶按壓或不認爲

@Override 
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) { 
     if(arg1.findViewById(R.id.marcoNota) != null){ 
      arg1.findViewById(R.id.marcoNota).setBackgroundColor(Color.WHITE); 
     } 

    } 

    @Override 
    public void onNothingSelected(AdapterView<?> arg0) { 


    } 

    @Override 
    public void onItemUnSelected(View v) { 
     if(v.findViewById(R.id.marcoNota) != null){ 
     v.findViewById(R.id.marcoNota).setBackgroundColor(Color.BLACK); 
     } 
    } 

我知道這是一個非常具體的問題,但在這裏它是,如果任何人需要做同樣的事情。

相關問題