2015-10-24 7 views
0

Извините за мой английский. Я провожу много раз, но я не могу исправить свою проблему. У меня есть listView, и я хочу установить его setOnItemClickListener. Это мой ListView:ListView setOnItemClickListener не работает

<ListView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:id = "@+id/listScene" 
        android:clickable="true" 
        android:cacheColorHint="#00000000" 
        android:divider="#adb8c2" 
        android:dividerHeight="1dp" 
        android:scrollingCache="false" 
        android:smoothScrollbar="true" 
        > 
       </ListView> 

Сво мой код

public void outputListView(ArrayList<String> list) { 
     Adapter adapter = new Adapter(getApplicationContext(), R.layout.item, list); 
     adapter.notifyDataSetChanged(); 
     listView.setAdapter(adapter); 

     listView.setItemsCanFocus(false); 
     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
       Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 

, но когда я нажимаю в пункте его ничего не происходит

UPD

Мой item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <service.SwipeLayout 
     android:layout_marginTop="5dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <LinearLayout 
       android:orientation="horizontal" 
       android:background="#32777777" 
       android:layout_width="match_parent" 
       android:layout_height="70dp"> 

       <RelativeLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:background="#fff" 
        android:layout_weight="0.7" 
        > 

        <TextView 
         android:id="@+id/conditions" 
         android:layout_marginTop="10dp" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textSize="22dp" 
         android:text="" 
         android:textColor="#25ac36" 
         android:layout_centerVertical="true" 
         android:layout_centerHorizontal="true" /> 

       </RelativeLayout> 

       <RelativeLayout 
        android:layout_weight="0.3" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 

        <TextView 
         android:id="@+id/description" 
         android:layout_marginLeft="10dp" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textSize="18dp" 
         android:text="" 
         android:textColor="#222222" 
         android:layout_centerVertical="true" 
         /> 
       </RelativeLayout> 

      </LinearLayout> 

      <RelativeLayout 
       android:background="#66777777" 
       android:layout_width="match_parent" 
       android:layout_height="40dp"> 

       <TextView 
        android:id="@+id/rooms" 
        android:layout_marginLeft="10dp" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textSize="18dp" 
        android:text="" 
        android:textColor="#fff" 
        android:layout_centerVertical="true" 
        /> 
      </RelativeLayout> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="250dp" 
      android:layout_height="match_parent"> 

      <RelativeLayout 
       android:id="@+id/open_b" 
       android:clickable="true" 
       android:layout_width="78dp" 
       android:layout_marginRight="5dp" 
       android:layout_marginLeft="5dp" 
       android:layout_height="match_parent" 
       android:background="#4fcc54"> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textSize="18dp" 
        android:text="Open" 
        android:textColor="#fff" 
        android:id="@+id/openText" 
        android:layout_centerVertical="true" 
        android:layout_centerHorizontal="true" 
        /> 
      </RelativeLayout> 

      <RelativeLayout 
       android:id="@+id/disable_b" 
       android:clickable="true" 
       android:layout_width="78dp" 
       android:layout_marginRight="5dp" 
       android:layout_height="match_parent" 
       android:background="#8e8e8e"> 
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textSize="18dp" 
        android:text="Disable" 
        android:textColor="#fff" 
        android:id="@+id/disableT" 
        android:layout_centerVertical="true" 
        android:layout_centerHorizontal="true" 
        /> 
      </RelativeLayout> 

      <RelativeLayout 
       android:id="@+id/delete_b" 
       android:clickable="true" 
       android:layout_width="78dp" 
       android:layout_height="match_parent" 
       android:background="#de5454"> 
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textSize="18dp" 
        android:text="Delete" 
        android:textColor="#fff" 
        android:id="@+id/deleteText" 
        android:layout_centerVertical="true" 
        android:layout_centerHorizontal="true" 
        /> 
      </RelativeLayout> 
     </LinearLayout> 
    </service.SwipeLayout> 

</LinearLayout> 

UPD: простой ListView

activity_main .xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 

    <ListView 
     android:id="@+id/listView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:cacheColorHint="#00000000" 
     android:divider="#adb8c2" 
     android:dividerHeight="1dp" 
     android:scrollingCache="false" 
     android:smoothScrollbar="true" /> 


</RelativeLayout> 

мой главный

ArrayList<String> asd = new ArrayList<>(); 
    for(int i = 0; i < 10; i++) { 
     asd.add(String.valueOf(i)); 
    } 

    ListView listView = (ListView) findViewById(R.id.listView); 
    Adapter adapter = new Adapter(getApplicationContext(), R.layout.item, asd); 
    listView.setAdapter(adapter); 

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
      Toast.makeText(getBaseContext(), String.valueOf(position), Toast.LENGTH_LONG).show(); 
     } 
    }); 

