在可扩展列表视图中,通过单击(子级)CheckedTextview

时间:2019-02-02 09:04:49

标签: android android-studio

作为初学者级android开发人员,我需要帮助。我创建了一个可扩展的
带有(父项)textview和(Child)已检查textview的android studio中的listview全部
工作正常,但我想在其中添加更多内容。当我单击子列表项时
(至少有一个孩子),它必须更改相应的(父)textview
背景颜色
。这是我的代码和快照。请帮帮我。

  

activity_main.xml

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


    <ExpandableListView

        android:layout_weight="9"
        android:id="@+id/exp_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"

        android:divider="#CECECE"


        />

    <Button
        android:visibility="gone"
        android:layout_weight="1"
        android:layout_gravity="bottom"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/next"
        android:gravity="center"
        android:text="next"
        android:textColor="#FFF"
        android:background="@color/colorPrimary"
        />
</LinearLayout>
  

parent_layout.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="wrap_content"
    android:descendantFocusability="beforeDescendants"
    >

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="8dp"

        android:background="#F3F3F3"/>

    <LinearLayout
        android:gravity="center"

        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal"
        android:background="#FFF"
        android:baselineAligned="false"
        android:layout_gravity="center"
        >
         <TextView
             android:elevation="5dp"
             android:textColor="@color/colorBlack"
             android:textSize="14sp"
             android:gravity="left|center"
             android:paddingStart="45dp"
             android:id="@+id/heading_item"
             android:text="Abc"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:paddingLeft="35dp" />





    </LinearLayout>



</LinearLayout>
  

row_layout.xml

<?xml version="1.0" encoding="utf-8"?>

    <CheckedTextView    xmlns:android="http://schemas.android.com/apk/res/android"
                        xmlns:app="http://schemas.android.com/apk/res-auto"

        android:background="#FFFFFF"
        android:backgroundTint="#F3F3F3"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:textSize="13sp"
        android:gravity="center"
        android:text="abc"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        android:textColor="#4B4B4B"
        android:paddingLeft="50dp"
        android:paddingRight="10dp"
        android:id="@+id/child_item"
        android:layout_gravity="center_vertical"
        android:checked="false"

        android:checkMark="?android:attr/listChoiceIndicatorMultiple"

       />
  

