Android ListFragment 单行多个复选框

Android ListFragment multiple checkbox in a single row

我遇到了一些问题,所以我想也许有人可以帮助我,因为我处于死胡同,不知道如何解决这个问题。所以我正在创建一个应用程序,它在一行中使用带有多个(恰好 5 个)复选框和 1 个文本视图的列表片段。所以第一个问题是:我只想检查 5 个复选框中的一个(这意味着如果我检查第一个,那么其他(第二个、第三个、第四个、第五个框)不应该被检查等等......) .第二个也是最重要的问题是会有 10-20 行,所以当我滚动 down/up 一些行被回收并且选中的框变得未选中或随机选中,所以我应该如何存储数据,这样被选中的复选框在向上或向下滚动后不会被取消选中或随机选中。 onListItemClick 也不起作用:(

ListFragment:

package com.android4dev.navigationview;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import static android.support.design.widget.Snackbar.*;


public class SymptomsSelectionFragment extends ListFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle  savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_symptoms_selection, container, false);
        return root;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
                "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                "Linux", "OS/2","shitPhone","testPhone", "boomPhone", "meskaPhone" };
        ArrayAdapter<Question> adapter = new InteractiveArrayAdapter(getActivity(),
                getQuestion());
        FloatingActionButton fab = (FloatingActionButton) getActivity().findViewById(R.id.fab);
        fab.hide();
        setListAdapter(adapter);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // TODO implement some logic
        super.onListItemClick(l, v, position, id);
        String item = (String) getListAdapter().getItem(position);
        Snackbar.make(getListView(), item+" selected", Snackbar.LENGTH_LONG).show();
        Toast.makeText(getActivity(),position+" <--- Nr.",Toast.LENGTH_SHORT).show();
    }
    private List<Question> getQuestion() {
        List<Question> list = new ArrayList<Question>();
        list.add(get("Linux"));
        list.add(get("Windows7"));
        list.add(get("Suse"));
        list.add(get("Eclipse"));
        list.add(get("Ubuntu"));
        list.add(get("Solaris"));
        list.add(get("Android"));
        list.add(get("iPhone"));
        // Initially select one of the items
        //list.get(1).setSelected(true);
        return list;
    }

    private Question get(String s) {
        return new Question(s);
    }







}

数组适配器:

import java.util.List;
package com.android4dev.navigationview;
import android.app.Activity;
import android.media.Image;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.TextView;

public class InteractiveArrayAdapter extends ArrayAdapter<Question> {

    private final List<Question> list;
    private final Activity context;

    public InteractiveArrayAdapter(Activity context, List<Question> list) {
        super(context, R.layout.row_layout_symptoms_selection, list);
        this.context = context;
        this.list = list;
    }

    static class ViewHolder {
        protected TextView text;
        protected CheckBox checkbox1;
        protected CheckBox checkbox2;
        protected CheckBox checkbox3;
        protected CheckBox checkbox4;
        protected CheckBox checkbox5;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;

        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.row_layout_symptoms_selection, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.label_symptom);
            viewHolder.checkbox1 = (CheckBox) view.findViewById(R.id.button1);
            viewHolder.checkbox2 = (CheckBox) view.findViewById(R.id.button2);
            viewHolder.checkbox3 = (CheckBox) view.findViewById(R.id.button3);
            viewHolder.checkbox4 = (CheckBox) view.findViewById(R.id.button4);
            viewHolder.checkbox5 = (CheckBox) view.findViewById(R.id.button5);
            viewHolder.checkbox1
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                                     boolean isChecked) {
                            Question element = (Question) viewHolder.checkbox1
                                    .getTag();
                            element.setSelected(buttonView.isChecked());

                        }
                    });
            viewHolder.checkbox2
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                                     boolean isChecked) {
                            Question element2 = (Question) viewHolder.checkbox2
                                    .getTag();
                            element2.setSelected(buttonView.isChecked());

                        }
                    });
            viewHolder.checkbox3
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                                     boolean isChecked) {
                            Question element3 = (Question) viewHolder.checkbox3
                                    .getTag();
                            element3.setSelected(buttonView.isChecked());

                        }
                    });
            viewHolder.checkbox4
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                                     boolean isChecked) {
                            Question element4 = (Question) viewHolder.checkbox4
                                    .getTag();
                            element4.setSelected(buttonView.isChecked());

                        }
                    });
            viewHolder.checkbox5
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                                     boolean isChecked) {
                            Question element5 = (Question) viewHolder.checkbox5
                                    .getTag();
                            element5.setSelected(buttonView.isChecked());

                        }
                    });
            view.setTag(viewHolder);
            viewHolder.checkbox1.setTag(list.get(position));
            viewHolder.checkbox2.setTag(list.get(position));
            viewHolder.checkbox3.setTag(list.get(position));
            viewHolder.checkbox4.setTag(list.get(position));
            viewHolder.checkbox5.setTag(list.get(position));
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox1.setTag(list.get(position));
            ((ViewHolder) view.getTag()).checkbox2.setTag(list.get(position));
            ((ViewHolder) view.getTag()).checkbox3.setTag(list.get(position));
            ((ViewHolder) view.getTag()).checkbox4.setTag(list.get(position));
            ((ViewHolder) view.getTag()).checkbox5.setTag(list.get(position));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(list.get(position).getName());
        holder.checkbox1.setChecked(list.get(position).isSelected());
        holder.checkbox2.setChecked(list.get(position).isSelected());
        holder.checkbox3.setChecked(list.get(position).isSelected());
        holder.checkbox4.setChecked(list.get(position).isSelected());
        holder.checkbox5.setChecked(list.get(position).isSelected());
        return view;
    }
}

