4

使用 a 时listView,我注意到,checkBox在 listView 中设置 a 时,listView.OnItemClicked侦听器不起作用!目前,我正在尝试 on click listenerslistView. 第一个用于listView 主体,第二个用于listView 主体的 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();
                        }
                    }
                });
            }
        });
    }



}
4

2 回答 2

3

添加这个

  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();

            }
        });
    }
}

快照

在此处输入图像描述

于 2013-08-18T09:22:46.947 回答
0

你也可以使用

android:descendantFocusability="blocksDescendants"

如本答案所述:https ://stackoverflow.com/a/13415566/2812681

于 2014-04-08T15:09:05.213 回答