添加这个
android:focusable="false"
到 xml 中的复选框。
假设您想在单击复选框时显示祝酒词。试试下面
您应该使用视图支架来实现平滑滚动和性能。
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
在 中初始化您的列表onCreate
。从您之前编辑之前的代码中,我可以看到您两次初始化列表视图。没必要。
将您的活动更改为
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();
}
});
}
}
快照