我需要一个带有自定义设计切换按钮的列表视图,一次只能选择一个

时间:2018-02-07 16:10:36

标签: android listview togglebutton

下面的代码代码可以选择多项选择。我只需要选择一个带有自定义切换按钮的设计。

我需要选择一个选项并获取所选切换按钮的ID。我在互联网上搜索了自定义列表视图,单选切换按钮,但我没有找到任何有用的东西。

我只需要动态添加5个切换按钮 - 当我选择一个时,应该取消选择其他按钮。

public class MainActivity extends Activity {

    int startminute;
    int endminute;

    Date date;
    ToggleButton togg;
    ListView lv;
    String[] days = {"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY",
            "FRIDAY", "SATURDAY"};
    boolean[] onOff = new boolean[]{false, false, false, false, false, false,
            false};

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState != null) {
            onOff = savedInstanceState.getBooleanArray("status");
        }
        lv = (ListView) findViewById(R.id.listView1);
        lv.setAdapter(new MyAdapter());
        lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1,
                                    int position, long arg3) {



            }
        });

    }

    public class MyAdapter extends BaseAdapter {

        public int getCount() {
            return days.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemViewType(int position) {

            return position;
        }

        @Override
        public int getViewTypeCount() {
            return days.length;
        }

        public View getView(final int position, View convertView,
                            ViewGroup parent) {
            View v = null;
            TextView arryText;

            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.list_item, null);
            }
            arryText = (TextView) v.findViewById(R.id.inflateText);
            togg = (ToggleButton) v.findViewById(R.id.toggleButton1);
            v.setTag(new ViewHolder(arryText, togg));

            togg.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {

                    if (onOff[position]) {
                        togg.setChecked(false);
                        onOff[position] = false;

                        Toast.makeText(MainActivity.this, "is off",
                                Toast.LENGTH_SHORT).show();

                    } else {

                        onOff[position] = true;
                        togg.setChecked(true);
                        Toast.makeText(MainActivity.this, "is on",
                                Toast.LENGTH_SHORT).show();

                    }

                }
            });

            arryText.setText(days[position]);
            togg.setChecked(onOff[position]);



            return v;
        }

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBooleanArray("status", onOff);
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用一个指示所选项目位置的整数,而不是使用布尔数组。

在toggleButton中加载值时,它将是这样的:

togg.setChecked(selectedDay == position);

项目点击监听器:

public void onClick(View v) {
    //Store selected position
    selectedDay = position;

    //Reload all items
    notifyDataSetChanged();
}