我在更改位置上的GroupView的背景颜色时有问题。
根据我的守则
当我按第一个数字GroupView时,则第一个数字GroupView的颜色发生变化。



当我按第二个数字GroupView时,第一个数字GroupView的颜色已更改。

我想在位置展开和折叠时更改GroupView颜色。



ExpandableListViewAdapter.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.NumberPicker;
import android.widget.TextView;
import com.acase.clouds.cloudstailor.Models.ExpandableModel;
import com.acase.clouds.cloudstailor.R;
import java.util.List;

public class ExpandableListViewAdapter extends BaseExpandableListAdapter {

    private Context context;
    private LayoutInflater inflater;

    private List<ExpandableModel> listDataGroup;

    public ExpandableListViewAdapter(Context context, List<ExpandableModel> listDataGroup) {
        this.context = context;
        this.listDataGroup = listDataGroup;
        this.inflater = LayoutInflater.from(context);
    }

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

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

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        ExpandableModel model = listDataGroup.get(groupPosition);
        if (convertView == null) {
            convertView = inflater.inflate(model.getLayoutId(), null);
        }

        switch (model.getType()) {
            case AGE:
                setNumberPicker(convertView, model);
                break;
            case STATE:
                setNumberPicker(convertView, model);
                break;
        }

        return convertView;
    }

    private void setNumberPicker(View convertView, final ExpandableModel model) {
        NumberPicker numberPicker = convertView.findViewById(R.id.numberPicker);
        numberPicker.setMinValue(0);
        numberPicker.setMaxValue(99);
        if (!model.getValue().isEmpty()) {
            numberPicker.setValue(Integer.parseInt(model.getValue()));
        }
        // to change formate of number in numberpicker
        numberPicker.setFormatter(new NumberPicker.Formatter() {
            @Override
            public String format(int i) {
                return String.format("%02d", i);
            }
        });

        numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker numberPicker, int i, int i1) {
                model.setValue(i1 + "");
            }
        });
    }


    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
  //      String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_row_group, null);
        }
        ExpandableModel model = listDataGroup.get(groupPosition);
        TextView tv_title = convertView.findViewById(R.id.tv_title);
        TextView tv_value = convertView.findViewById(R.id.tv_value);
        tv_title.setText(model.getName());
        tv_value.setText(model.getValue());
        return convertView;
    }

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

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


EditProfileActivity

 import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.acase.clouds.cloudstailor.Adapter.ExpandableListViewAdapter;
import com.acase.clouds.cloudstailor.Models.ExpandableModel;
import com.bumptech.glide.Glide;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class EditProfileActivity extends AppCompatActivity {

    Toolbar toolbar;
    ImageView profileimage;
    TextView changephototext, textView;
    RelativeLayout item;
    private static final int PICK_IMAGE_REQUEST = 1;
    public Uri path;



    private ExpandableListView expandableListView;
    private ExpandableListViewAdapter expandableListViewAdapter;
    List<ExpandableModel> list = new ArrayList<>();

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

        toolbar = findViewById(R.id.tool).findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Edit Profile");

        profileimage = findViewById(R.id.Profile_Img);
        profileimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fileChooser();
            }
        });

        changephototext = findViewById(R.id.Change_Photo_Button);
        changephototext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fileChooser();
            }
        });


        // initializing the views
        initViews();

        // initializing the listeners
        initListeners();

        // initializing the objects
        initObjects();

    }
    private void initViews() {

        expandableListView = findViewById(R.id.expandableListView);
    }

    private void initListeners() {

        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                ExpandableModel model = list.get(groupPosition);
                Toast.makeText(EditProfileActivity.this, model.getName() + " clicked at " + childPosition, Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                ExpandableModel model = list.get(groupPosition);
                Toast.makeText(EditProfileActivity.this, model.getName() + " expanded", Toast.LENGTH_SHORT).show();

                item = findViewById(R.id.Group);
                item.setBackgroundResource(R.drawable.selectedlistback);

                textView = findViewById(R.id.tv_title);
                textView.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
            }
        });

        // ExpandableListView Group collapsed listener
        expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {
                ExpandableModel model = list.get(groupPosition);
                Toast.makeText(EditProfileActivity.this, model.getName() + " collapsed", Toast.LENGTH_SHORT).show();

                item = findViewById(R.id.Group);
                item.setBackgroundResource(R.drawable.listback);

                textView = findViewById(R.id.tv_title);
                textView.setTextColor(getResources().getColor(R.color.colorAccent));
            }
        });

    }

    private void initObjects() {

        ExpandableModel age = new ExpandableModel("AGE", "", ExpandableModel.ExpandableType.AGE, R.layout.list_row_child);
        list.add(age);

        ExpandableModel state = new ExpandableModel("STATE", "", ExpandableModel.ExpandableType.STATE, R.layout.list_row_child);
        list.add(state);


        expandableListViewAdapter = new ExpandableListViewAdapter(this, list);

        expandableListView.setAdapter(expandableListViewAdapter);

    }

    private void fileChooser()
    {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, PICK_IMAGE_REQUEST);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data.getData() != null)
        {
            path = data.getData();

            Glide.with(this).load(path).into(profileimage);
            path = data.getData();
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),path);

                profileimage.setImageBitmap(bitmap);

            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}

最佳答案

尝试在适配器的getGroupView中添加以下代码:

    if(isExpanded) {
        convertView.setBackgroundResource(R.drawable.selectedlistback);
        tv_title.setTextColor(context.getResources().getColor(R.color.colorPrimaryDark));
    }else{
        convertView.setBackgroundResource(R.drawable.listback);
        tv_title.setTextColor(context.getResources().getColor(R.color.colorAccent));
    }


并删除监听器。希望有帮助!

关于java - 如何在ExpandableListView中的Expand上更改GroupView背景颜色?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56222311/

10-10 03:18