使用listView
时,我注意到,在listView中设置checkBox
时,listView.OnItemClicked
侦听器无效!目前,我正在尝试为on click listeners
实施两个不同的 listView
。第一个用于 listView body ,第二个用于 listView的body的checkBox 。 listView的主体是(现在)生成一个toast,checkBox是生成另一个toast(现在)。
我所面临的问题是在提及checkBox.onClick
时
逻辑仅实现到布局中的最后一个(或任何随机,但只有一个)复选框。
listView布局如下所示:
MainActivity.java :
private void registerClickCallBack() {
// TODO Auto-generated method stub
ListView list = (ListView)findViewById(R.id.listViewComplex);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position,
long id) {
// TODO Auto-generated method stub
Random clickedRandom = randomList.get(position);
String message = "You have clicked position "+position+" which is titled "+clickedRandom.getTitle();
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
final CheckBox check= (CheckBox)findViewById(R.id.checkBoxComplex);
check.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(check.isChecked()){
Toast.makeText(MainActivity.this, "Checked", Toast.LENGTH_LONG).show();
}
}
});
}
});
}
}
答案 0 :(得分:3)
添加此
android:focusable="false"
到xml中的复选框。
假设您想要点击复选框显示祝酒词。试试下面的
您应该使用视图持有者进行平滑滚动和演奏。
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
在onCreate
初始化您的listiew。从编辑之前的代码开始,我可以看到你两次初始化listview。没必要。
将您的活动更改为
public class MainActivity extends Activity {
private List<Random> randomList = new ArrayList<Random>();
ListView list;
ArrayAdapter<Random> adapter;
private SparseBooleanArray mCheckStates;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView)findViewById(R.id.listViewComplex);
adapter = new MyListAdapter();
populateRandomList();
populateListView();
registerClickCallBack();
}
private void populateRandomList() {
// TODO Auto-generated method stub
randomList.add(new Random("Title One", "Simple", "Status: OK", R.drawable.ic_launcher));
randomList.add(new Random("Title Two", "Complex", "Status: WORKING", R.drawable.ic_launcher));
randomList.add(new Random("Title Three", "Moderate", "Status: NOT OK", R.drawable.ic_launcher));
randomList.add(new Random("Title Four", "Simple", "Status: BAD", R.drawable.ic_launcher));
randomList.add(new Random("Title Five", "Moderate", "Status: NOT OK", R.drawable.ic_launcher));
randomList.add(new Random("Title Six", "Simple", "Status: WORKING",R.drawable.ic_launcher));
randomList.add(new Random("Title Seven", "Complex", "Status: EXCELLENT", R.drawable.ic_launcher));
randomList.add(new Random("Title Eight", "Complex", "Status: NBA", R.drawable.ic_launcher));
}
private void populateListView() {
list.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<Random> implements CompoundButton.OnCheckedChangeListener{
public MyListAdapter(){
super(MainActivity.this,R.layout.layout_complex,randomList);
mCheckStates = new SparseBooleanArray(randomList.size());
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return randomList.size();
}
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder vh;
if(convertView == null){
convertView = getLayoutInflater().inflate(R.layout.layout_complex,parent,false);
vh = new ViewHolder();
vh.imageView = (ImageView)convertView.findViewById(R.id.itemImage);
vh.titleText = (TextView)convertView.findViewById(R.id.itemTitle);
vh.typeText = (TextView)convertView.findViewById(R.id.itemType);
vh.statusText = (TextView)convertView.findViewById(R.id.itemStatus);
vh.cb = (CheckBox) convertView.findViewById(R.id.checkBoxComplex);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
Random currentItem = randomList.get(position);
vh.imageView.setImageResource(currentItem.getIconID());
vh.titleText.setText(currentItem.getTitle());
vh.typeText.setText(currentItem.getType());
vh.statusText.setText(currentItem.getStatus());
vh.cb.setTag(position);
vh.cb.setChecked(mCheckStates.get(position, false));
vh.cb.setOnCheckedChangeListener(this);
return convertView;
}
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
if(isChecked)
{
Toast.makeText(MainActivity.this, "Checked at "+(Integer) buttonView.getTag(), Toast.LENGTH_SHORT).show();
}
}
class ViewHolder
{
TextView titleText,typeText,statusText;
ImageView imageView;
CheckBox cb;
}
}
private void registerClickCallBack() {
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position,
long id) {
Random clickedRandom = randomList.get(position);
String message = "You have clicked position "+position+" which is titled "+clickedRandom.getTitle();
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}
});
}
}
快照
答案 1 :(得分:0)
您也可以使用
android:descendantFocusability="blocksDescendants"