ExpandableListView изменяет свое дочернее значение при расширении / сворачивании своей группы - PullRequest
0 голосов
/ 14 октября 2018

У меня есть этот расширяемый просмотр списка с переключателем и поисковой панелью и некоторым текстовым представлением в дочернем элементе каждой группы, все в порядке с этим расширяемым просмотром списка, за исключением одной проблемы, то есть всякий раз, когда я устанавливаю значение любого переключателя или поисковой панели в любой группе, происходит сброс послея сверну эту группу или разверну другую группу.

Я погуглил по этим вопросам, но не нашел решения

ExpandableListAdapter.java

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

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

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<String> listGroup;
    private HashMap<String, List<CustomChild>> listChild;

    public ExpandableListAdapter(Context context, List<String> listGroup, HashMap<String, List<CustomChild>> listChild) {
        this.context = context;
        this.listGroup = listGroup;
        this.listChild = listChild;
    }

    public CustomChild getChild(int groupPosition, int childPosition){
        return this.listChild.get(this.listGroup.get(groupPosition)).get(childPosition);
    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        return this.listChild.get(this.listGroup.get(groupPosition)).size();
    }

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

    /*@Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }*/

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    @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(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null){
            LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService
                    (Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.exp_group_data, null);
        }

        TextView textView = (TextView) convertView.findViewById(R.id.group_tv);
        textView.setText(headerTitle);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        CustomChild child = getChild(groupPosition, childPosition);
        final ViewHolder holder;
        SharedPreferences sharedPreferences = this.context.getSharedPreferences("data", Context
                .MODE_PRIVATE);
        final SharedPreferences.Editor editor = sharedPreferences.edit();

        View row = convertView;
        if (row == null){
            LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context
                    .LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.exp_child_data, parent, false);

            holder = new ViewHolder();
            holder.child_tv = (TextView) row.findViewById(R.id.child_tv);
            holder.child_switch = (Switch) row.findViewById(R.id.child_switch);
            holder.child_seekbar_tv = (TextView) row.findViewById(R.id.child_seekbar_tv);
            holder.child_seekbar = (SeekBar) row.findViewById(R.id.child_seekbar);
            row.setTag(holder);
        }else{
            holder = (ViewHolder) row.getTag();
        }

        holder.child_tv.setText(child.getText_name());
        holder.child_switch.setChecked(child.isSwitch_set());
        holder.child_seekbar.setProgress(child.getSeekbar_value());
        holder.child_seekbar_tv.setText(String.valueOf(child.getSeekbar_value()));

        Log.d("GPos CPos ", String.valueOf(groupPosition)+" "+String.valueOf(childPosition));

        holder.child_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                holder.child_seekbar_tv.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        holder.child_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                editor.putBoolean("switchBtn", isChecked);
                editor.apply();
                Toast.makeText(context, String.valueOf(isChecked), Toast.LENGTH_LONG).show();
            }
        });

        return row;
    }

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

    static class ViewHolder{
        TextView child_tv;
        TextView child_seekbar_tv;
        SeekBar child_seekbar;
        Switch child_switch;
    }
}

CustomChild.java

public class CustomChild {

    private String text_name, text_seekbar_value;
    private boolean switch_set;
    private int seekbar_value;

    public CustomChild(String text_name, String text_seekbar_value, boolean switch_set, int seekbar_value) {
        this.text_name = text_name;
        this.text_seekbar_value = text_seekbar_value;
        this.switch_set = switch_set;
        this.seekbar_value = seekbar_value;
    }

    public CustomChild(String text_name, boolean switch_set) {
        this.text_name = text_name;
        this.switch_set = switch_set;
    }

    public String getText_seekbar_value() {
        return text_seekbar_value;
    }

    public void setText_seekbar_value(String text_seekbar_value) {
        this.text_seekbar_value = text_seekbar_value;
    }

    public int getSeekbar_value() {
        return seekbar_value;
    }

    public void setSeekbar_value(int seekbar_value) {
        this.seekbar_value = seekbar_value;
    }

    public CustomChild(){

    }

    public String getText_name() {
        return text_name;
    }

    public void setText_name(String text_name) {
        this.text_name = text_name;
    }

    public boolean isSwitch_set() {
        return switch_set;
    }

    public void setSwitch_set(boolean switch_set) {
        this.switch_set = switch_set;
    }
}

MainActivity.java

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<CustomChild>> listDataChild;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        expListView = findViewById(R.id.expandableListView);
        prepareListData();
        listAdapter = new ExpandableListAdapter(getApplicationContext(), listDataHeader,
                listDataChild);

        expListView.setAdapter(listAdapter);
        expListView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

        expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
               Toast.makeText(getApplicationContext(), String.valueOf(groupPosition), Toast
                        .LENGTH_LONG).show();
            }
        });

        expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
                Toast.makeText(getApplicationContext(), String.valueOf(groupPosition), Toast
                        .LENGTH_LONG).show();
            }
        });

    }
    private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<CustomChild>>();
        listDataHeader.add("Nokia");
        listDataHeader.add("Apple");
        listDataHeader.add("Samsung");

        List<CustomChild> nokia = new ArrayList<CustomChild>();
        nokia.add(new CustomChild("N 70", "5", true, 3));
        nokia.add(new CustomChild("Nokia Lumia", "5", true, 8));

        List<CustomChild> samsung = new ArrayList<CustomChild>();
        samsung.add(new CustomChild("Galaxy S7", "5", true, 5));
        samsung.add(new CustomChild("Galaxy J7", "5", true, 6));
        samsung.add(new CustomChild("Galaxy z", "5", true, 9));


        listDataChild.put(listDataHeader.get(0), nokia);
        listDataChild.put(listDataHeader.get(2), samsung);
    }
}

Если у кого-нибудь есть какое-либо решение, пожалуйста, скажите мне Спасибо

...