и item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Large Text" 
     android:id="@+id/textView" /> 
</LinearLayout> 

Его хорошо, его работа, когда я нажимаю в элементе listView, Тост показывает позицию. Но мне нужно использовать SwipeLayout тогда я изменить код item.xml и добавить:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    android:descendantFocusability="blocksDescendants" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <com.bottom.smart.detetethis.SwipeLayout 
     android:layout_marginTop="5dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <LinearLayout 
       android:orientation="horizontal" 
       android:background="#32777777" 
       android:layout_width="match_parent" 
       android:layout_height="70dp"> 

       <RelativeLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:background="#fff" 
        android:layout_weight="0.7" 
        > 

        <TextView 
         android:id="@+id/conditions" 
         android:layout_marginTop="10dp" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textSize="22dp" 
         android:text="Active" 
         android:textColor="#25ac36" 
         android:layout_centerVertical="true" 
         android:layout_centerHorizontal="true" /> 

       </RelativeLayout> 

       <RelativeLayout 
        android:layout_weight="0.3" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 

        <TextView 
         android:id="@+id/description" 
         android:layout_marginLeft="10dp" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textSize="18dp" 
         android:text="Turn light on \n when people arrive" 
         android:textColor="#222222" 
         android:layout_centerVertical="true" 
         /> 
       </RelativeLayout> 

      </LinearLayout> 

      <RelativeLayout 
       android:background="#66777777" 
       android:layout_width="match_parent" 
       android:layout_height="40dp"> 

       <TextView 
        android:id="@+id/rooms" 
        android:layout_marginLeft="10dp" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textSize="18dp" 
        android:text="Livingroom, Kitechen, Rest room" 
        android:textColor="#fff" 
        android:layout_centerVertical="true" 
        /> 
      </RelativeLayout> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="250dp" 
      android:layout_height="match_parent"> 

      <RelativeLayout 
       android:id="@+id/open_b" 
       android:layout_width="78dp" 
       android:layout_marginRight="5dp" 
       android:layout_marginLeft="5dp" 
       android:layout_height="match_parent" 
       android:background="#4fcc54"> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textSize="18dp" 
        android:text="Open" 
        android:textColor="#fff" 
        android:id="@+id/openText" 
        android:layout_centerVertical="true" 
        android:layout_centerHorizontal="true" 
        /> 
      </RelativeLayout> 

      <RelativeLayout 
       android:id="@+id/disable_b" 
       android:layout_width="78dp" 
       android:layout_marginRight="5dp" 
       android:layout_height="match_parent" 
       android:background="#8e8e8e"> 
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textSize="18dp" 
        android:text="Disable" 
        android:textColor="#fff" 
        android:id="@+id/disableT" 
        android:layout_centerVertical="true" 
        android:layout_centerHorizontal="true" 
        /> 
      </RelativeLayout> 

      <RelativeLayout 
       android:id="@+id/delete_b" 
       android:layout_width="78dp" 
       android:layout_height="match_parent" 
       android:background="#de5454"> 
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textSize="18dp" 
        android:text="Delete" 
        android:textColor="#fff" 
        android:id="@+id/deleteText" 
        android:layout_centerVertical="true" 
        android:layout_centerHorizontal="true" 
        /> 
      </RelativeLayout> 
     </LinearLayout> 
    </com.bottom.smart.detetethis.SwipeLayout> 

</LinearLayout> 

заболела, ее не работает, когда я нажимаю пункт ListView. Но когда я получаю палец между элементом оформления, его работы:

enter image description here

виноват SwipeLayout, код SwipeLayout:

public class SwipeLayout extends LinearLayout { 

    private ViewDragHelper viewDragHelper; 
    private View contentView; 
    private View actionView; 
    private int dragDistance; 
    private final double AUTO_OPEN_SPEED_LIMIT = 400.0; 
    private int draggedX; 

    public SwipeLayout(Context context) { 
     this(context, null); 
    } 

    public SwipeLayout(Context context, AttributeSet attrs) { 
     this(context, attrs, -1); 
    } 

    public SwipeLayout(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     viewDragHelper = ViewDragHelper.create(this, new DragHelperCallback()); 
    } 

