2012-01-28 35 views
6

Vì vậy, thực sự nhanh chóng tôi có một listView với một adapter tùy chỉnh và nó thổi phồng một cái nhìn có chứa một horizontalScrollView cũng như một textview vv Vấn đề tôi đang có là khi tôi cố gắng đính kèm một người nghe vào danh sách nàyXem nó không nhận được bất kỳ cuộc gọi lại nào.ListView với horizontalScrollView OnItemClick không hoạt động

Tôi tin rằng sự cố phải liên quan đến thực tế là mục danh sách của tôi chứa chế độ xem cuộn đang chặn các sự kiện nhấp chuột (mặc dù tôi cho rằng nó chỉ chặn các cử chỉ khác).

mã ... (tôi danh sách mục xml)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linearMain" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 

    android:orientation="vertical" > 

    <RelativeLayout 
     android:id="@+id/relativeLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/grey" > 

     <TextView 
      android:id="@+id/headerName" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:padding="4dp" 
      android:textColor="@color/text_header" 
      android:textStyle="bold" 
      android:text="TextView" /> 

    </RelativeLayout> 
    <View 
     android:background="@color/border" 
     android:layout_width="fill_parent" 
     android:layout_height="1px" /> 
    <HorizontalScrollView 
     android:id="@+id/horizontalScrollView1" 
     android:layout_width="fill_parent" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:layout_height="wrap_content" > 

     <LinearLayout 
      android:id="@+id/linearImages" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="horizontal" 
      android:padding="4dp"> 
     </LinearLayout> 
    </HorizontalScrollView> 
    <View 
     android:background="@color/border" 
     android:layout_width="fill_parent" 
     android:layout_height="1px" /> 
</LinearLayout> 

và sau đó trong onCreate của tôi ...

lv.setAdapter(myobj.adapter); 
    lv.setTextFilterEnabled(true); 
    lv.setOnItemClickListener(new OnItemClickListener(){ 

     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 
      // TODO Auto-generated method stub 
      Log.w("dsdsds", "sdsdsds"); 
     }}); 

Bất kỳ trợ giúp hoặc đề xuất sẽ được đánh giá rất

+0

lời khuyên nào sẽ là tuyệt vời – Maurycy

+0

Vì vậy, không ai trong số các câu trả lời thực sự sửa lỗi này và tôi không có quyền truy cập vào mã cơ sở nữa. Nếu có ai gặp vấn đề này và có một bản sửa lỗi đã biết, tôi sẽ chấp nhận – Maurycy

Trả lời

1

Bạn có thể gọi setOnClickListener cho linearMain trong phương thức getView() của adapter của bạn.

1

cho scrollview bạn nên sử dụng OnTouchListener, như:

private class ScrollViewOnTouchListener implements OnTouchListener{ 
     HorizontalScrollView view; 
     float historicX = Float.NaN; 
     static final int DELTA_ON_CLICK = 20; 
     long historicTime; 
     int position; 
     public ScrollViewOnTouchListener(HorizontalScrollView view, int position){ 
      this.view = view; 
      this.position = position; 
     } 

     @Override 
     public boolean onTouch(View v, MotionEvent event) 
     { 
      switch (event.getAction()) 
      { 
       case MotionEvent.ACTION_DOWN: 
       historicX = event.getX(); 
       historicTime = System.currentTimeMillis(); 
       break; 

       case MotionEvent.ACTION_UP: 

       if (Math.abs(event.getX() - historicX) < DELTA_ON_CLICK) 
        // seems like onclick 
        // do what you want for onClick 

       break; 
       default: return false; 
      } 
      return true; 
     } 


    } 

và trong phương pháp getView adapter:

public View getView(int position, View convertView, ViewGroup parent) { 
    ... 
    HorizontalScrollView scrollItem; 
    scrollItem =( HorizontalScrollView)itemView.findViewById(R.id.item_scroll_view); 
    scrollItem .setOnTouchListener(new ScrollViewOnTouchListener(scrollItem , position));  
} 
2

Thêm phần này vào Layout trên cùng:

android:descendantFocusability="blocksDescendants"

Nó đã làm các trick cho tôi, bây giờ onItemClickListener đang bị sa thải.

+0

cảm ơn nó hữu ích cho tôi .. –

+0

nơi đặt điều này - trong bố cục gốc của mục danh sách? – NarendraJi

+0

@DroidWormNarendra có trong bố cục mặt hàng –

0

Bên list_item.xml đánh dấu bố trí của bạn với một số id:

<?xml version="1.0" encoding="utf-8"?> 
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
         xmlns:tools="http://schemas.android.com/tools" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:fillViewport="true"> 

    <LinearLayout 
     android:id="@+id/layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

... 

Mở bộ chuyển đổi tùy chỉnh của bạn, trong getView(...) viết:

layout = view.findViewById(R.id.layout); 
layout.setTag(position); 
layout.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (callback != null) { 
       callback.showItem(getItem((int) v.getTag())); 
      } 
     } 
    }); 

Dưới đây trong adapter viết:

public interface OnClickInterface { 
    void showItem(YourClass item); 
} 

Và trong hàm tạo của bộ điều hợp gán một cuộc gọi lại:

public CustomAdapter(Context context, List<YourClass> list, OnClickInterface callback, int resId) { 
    this.callback = callback; 
    ... 

Trong hoạt động của bạn/đoạn có chứa một ListView tạo ra một bộ chuyển đổi như sau:

CustomAdapter.OnClickInterface callback = new CustomAdapter.OnClickInterface() { 
     @Override 
     public void showItem(YourClass item) { 
      // Do something with an item. 
     } 
} 
adapter = new Customdapter(getActivity(), new ArrayList<YourClass>(), callback, R.layout.list_item); 
Các vấn đề liên quan