Android ListFragment多个复选框在一行中

时间:2016-10-25 16:20:44

标签: android checkbox android-recyclerview android-arrayadapter android-listfragment

我面临一些问题,所以我可能会有人帮助我,因为我处于死路,并且不知道如何解决这个问题。所以我创建了一个应用程序,它使用List Fragment在一行中包含多个(正好是5个)复选框和1个文本视图。所以第一个问题是:我只想检查5个复选框中的一个(这意味着如果我先检查一个,那么其他(第二,第三,第四,第五个框)不应该被检查等等...)。而第二个也是最重要的问题是会有10-20行,所以当我向下/向上滚动时,一些行会被回收并且选中的复选框会被取消选中或随机检查,所以我应该如何存储数据,因此,在向上或向下滚动后,未选中或随机选中已选中的复选框。 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);
    }







}

ArrayAdapter:

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;
    }
}

QuestionClass:

 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>

2 个答案:

答案 0 :(得分:0)

这是一种如何让用户只选择一个选项的方法。更好的方法是实现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);


            }


        }
    });

答案 1 :(得分:0)

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

a-在Question Class中,添加以下内容:

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

b-在数据库适配器中,将STATUS添加到表项。

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

希望它有所帮助!