单击子视图时更改父视图的文本

时间:2019-04-01 06:45:15

标签: android expandablelistview

无论何时选择或取消选择子视图,我都想更改其父视图的文本。

这是我的适配器的代码:

package com.tablefortwo.All_Modules.ProfileSection.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.tablefortwo.All_Modules.ProfileSection.Model.Interest_Parent;
import com.tablefortwo.R;

import java.util.ArrayList;

import androidx.constraintlayout.widget.ConstraintLayout;


public class Adapter_Interest_Hobbies extends BaseExpandableListAdapter {

    ExpandableListView l_view;
    private Context context;
    private ArrayList<Interest_Parent> groups;

    public Adapter_Interest_Hobbies(Context context, ArrayList<Interest_Parent> groups, ExpandableListView l_view) {
        this.context = context;
        this.groups = groups;
        this.l_view = l_view;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        ArrayList<Interest_Parent.CategoryChild> chList = groups.get(groupPosition).getCategories();
        return chList.get(childPosition);
    }

    @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) {

        Interest_Parent.CategoryChild child = (Interest_Parent.CategoryChild) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.listitems_hobbieschild, null);
        }

        TextView childitemTv = convertView.findViewById(R.id.childitemTv);
        ConstraintLayout main = convertView.findViewById(R.id.main);
        ImageView check1 = convertView.findViewById(R.id.circle_check);
        childitemTv.setText(child.getTitle());
        check1.setTag(childPosition);
        main.setTag(childPosition);

        check1.setBackgroundResource(groups.get(groupPosition).getCategories().get(childPosition).getIsSelected() == 1
                ? R.drawable.radio_on : R.drawable.radio_off);

        check1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setupChecks(check1, groupPosition, childPosition);
            }
        });

        return convertView;
    }

    private void setupChecks(ImageView checks, int groupPosition, int childPosition) {
        Interest_Parent.CategoryChild child2 = groups.get(groupPosition).getCategories().get(childPosition);
        Interest_Parent parent2 = groups.get(groupPosition);

        if (child2.getIsSelected() == 1) {
            child2.setIsSelected(0);
            for (int j = 0; j < groups.get(groupPosition).getCategories().size(); j++) {
                if (groups.get(groupPosition).getCategories().get(j).getIsSelected() == 1)
                    break;
                else
                    parent2.setParent_selected(false);
            }
            parent2.setCount(parent2.getCount() > 0 ? parent2.getCount() - 1 : 0);
            checks.setBackgroundResource(R.drawable.radio_off);
        } else {
            child2.setIsSelected(1);
            parent2.setParent_selected(true);
            parent2.setCount(parent2.getCount() + 1);
            checks.setBackgroundResource(R.drawable.radio_on);
        }
        groups.set(groupPosition, parent2);
        groups.get(groupPosition).getCategories().set(childPosition, child2);
        //    count.setText(parent2.getCount() + " Selected");
    }


    @Override
    public int getChildrenCount(int groupPosition) {
        ArrayList<Interest_Parent.CategoryChild> chList = groups.get(groupPosition).getCategories();
        return chList.size();
    }

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

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

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


    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        Interest_Parent group = (Interest_Parent) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = inf.inflate(R.layout.listitems_hobbiesparent, null);
        }
        ExpandableListView eLV = (ExpandableListView) parent;
       // eLV.expandGroup(groupPosition);
        eLV.setTag(groupPosition);
        TextView nameTv = convertView.findViewById(R.id.nameTv);
        TextView count = convertView.findViewById(R.id.count);
        count.setTag(groupPosition);

        nameTv.setText(group.getTitle());
        count.setText(groups.get(groupPosition).getCount() + " Selected");

        nameTv.setCompoundDrawablesWithIntrinsicBounds(isExpanded ? R.drawable.small_drop_up : R.drawable.small_drop_down, 0, 0, 0);

        eLV.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                //count.setText(groups.get(groupPosition).getCount() + " Selected");
                Toast.makeText(context, "" + groupPosition, Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        return convertView;
    }


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

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

