2012-02-24 2 views
3

내 listview에 체크 박스, textviews 및 imageview가 있습니다. 항목을 클릭하면 onclicklistnere가 작동하지 않습니다. 확인란이 자체 리스너를 가지고 있으며 listview onclicklistener를 재정의하기 때문에이 사실을 알고 있습니다. 벌금. 내가 체크 박스에안드로이드리스트보기 onclicklistener가 체크 박스와 함께 작동하지 않습니다.

android:focusable="false" 
android:focusableInTouchMode="false" 

을 설정할 때, 나는 목록 항목을 클릭 할 수 있지만, 체크 박스 (아이템 등)뿐만 아니라 오렌지를 becames.

반식법이므로 누구보다 나은가요?

public class ListViewTutorial2Activity extends Activity { 

    SimpleAdapter mSchedule; 
    ListView list; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     list = (ListView) findViewById(R.id.SCHEDULE); 

     ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); 
     HashMap<String, String> map = new HashMap<String, String>(); 
     map.put("train", "101"); 
     map.put("from", "6:30 AM"); 
     map.put("to", "7:40 AM"); 
     mylist.add(map); 
     map = new HashMap<String, String>(); 
     map.put("train", "103(x)"); 
     map.put("from", "6:35 AM"); 
     map.put("to", "7:45 AM"); 
     mylist.add(map); 
     mSchedule = new SimpleAdapter(this, mylist, R.layout.row, 
        new String[] {"train", "from", "to"}, new int[] {R.id.TRAIN_CELL, R.id.FROM_CELL, R.id.TO_CELL}); 
     list.setAdapter(mSchedule); 

     list.setOnItemClickListener(new OnItemClickListener() 
     { 
     @Override 
     public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
      Object o = list.getItemAtPosition(position); 
      } 
     }); 
    } 
} 

및 row.xml : 당신은이 작업을 수행하는 확인란에 특별한 속성을 설정할 필요가 없습니다

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:paddingTop="4dip" 
    android:paddingBottom="6dip" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <CheckBox 
     android:id="@+id/check" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:clickable="true" 
     android:descendantFocusability="beforeDescendants"> 
    </CheckBox> 
    <TextView android:id="@+id/TRAIN_CELL" 
     android:layout_width="50dip" 
     android:layout_height="wrap_content"/> 

    <TextView android:id="@+id/FROM_CELL" 
     android:layout_width="70dip" 
     android:layout_height="wrap_content" android:layout_weight="1"/> 

    <TextView android:id="@+id/TO_CELL" 
     android:layout_width="60dip" 
     android:layout_height="wrap_content" android:layout_weight="1"/> 

      <ImageView 
     android:id="@+id/icon" 
     android:layout_width="22px" 
     android:layout_height="22px" 
     android:layout_marginLeft="4px" 
     android:layout_marginRight="10px" 
     android:layout_marginTop="4px" 
     android:src="@drawable/icon" > 
    </ImageView> 
</LinearLayout> 

답변

1

는 클래스입니다. 그러나 ListView에서 다음을 시도하십시오.

그래도 작동하지 않으면 다른 곳에서 잘못된 작업을하고있을 가능성이 큽니다.

+0

에 대한 onStateChangedListener을했기 때문에 내 문제는 것을 깨달았다 확인란이를 추가 할 때 – erdomester

+0

ListViews 및 ExpandableListViews를 그런 식으로 처리하면 안됩니다 (항목에 직접 액세스하려고 시도). 대신 사용자 정의 ListAdapter (예 : extends SimpleAdapter)를 만들고 .getView() 메소드를 재정의해야합니다. 사용자 정의 버전의 .getView() 메소드에서 CheckBox (즉, view.findViewById (R.id.itemcheckbox))의 하위보기에서 로컬 검색을 수행하고 거기에 OnClickListener를 설정할 수 있습니다. ListView는 다른 항목에 대해 Views를 재사용하므로 이러한 방식으로 ListView와 해당 하위보기를 처리해야합니다. – straya

+0

이 설명에 대한 귀하의 대답을 수락합니다.감사. 나는 나의 문제를 마침내 풀 수 있었다. 그러나 그것은 쉽지 않았다. – erdomester

7

xml 레이아웃에서 CheckBox에 추가해 보았습니까?

android:focusable="false" 
android:focusableInTouchMode="false" 

이것은 나를 위해 일한 다음 더 나은 해결책이 될 수 있습니다.

1
android:focusable="false" 

그것은 내가 RecyclerView에서이 문제를 충족 이러한 맥락

0

에 문제를 해결할 것, 어쩌면이 대답은 누군가에 도움이 될 것입니다. 이 모든 답변이 도움이되지 못합니다. 어쩌면이 솔루션은 최적 아니지만, 여기있다 :

<FrameLayout 
      android:id="@+id/like" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBottom="@+id/views_holder" 
      android:layout_toEndOf="@+id/btn_comment" 
      android:clickable="true"> 

      <CheckBox 
       android:id="@+id/cb_like" 
       android:layout_width="wrap_content" 
       android:layout_height="@dimen/pressable_size" 
       android:gravity="center_vertical" 
       android:button="@drawable/ic_heart_checkbox" 
       android:paddingStart="8dp" 
       android:paddingEnd="8dp" 
       android:clickable="false" 
       android:focusable="false" 
       android:focusableInTouchMode="false" 
       tools:text="324" 
       /> 

     </FrameLayout> 

내가 (이 경우 - FrameLayout이) - 래퍼 뷰 그룹 추가 한 FrameLayout이에 수신기를 클릭하고 설정 (ButterKnife 내 경우) 을 :

@OnClick(R.id.like) 
void onLikeClicked() { 
     cbLike.setChecked(!cbLike.isChecked()); 
     if (onNewsClickListener != null) { 
      onNewsClickListener.onLikeClicked(getAdapterPosition()); 
     } 
    } 

등 - FrameLayout, cbLike (CheckBox). 제대로 작동합니다. 내가

android:clickable="false" 
android:focusableInTouchMode="false" 
android:focusable="false 

가 그럼 난 내가 작동하지 않습니다 확인란

0

나는 심지어 같은 problema했다. 위의 코드를 찾으십시오.
관련 문제