<custom.xml>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/txt1"
android:textColor="#ffff00"
android:textSize="30px"
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"/>
<CheckBox
android:id="@+id/chk"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
</LinearLayout>
<main.xml>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/list1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<List_Test.java>
package exam.List_Test;
import java.util.*;
import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class List_Test extends Activity implements AdapterView.OnItemClickListener{
ArrayList<String> item;
ListView list;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list =(ListView)findViewById(R.id.list1);
item= new ArrayList<String>();
item.add("one");
item.add("two");
item.add("three");
item.add("four");
CustomAdapter adapter = new CustomAdapter(this,R.layout.custom,item);
// ArrayAdapter<String> adaper = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,item);
//list.setAdapter(adaper);
list.setAdapter(adapter);
list.setOnItemClickListener(this);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView text=(TextView)parent.getChildAt(position);
String value=text.getText().toString();
Toast.makeText(this,""+value,Toast.LENGTH_SHORT).show();
}
private class CustomAdapter extends ArrayAdapter {
private ArrayList<String> items;
public CustomAdapter(Context context, int textViewResourceId, ArrayList<String> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.custom, null);
}
String str = items.get(position);
if (str != null) {
final TextView tt = (TextView) v.findViewById(R.id.txt1);
v.setOnClickListener(new OnClickListener(){
public void onClick(View view){
Toast.makeText(con,""+tt.getText(), Toast.LENGTH_SHORT).show();
}
});
if (tt != null){
tt.setText(str);
}
}
return v;
}
}
}
이상하게 체크박스가 들어가니깐 onItemClickListener가 동작을 안해요
이거 해결방법 없나요 사이드에 올려보니 TextView에 클릭 이벤트를 준다고 하셔서
줘봤는데 되긴 되네요 클릭 이벤트가 근데 클릭 이벤트 말고는 답이없나요?
CheckedTextView를 보면 체크박스는 그냥 그림입니다.
우리가 생각하는 체크박스 컨트롤 개념이 아닙니다.
그럼 체크박스 컨트롤은 어디에? 라고 물으신다면
CheckedTextView 전체가 체크박스라고 대답할수 있습니다.
만약 텍스트랑 체크박스랑 이벤트를 분리하고 싶으시다면...
1. 텍스트뷰와 이미지뷰(체크박스가 그려질...)를 포함한 Layout을 만든다.
2. 1에서 만든 레이아웃은 단순한 레이아웃이 아니고 Checkable을 구현한 CustomLayout임....
3. Checkable을 구현하면서 자신의 Check상태에 따라 이미지 뷰의 그림을 바꾸는 소스를 넣는다.
4. 텍스트뷰에 텍스트부분 눌렀을때 하고 싶은 이벤트를 추가한다.
이렇게 하면 체크는 체크대로 제대로 동작하고, 텍스트를 눌렀을때 이벤트는 또 따로 동작하게 됩니다.
체크버튼에 focusable을 false로 줘보세요..
그랬던 걸로 기업합니다..
List가 Focus로 컨트롤 됐던걸로...
Focusable랑 clickable이랑 checkable 을 어떻게 조정하니깐 해결 되었는데..
정확이 어떻게 셋팅해줬는지 기억이 잘 안나네요;;