问题类:

 package com.android4dev.navigationview;

/**
 * Created by Home-PC on 10/19/2016.
 */

public class Question {
    private String name;
    private boolean selected;

    public Question(String name) {
        this.name = name;
        selected = false;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }
}

fragment_symptoms_selection.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="58dp"
    >


    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:layout_weight="1"
        android:drawSelectorOnTop="false" />

    <TextView
        android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:text="No data" />
</LinearLayout>

row_layout_symptoms_selection.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:showDividers="middle"
    android:divider="?android:attr/listDivider"
    tools:context=".SymptomsActivity"
    >

    <TextView
        android:id="@+id/label_symptom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/text"
        android:textSize="19dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="4dp"
        android:layout_marginTop="4dp" />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        >

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:button="@drawable/checkbox_drawable"
            android:id="@+id/button1"
            android:layout_weight="1"
            android:background="@color/CheckBox1"
            />
        <CheckBox
            android:button="@drawable/checkbox_drawable"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:id="@+id/button2"
            android:layout_weight="1"
            android:background="@color/CheckBox2"/>
        <CheckBox
            android:button="@drawable/checkbox_drawable"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:id="@+id/button3"
            android:layout_weight="1"
            android:background="@color/CheckBox3"/>
        <CheckBox
            android:button="@drawable/checkbox_drawable"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:id="@+id/button4"
            android:layout_weight="1"
            android:background="@color/CheckBox4"/>
        <CheckBox
            android:button="@drawable/checkbox_drawable"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:id="@+id/button5"
            android:layout_weight="1"
            android:background="@color/CheckBox5"/>
    </LinearLayout>
</LinearLayout>

这是一种让用户只能选择一个选项的方法。更好的方法是实现 onCheckChangeListener 并做同样的事情,这样你就不必重新使用相同的方法 :) 要存储用户选择的值,你应该使用 "extends Application" 方法。

    final CheckBox cbOne = (CheckBox) findViewById(R.id.cbOne);
    final CheckBox cbTwo = (CheckBox) findViewById(R.id.cbTwo);
    final CheckBox cbThree = (CheckBox) findViewById(R.id.cbThree);

    cbOne.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if (isChecked) {

                cbTwo.setClickable(false);
                cbThree.setClickable(false);

                cbTwo.setChecked(false);
                cbThree.setChecked(false);


            } else {

                cbTwo.setClickable(true);
                cbThree.setClickable(true);

            }

        }
    });

    cbTwo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


            if (isChecked) {

                cbOne.setClickable(false);
                cbThree.setClickable(false);

                cbOne.setChecked(false);
                cbThree.setChecked(false);


            } else {

                cbOne.setClickable(true);
                cbThree.setClickable(true);


            }


        }
    });


    cbThree.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


            if (isChecked) {

                cbTwo.setClickable(false);
                cbOne.setClickable(false);

                cbTwo.setChecked(false);
                cbOne.setChecked(false);


            } else {

                cbTwo.setClickable(true);
                cbOne.setClickable(true);


            }


        }
    });

首先,如果您一次只需要选择一个选项,请考虑使用 RadioButton 实现。 其次,您是否将数据存储在 sql 数据库中?如果您已经在使用 sql 数据库,这是存储复选框状态的方法:

a- 在 Question Class 中添加:

int status   
public int getStatus() {
        return status;
   }
public void setStatus(int status) {
        this.status = status;
    }

b- 在您的数据库适配器中,将 STATUS 添加到您的 table 项目。

c- 在您的 getView 方法中像这样处理选中的项目:

holder.check.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        CheckBox cb = (CheckBox) v;
                        Item changeItem = (Item) cb.getTag();
                        changeItem.setStatus(cb.isChecked() ? 1 : 0);
                        mDbHelper.updateItem(changeItem);
                             }
                     });

请参阅 this 了解如何在 sql 数据库中存储数据。 如果你不使用数据库,也许你需要使用 SharedPreferences.

希望对您有所帮助!