2014-12-31 41 views
0

我有一個與ListView的每一行的多個元素如下:點擊事件沒有被捕獲的ListView項與Horizo​​ntalScrollView

list_element.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/global_container" 
        android:descendantFocusability="blocksDescendants" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 


     <HorizontalScrollView 
      android:id="@+id/scrollview" 
      android:clickable="true" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:scrollbars="none"> 

Fragment1.java被充氣包含佈局包含上述元素的列表視圖。

我需要能夠捕獲每個項目的點擊事件來改變元素的顏色,當單擊項目和單擊另一個項目時,將其恢復爲其原始顏色,因爲新項目更改元素顏色等上。

這意味着,我試圖捕捉OnItemClickListenerFragment1.java

if (rootView != null) { 
     myList = (ListView) rootView.findViewById(R.id.my_list); 
    } 

    ArrayList l = new ArrayList(); 

//Bogus list 
    for (int i=0; i<20; i++) { 
     l.add(new Object()); 
    } 

    adapter = new MyListAdapter(l, getActivity()); 

    myList.setAdapter(adapter); 


    final MyListAdapter ad = adapter; 

    myList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) { 
      ad.setClickedPosition(position); 
    ad.notifyDataSetChanged(); 
     } 
    }); 

但(我認爲)所有的HorizontalScrollView觸摸事件被捕獲點擊/觸摸......事件,我沒有得到任何迴應。正如你所看到的,我嘗試了android:descendantFocusability="blocksDescendants",並且沒有任何結果來回改變可聚焦性。

我也試圖捕捉名單轉接器內不同的事件:

vi = inflater.inflate(R.layout.list_element, null, true); 

vi.setOnClickListener(new View.OnClickListener() { 
    @Override 
     public void onClick(View view) { 

     Log.d("aaa", "aaa"); 


    } 
}); 

和/或

horizontalScroll = vi.findViewById(R.id.scrollview); 

horizontalScroll.setOnClickListener(new View.OnClickListener() { 
    @Override 
     public void onClick(View view) { 

     Log.d("aaa", "aaa"); 


    } 
}); 

兩個選項都沒有任何結果。然後,我應該如何捕獲列表中某個項目的點擊事件並保持能夠與HorizontalScrollView一起使用?

回答

3

好吧,我解決了這個問題,如下所示:

爲了能夠點擊列表,項目這兩件事情需要完成。

  • 第一:

    機器人:descendantFocusability = 「blocksDescendants」

需要存在的行元素佈局的最頂部元件上。

  • 第二:

    機器人:可點擊= 「假」 機器人:可聚焦= 「假」 機器人:focusableInTouchMode = 「假」

這些3個屬性需要存在在Horizo​​ntalScrollView上。

該解決方案的問題是Horizo​​ntalScrollView本身無法正常工作,因此您需要在檢測到單擊事件後激活它。我在適配器的getView()方法上執行它。

當元件被點擊我通過位置到適配器:

fragment.java

list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) { 

     //Private method inside the adapter 
     ad.setClickedPosition(position); 
     ad.notifyDataSetChanged(); 

    } 
}); 

adapter.java

public void setClickedPosition(int clickedPosition) { 
    this.clickedPosition = clickedPosition; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    if (clickedPosition == position) 
     holder.scrollview.setScrollingEnabled(true); 
    else 
     holder.scrollview.setScrollingEnabled(false); 
} 

CustomHorizo​​ntalScrollview.java

public void setScrollingEnabled (boolean enabled) { 
     this.isScrollEnabled = enabled; 
    } 

//... 

    @Override 
    public boolean onTouchEvent(MotionEvent ev) { 

    //... 

    if (isScrollEnabled) { 
     return super.onTouchEvent(ev); 
    } 

    //... 
} 

事情要考慮的是,這種解決方案適用於我,因爲行爲的「第一次點擊,然後刷卡」就是我一直在尋找,所以要知道這可能不是你想要的是什麼,但應該可以幫助你在你的尋求做這項工作。

0

試圖改變自己的list_element.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/global_container" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 


    <HorizontalScrollView 
     android:id="@+id/scrollview"    
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scrollbars="none" /> 
</RelativeLayout> 

希望這可以幫助你

+0

感謝您的回覆,但沒有奏效,這是它原來的樣子。 – Carlos

相關問題