MainActivity.class

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {

    Button next;

  static   List<String> course=new ArrayList<>();

   static HashMap<String, List<String>>  subjects=new  HashMap<String, List<String>> ();

    List<String> selectedsubjects=new ArrayList<>();

    ExpandableListView Exp_list;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        next=findViewById(R.id.next);
        Exp_list=findViewById(R.id.exp_list);
        preparedatadata();

        final String status[][]=new String[subjects.size()][];
        String status1[]=new String[course.size()];

        for(int i=0;i<course.size();i++){

            status[i]=new String[5];
            status1[i]=ConstantManager.CHECK_BOX_CHECKED_FALSE;
            for(int j=0;j<subjects.get(course.get(i)).size();j++){

                status[i][j]=ConstantManager.CHECK_BOX_CHECKED_FALSE;
            }

        }


        Exp_list.setAdapter(new CustomExpendibleViewAdaptor(getApplicationContext(),course,subjects,false,status,status1));


       Exp_list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Log.e("groupPosition: "+groupPosition,"childPosition: "+childPosition);

                if(status[groupPosition][childPosition].equals(ConstantManager.CHECK_BOX_CHECKED_TRUE)){
                    Log.e("YES  :groupPosition: "+groupPosition,"childPosition: "+childPosition);

                    ((CheckedTextView)v.findViewById(R.id.child_item)).setChecked(false);
                    status[groupPosition][childPosition]=ConstantManager.CHECK_BOX_CHECKED_FALSE;
                }
                else {
                    Log.e("NO  :groupPosition: "+groupPosition,"childPosition: "+childPosition);
                    status[groupPosition][childPosition]=ConstantManager.CHECK_BOX_CHECKED_TRUE;
                    ((CheckedTextView)v.findViewById(R.id.child_item)).setChecked(true);
                }


                String selectedItem=((TextView)v).getText().toString();


              //  Log.e("groupPosition: "+groupPosition,"selectedItem: "+selectedItem);
                if(selectedsubjects.contains(selectedItem)){

                    selectedsubjects.remove(selectedItem); //add item for check
                }
                else {

                    selectedsubjects.add(selectedItem);  //remove item  for uncheck
                }

                if(selectedsubjects.isEmpty()){

                    next.setVisibility(View.GONE);
                }
                else {

                    next.setVisibility(View.VISIBLE);
                }


                return true;
            }
        });

        Exp_list.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {



                Log.e("groupPosition: "+groupPosition,"id: "+id);
                return false;
            }
        });

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String list="";
                for(String l:selectedsubjects){

                    list+=l+", ";
                }
                Toast.makeText(getApplicationContext(),list+"",Toast.LENGTH_LONG).show();
            }
        });
    }


    void preparedatadata(){

        course.add("Class 1-5");
        course.add("B.Tech");
        course.add("B.Com");
        course.add("MCA");

        course.add("ISC");

        course.add("BSC");

        course.add("PHD");

        course.add("Fashion designing");

        course.add("Cricket");

        course.add("ISRO");
        course.add("Uma-clases");

        List<String> l1=new ArrayList<>();
        List<String> l2=new ArrayList<>();
        List<String> l3=new ArrayList<>();
        List<String> l4=new ArrayList<>();
        List<String> l5=new ArrayList<>();
        List<String> l6=new ArrayList<>();
        List<String> l7=new ArrayList<>();
        List<String> l8=new ArrayList<>();
        List<String> l9=new ArrayList<>();
        List<String> l10=new ArrayList<>();
        List<String> l11=new ArrayList<>();



        l1.add(course.get(0)+" -  "+"English");
        l1.add(course.get(0)+" -  "+"Math");
        l1.add(course.get(0)+" -  "+"Science");


        l2.add(course.get(1)+" -  "+"Java");
        l2.add(course.get(1)+" -  "+"Python");
        l2.add(course.get(1)+" -  "+"Android");

        l3.add(course.get(2)+" -  "+"Accountancy");
        l3.add(course.get(2)+" -  "+"Economics");
        l3.add(course.get(2)+" -  "+"Business Study");

        l4.add(course.get(3)+" -  "+"Computer 1");
        l4.add(course.get(3)+" -  "+"Computer 2");
        l4.add(course.get(3)+" -  "+"Computer 3");

        l5.add(course.get(4)+" -  "+"Math");
        l5.add(course.get(4)+" -  "+"Physics");
        l5.add(course.get(4)+" -  "+"Chemistry");

        l6.add(course.get(5)+" -  "+"math honors");
        l6.add(course.get(5)+" -  "+"English 1");
        l6.add(course.get(5)+" -  "+"English 2");

        l7.add(course.get(6)+" -  "+"Phd paper 1");
        l7.add(course.get(6)+" -  "+"Phd paper 2");
        l7.add(course.get(6)+" -  "+"Phd paper 3");


        l8.add(course.get(7)+" -  "+"Dress desigining");
        l8.add(course.get(7)+" -  "+"Interior Decorating");





        l9.add(course.get(8)+" -  "+"Ball Catch ");
        l9.add(course.get(8)+" -  "+"Batting practice");
        l9.add(course.get(8)+" -  "+"Bowling Practice");

        l10.add(course.get(9)+" -  "+"Making Astronuot");
        l10.add(course.get(9)+" -  "+"Making Scientist");

        l11.add(course.get(10)+" -  "+"Coming Course 1");
        l11.add(course.get(10)+" -  "+"Coming Course 2");
        l11.add(course.get(10)+" -  "+"Coming Course 3");



        subjects.put(course.get(0),l1);
        subjects.put(course.get(1),l2);
        subjects.put(course.get(2),l3);
        subjects.put(course.get(3),l4);
        subjects.put(course.get(4),l5);
        subjects.put(course.get(5),l6);
        subjects.put(course.get(6),l7);
        subjects.put(course.get(7),l8);
        subjects.put(course.get(8),l9);
        subjects.put(course.get(9),l10);
        subjects.put(course.get(10),l11);






    }




}
  

