使用ListVIEw时,我注意到在ListVIEw中设置一个复选框时,ListVIEw.OnItemClicked侦听器不起作用!现在,我正在尝试为ListVIEw实现两个不同的单击侦听器.第一个用于ListVIEw主体,第二个用于ListVIEw主体的checkBox. ListVIEw的主体是(现在)生成一个吐司,而checkBox是另一个(现在)生成.
我面临的问题是在引用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(); } } }); } }); }}
解决方法:
加上这个
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(); } }); }}
快照
总结以上是内存溢出为你收集整理的android-listView项目侦听器全部内容,希望文章能够帮你解决android-listView项目侦听器所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)