    @SuppressLint("MissingSuperCall") 
    @Override 
    protected void onFinishInflate() { 
     contentView = getChildAt(0); 
     actionView = getChildAt(1); 
     actionView.setVisibility(GONE); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     dragDistance = actionView.getMeasuredWidth(); 
    } 

    private class DragHelperCallback extends ViewDragHelper.Callback { 

     @Override 
     public boolean tryCaptureView(View view, int i) { 
      return view == contentView || view == actionView; 
     } 

     @Override 
     public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { 
      draggedX = left; 
      if (changedView == contentView) { 
       actionView.offsetLeftAndRight(dx); 
      } else { 
       contentView.offsetLeftAndRight(dx); 
      } 
      if (actionView.getVisibility() == View.GONE) { 
       actionView.setVisibility(View.VISIBLE); 
      } 
      invalidate(); 
     } 

     @Override 
     public int clampViewPositionHorizontal(View child, int left, int dx) { 
      if (child == contentView) { 
       final int leftBound = getPaddingLeft(); 
       final int minLeftBound = -leftBound - dragDistance; 
       final int newLeft = Math.min(Math.max(minLeftBound, left), 0); 
       return newLeft; 
      } else { 
       final int minLeftBound = getPaddingLeft() + contentView.getMeasuredWidth() - dragDistance; 
       final int maxLeftBound = getPaddingLeft() + contentView.getMeasuredWidth() + getPaddingRight(); 
       final int newLeft = Math.min(Math.max(left, minLeftBound), maxLeftBound); 
       return newLeft; 
      } 
     } 

     @Override 
     public int getViewHorizontalDragRange(View child) { 
      return dragDistance; 
     } 

     @Override 
     public void onViewReleased(View releasedChild, float xvel, float yvel) { 
      super.onViewReleased(releasedChild, xvel, yvel); 
      boolean settleToOpen = false; 
      if (xvel > AUTO_OPEN_SPEED_LIMIT) { 
       settleToOpen = false; 
      } else if (xvel < -AUTO_OPEN_SPEED_LIMIT) { 
       settleToOpen = true; 
      } else if (draggedX <= -dragDistance/2) { 
       settleToOpen = true; 
      } else if (draggedX > -dragDistance/2) { 
       settleToOpen = false; 
      } 

      final int settleDestX = settleToOpen ? -dragDistance : 0; 
      viewDragHelper.smoothSlideViewTo(contentView, settleDestX, 0); 
      ViewCompat.postInvalidateOnAnimation(SwipeLayout.this); 
     } 
    } 

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) { 
     if(viewDragHelper.shouldInterceptTouchEvent(ev)) { 
      return true; 
     } 
     return super.onInterceptTouchEvent(ev); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     viewDragHelper.processTouchEvent(event); 
     return true; 
    } 

    @Override 
    public void computeScroll() { 
     super.computeScroll(); 
     if(viewDragHelper.continueSettling(true)) { 
      ViewCompat.postInvalidateOnAnimation(this); 
     } 
    } 
} 

Я не могу удалить его, потому что это нужно в проекте , Я не знаю, как исправить эту ситуацию

+1

Может быть, потому, что элемент списка потребляет событие click. –

+1

post item.xml .. – Blackbelt

+0

@Blackbelt i update qestion – g8214435

ответ

1

в вашем ListView удалить атрибут XML android:clickable="true"

Это принимает ваше событие щелчка и не посылая его вниз к мнению ребенка.

Редактировать

Вы передаете в getApplicationContext(), который является неправильным контекстом. Слушатель работает, но Toast не отображается в правильном контексте. Вы должны пройти getContext(). Однако, так как это действие, вы можете просто перечислить в this.

public void outputListView(ArrayList<String> list) { 
    Adapter adapter = new Adapter(this, R.layout.item, list); 
    adapter.notifyDataSetChanged(); 
    listView.setAdapter(adapter); 

    listView.setItemsCanFocus(false); 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
      Toast.makeText(this, String.valueOf(position), Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 
+0

Не помогите мне – g8214435

+1

использовать 'getContext()' вместо 'getApplicationContext()' – overflowingStack

+0

У меня '' невозможно разрешить метод getContext() ' – g8214435

2

Иногда клик ListView не работает. И в этом случае вам может понадобиться добавить еще один атрибут.

android:descendantFocusability="blocksDescendants" 

Добавить этот атрибут в начало самой компоновки вашего XML, где вы предоставили элементы ListView.

+0

его не помогло мне – g8214435

+0

большое спасибо, я потратил два дня, чтобы найти ваше решение –

Смежные вопросы