CustomExpendibleViewAdaptor.class

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckedTextView;
import android.widget.TextView;

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

public class CustomExpendibleViewAdaptor extends BaseExpandableListAdapter {


    HashMap<String, List<String>>  subjects;
    List<String> course=new ArrayList<>();
    private boolean isFromMyCategoriesFragment;
    private Context context;
    String status[][];
    String status1[];
    LayoutInflater inflater;

    CustomExpendibleViewAdaptor(Context context , List<String> course, HashMap<String, List<String>>  subjects, boolean isFromMyCategoriesFragment, String status[][], String status1[]){

        this.context=context;
        this.course=course;
        this.subjects=subjects;
        this.isFromMyCategoriesFragment=isFromMyCategoriesFragment;
        this.status=status;
        this.status1=status1;

    }

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

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

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return subjects.get( course.get(groupPosition)).get(childPosition);
    }

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

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

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

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {


        Log.e("Adaptor: ","getGroupView()");



        if (convertView==null) {

             inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


                convertView=inflater.inflate(R.layout.parent_layout,(ViewGroup) null);

        }

            convertView.setTag(getGroup(groupPosition).toString());

            View view=convertView.findViewById(R.id.view);
       //     view.setMinimumHeight(20);
            TextView grouptextchecked=convertView.findViewById(R.id.heading_item);
        //    grouptextchecked.setTypeface(null, Typeface.BOLD);
          //  grouptextchecked.setHeight(100);
            grouptextchecked.setText(getGroup(groupPosition).toString());
            grouptextchecked.setTextColor(Color.parseColor("#000000"));




        return convertView;
    }



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

        Log.e("Adaptor: ","getChildView()");



        final ViewHolderChild viewHolderChild;

        String child=(String) this.getChild(groupPosition,childPosition);

        if (convertView == null) {
            Log.e("ExpandableListAdapter -","getChildView()-null");
            inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=inflater.inflate(R.layout.row_layout,null);

            viewHolderChild = new ViewHolderChild();

            viewHolderChild.checkedTextView = convertView.findViewById(R.id.child_item);

            convertView.setTag(viewHolderChild);

        }
        else {
            Log.e("ExpandableListAdapter -","getChildView()-not null");
            viewHolderChild = (ViewHolderChild) convertView.getTag();
        }

        if (status[groupPosition][childPosition].equalsIgnoreCase(ConstantManager.CHECK_BOX_CHECKED_TRUE)) {
            Log.e("ExpandableListAdapter -","getChildView()-if");
            viewHolderChild.checkedTextView.setChecked(true);
            notifyDataSetChanged();
        } else {
            Log.e("ExpandableListAdapter -","getChildView()-else");
            viewHolderChild.checkedTextView.setChecked(false);
            notifyDataSetChanged();
        }


       viewHolderChild.checkedTextView.setText(child);

                if (viewHolderChild.checkedTextView.isChecked()) {
                    Log.e("ExpandableListAdapter -","viewHolderChild.checkedTextView.isChecked(");

                    status[groupPosition][childPosition]=ConstantManager.CHECK_BOX_CHECKED_TRUE;
                    notifyDataSetChanged();
                } else {
                    Log.e("ExpandableListAdapter -","not .isChecked(");

                    status[groupPosition][childPosition]=ConstantManager.CHECK_BOX_CHECKED_FALSE;
                    notifyDataSetChanged();
                }


        return convertView;
    }

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

    private class ViewHolderChild {
        CheckedTextView checkedTextView;

    }
}
  

ConstantManager.class

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

public class ConstantManager {


    public static final String CHECK_BOX_CHECKED_TRUE = "YES";
    public static final String CHECK_BOX_CHECKED_FALSE = "NO";


}
  

现在快点

输出1:-

enter image description here

输出2:-

enter image description here

0 个答案:

没有答案