这是子视图的XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/grey"
    android:id="@+id/main"
    android:orientation="vertical"
    android:padding="5dp">

    <ImageView
        android:id="@+id/circle_check"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:background="@drawable/background_circle_empty"
        android:padding="5dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/childitemTv"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:drawablePadding="5dp"
        android:fontFamily="@font/roman_style"
        android:gravity="center_vertical"
        android:padding="5dp"
        android:text="@string/sports"
        android:textColor="@color/grey_txt"
        android:textSize="@dimen/text_14"
        android:textStyle="normal"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toRightOf="@+id/circle_check"
        app:layout_constraintRight_toLeftOf="@+id/count"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是我的父视图的XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:padding="5dp"
    android:background="@drawable/background_oval"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/nameTv"
        android:layout_width="0dp"
        app:layout_constraintHorizontal_weight="1"
        android:layout_height="wrap_content"
        android:padding="10dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:drawableLeft="@drawable/small_drop_down"
        android:drawablePadding="5dp"
        android:fontFamily="@font/roman_style"
        android:gravity="center_vertical"
        android:text="@string/sports"
        android:textColor="@color/grey_txt"
        android:textSize="@dimen/text_14"
        app:layout_constraintRight_toLeftOf="@+id/count"
        android:textStyle="normal" />


    <TextView
        android:id="@+id/count"
        android:layout_width="0dp"
        android:padding="10dp"
        app:layout_constraintHorizontal_weight="0.5"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        android:drawablePadding="5dp"
        android:fontFamily="@font/roman_style"
        android:gravity="right"
        android:text=""
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/nameTv"
        android:textColor="@color/colorPrimary"
        android:textSize="@dimen/text_14"
        android:textStyle="normal" />


</androidx.constraintlayout.widget.ConstraintLayout>

当我从子视图中选择或取消选择某项时,我想更改ID计数为TextView的文本。 当我单击 0th 位置父级的子级时, 1st 位置父级的文本会更改。

1 个答案:

答案 0 :(得分:1)

尝试一下:

public class Adapter_Interest_Hobbies extends BaseExpandableListAdapter {

private Context context;
private ArrayList<Interest_Parent> groups;

public Adapter_Interest_Hobbies(Context context, ArrayList<Interest_Parent> groups) {
    this.context = context;
    this.groups = groups;
}

@Override
public Interest_Parent.CategoryChild getChild(int groupPosition, int childPosition) {
    return groups.get(groupPosition).getCategories().get(childPosition);
}

@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) {

    Interest_Parent.CategoryChild child = getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.listitems_hobbieschild, null);
    }

    TextView childitemTv = convertView.findViewById(R.id.childitemTv);
    ConstraintLayout main = convertView.findViewById(R.id.main);
    ImageView check1 = convertView.findViewById(R.id.circle_check);
    childitemTv.setText(child.getTitle());
    check1.setTag(childPosition);
    main.setTag(childPosition);

    check1.setBackgroundResource(child.getIsSelected() == 1
            ? R.drawable.radio_on : R.drawable.radio_off);

    check1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setupChecks(groupPosition, childPosition);
        }
    });

    return convertView;
}

private void setupChecks(int groupPosition, int childPosition) {
    Interest_Parent.CategoryChild child2 = getChild(groupPosition, childPosition);
    Interest_Parent parent2 = getGroup(groupPosition);

    if (child2.getIsSelected() == 1) {
        child2.setIsSelected(0);
        parent2.setCount(parent2.getCount() - 1);
        if(parent2.getCount() == 0) parent2.setParent_selected(false);
    } else {
        child2.setIsSelected(1);
        parent2.setCount(parent2.getCount() + 1);
        parent2.setParent_selected(true);
    }
    notifyDataSetChanged();
}

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

@Override
public Interest_Parent getGroup(int groupPosition) {
    return groups.get(groupPosition);
}

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

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

@Override
public View getGroupView(final int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    Interest_Parent group = getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inf.inflate(R.layout.listitems_hobbiesparent, null);
    }
    ExpandableListView eLV = (ExpandableListView) parent;
    eLV.setTag(groupPosition);
    TextView nameTv = convertView.findViewById(R.id.nameTv);
    TextView count = convertView.findViewById(R.id.count);
    count.setTag(groupPosition);

    nameTv.setText(group.getTitle());
    count.setText(group.getCount() + " Selected");
    nameTv.setBackgroundResource(group.getParent_selected() == true
            ? R.drawable.radio_on : R.drawable.radio_off);

    nameTv.setCompoundDrawablesWithIntrinsicBounds(isExpanded ? R.drawable.small_drop_up : R.drawable.small_drop_down, 0, 0, 0);
    return convertView;
}

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

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

希望有帮助!