如何在android可扩展列表视图中允许单个子选择?

时间:2016-06-02 06:03:26

标签: android android-layout expandablelistview

我正在使用带有child的可扩展列表视图。我尝试在所有父级中设置单个子选择。请帮助我,我不知道该怎么做。

public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
        final ChildViewHolder childViewHolder;
        if(view==null) {
            childViewHolder=new ChildViewHolder();
            view=((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).
                    inflate(R.layout.product_listview_child_itemnew,null);
            childViewHolder.choice1=(TextView)view.findViewById(R.id.textView1);

            childViewHolder.choice1.setTextColor(Color.parseColor("#000000"));

            childViewHolder.checkBox1=(CheckBox)view.findViewById(R.id.checkBox1);
            //childViewHolder.checkBox1.setTag(getQuestion.get(i));
            view.setTag(childViewHolder);


        }
        else {
            childViewHolder=(ChildViewHolder)view.getTag();
        }
        childViewHolder.checkBox1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setquestion=getQuestion.get(i).getOptions().get(i1);
                Log.d("vikisimp",":"+setquestion);
            }
        });

        childViewHolder.choice1.setText(getQuestion.get(i).getOptions().get(i1));
        return view;
    }

1 个答案:

答案 0 :(得分:1)

please find the solution of single selection in expandable listview below:

ExpandableActivity.java

public class ExpandableActivity extends AppCompatActivity {

    private ExpandableListView expandableListView;
    ArrayList<String> groupList = new ArrayList<String>();
    ArrayList<ArrayList<Answer>> childList = new ArrayList<ArrayList<Answer>>();

    ExpandableListAdapter expandableListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expandable);

        expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);
        groupList.add("Question 1");
        groupList.add("Question 2");
        groupList.add("Question 3");
        groupList.add("Question 4");
        groupList.add("Question 5");

        ArrayList<Answer> answerList1 = new ArrayList<>();
        ArrayList<Answer> answerList2 = new ArrayList<>();
        ArrayList<Answer> answerList3 = new ArrayList<>();
        ArrayList<Answer> answerList4 = new ArrayList<>();
        ArrayList<Answer> answerList5 = new ArrayList<>();

        setData("Answer 1", 1, false, answerList1);
        setData("Answer 2 ", 2, false, answerList1);
        setData("Answer 3", 3, false, answerList1);
        setData("Answer 4", 4, false, answerList1);

        setData("Answer 1", 5, false, answerList2);
        setData("Answer 2 ",6, false, answerList2);
        setData("Answer 3", 7, false, answerList2);
        setData("Answer 4", 8, false, answerList2);

        setData("Answer 1", 9, false, answerList3);
        setData("Answer 2 ",10, false, answerList3);
        setData("Answer 3", 11, false, answerList3);
        setData("Answer 4", 12, false, answerList3);

        setData("Answer 1", 13, false, answerList4);
        setData("Answer 2 ", 14, false, answerList4);
        setData("Answer 3", 15, false, answerList4);
        setData("Answer 4", 16, false, answerList4);

        setData("Answer 1", 17, false, answerList5);
        setData("Answer 2 ", 18, false, answerList5);
        setData("Answer 3", 19, false, answerList5);
        setData("Answer 4", 20, false, answerList5);

        childList.add(answerList1);
        childList.add(answerList2);
        childList.add(answerList3);
        childList.add(answerList4);
        childList.add(answerList5);

        expandableListAdapter = new ExpandableListAdapter(this,groupList,childList);
        expandableListView.setAdapter(expandableListAdapter);


        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {


                for(int i =0;i<childList.get(groupPosition).size();i++)
                {
                    if(childPosition!=i)
                        childList.get(groupPosition).get(i).setState(false);
                }

                childList.get(groupPosition).get(childPosition).setState(!childList.get(groupPosition).get(childPosition).isState());


                expandableListAdapter.notifyDataSetChanged();
                return false;
            }
        });

    }

    public void setData(String text, int id, boolean state, ArrayList<Answer> answerList)
    {
        Answer answer = new Answer();
        answer.setAnswer(text);
        answer.setAnswerId(id);
        answer.setState(state);
        answerList.add(answer);
    }

}

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

    <ExpandableListView
        android:id="@+id/expandableListView"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        />
</LinearLayout>


ExpandableListAdapter.java
public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private ArrayList<String> groupItems;
    ArrayList<ArrayList<Answer>> childItems = new ArrayList<ArrayList<Answer>>();

    public ExpandableListAdapter(Context context, ArrayList<String> groupItems,
                                  ArrayList<ArrayList<Answer>> childItems) {
        this._context = context;
        this.groupItems = groupItems;
        this.childItems = childItems;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return childItems.get( groupPosition ).get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final Answer answer = (Answer)getChild( groupPosition, childPosition );

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.label_child, null);
        }

        TextView textViewChild = (TextView) convertView
                .findViewById(R.id.textViewChild);

        CheckBox checkBoxChild = (CheckBox)convertView.findViewById(R.id.checkBoxChild);
        textViewChild.setText(answer.getAnswer());
        checkBoxChild.setChecked(answer.isState());



        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childItems.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupItems.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return groupItems.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String groupTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.label_group, null);
        }

        TextView textViewGroup = (TextView) convertView
                .findViewById(R.id.textViewGroup);
        textViewGroup.setText(groupTitle);
        ExpandableListView mExpandableListView = (ExpandableListView) parent;
        mExpandableListView.expandGroup(groupPosition);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

labelgroup.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="8dp"
    android:background="@android:color/white">


    <TextView
        android:id="@+id/textViewGroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:textSize="16sp"
        android:textColor="@android:color/black" />

</LinearLayout>

label_child.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="8dp"
    android:background="@android:color/white">


    <TextView
        android:id="@+id/textViewChild"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:textSize="16sp"
        android:textColor="@android:color/holo_blue_dark"
        android:text="Example"/>

    <CheckBox
        android:id="@+id/checkBoxChild"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="false"/>

</LinearLayout>