首页 > 解决方案 > 如果在 Android Studio 上突出显示至少一个子视图,则突出显示可扩展列表视图的标题视图

问题描述

如果至少有一个子视图被突出显示,我希望可扩展列表视图上的标题视图在初始程序后立即自动更改背景颜色

在此处输入图像描述 我以http://tutorialscache.com/expandable-listview-android-tutorials/为例

这是 ExpandableCustomAdapter 类

public class ExpandableCustomAdapter extends BaseExpandableListAdapter{

//Initializing variables
private List<String> headerData;
private HashMap<String, ArrayList<ChildDataModel>> childData;
private Context mContext;
private LayoutInflater layoutInflater;

// constructor
public ExpandableCustomAdapter(Context mContext, List<String> headerData,
                             HashMap<String, ArrayList<ChildDataModel>> childData) {
    this.mContext = mContext;
    this.headerData = headerData;
    this.childData = childData;
    this.layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

@Override
public int getChildrenCount(int headPosition) {
    return this.childData.get(this.headerData.get(headPosition)).size();
}

@Override
public Object getGroup(int headPosition) {
    return this.headerData.get(headPosition);
}

@Override
public Object getChild(int headPosition, int childPosition) {
    return this.childData.get(this.headerData.get(headPosition))
            .get(childPosition);
}

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

@Override
public long getChildId(int headPosition, int childPosition) {
    return this.childData.get(this.headerData.get(headPosition))
            .get(childPosition).getId();
}

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

@Override
public View getGroupView(int headPosition, boolean is_expanded, View view, ViewGroup headGroup) {
    // Heading of each group
    String heading = (String) getGroup(headPosition);
    if (view==null){
        view = layoutInflater.inflate(R.layout.list_header,null);

    }
    TextView headerTv = view.findViewById(R.id.headerTv);
    headerTv.setText(heading+"");
    headerTv.setBackgroundColor( Color.BLUE );
    return view;
}

@Override
public View getChildView(int headPosition, int childPosition, boolean islastChild, View view, ViewGroup viewGroup) {

     ChildDataModel child = (ChildDataModel) getChild(headPosition, childPosition);

    if (view == null) {
        view = layoutInflater.inflate(R.layout.child_item, null);
    }

    TextView childTv = (TextView) view.findViewById(R.id.childTv);
    ImageView childImg = (ImageView) view.findViewById(R.id.childImg);

    childTv.setText(child.getTitle());
    if(child.getTitle().equalsIgnoreCase( "China" ))
    {
        childTv.setBackgroundColor( Color.RED );
    }
    childImg.setImageResource(child.getImage());
    return view;
}

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

这是 ChildDataModel 类

public class ChildDataModel {

long id;
int image;
String title;

public ChildDataModel(int id, String country, int image) {
     this.setId(id);
     this.setTitle(country);
     this.setImage(image);
}

public int getImage() {
    return image;
}

public void setImage(int image) {
    this.image = image;
}

public long getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

@Override
public String toString() {
    Log.d("response ","ID: "+getId()+" Title: "+getTitle());
    return super.toString();
}
}

这是 MainActivity 类

public class MainActivity extends AppCompatActivity {

ExpandableCustomAdapter expandableCustomAdapter;
ExpandableListView expandableListView;
List<String> headerData;
HashMap<String,ArrayList<ChildDataModel>> childData;
ChildDataModel childDataModel;
Context mContext;
ArrayList<ChildDataModel> asianCountries,africanCountries,nAmericanCountries,sAmericanCountries;
private int lastExpandedPosition = -1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mContext = this;

    //initializing arraylists
    headerData = new ArrayList<>();
    childData = new HashMap<String,ArrayList<ChildDataModel>>();
    asianCountries = new ArrayList<>();
    africanCountries = new ArrayList<>();
    nAmericanCountries = new ArrayList<>();
    sAmericanCountries = new ArrayList<>();



    // link listview from activity_main.xml
    expandableListView = findViewById(R.id.expandAbleListView);

    //populating data of world continents and their countries.
    headerData.add("ASIA");

    //adding countries to Asian continent
    childDataModel = new ChildDataModel(1,"Afghanistan",R.drawable.afghanistan);
    asianCountries.add(childDataModel);

    childDataModel = new ChildDataModel(2,"China",R.drawable.china);
    asianCountries.add(childDataModel);

    childDataModel = new ChildDataModel(3,"India",R.drawable.india);
    asianCountries.add(childDataModel);

    childDataModel = new ChildDataModel(4,"Pakistan",R.drawable.pakistan);
    asianCountries.add(childDataModel);

    childData.put(headerData.get(0),asianCountries);


    headerData.add("AFRICA");

    //adding countries to African continent
    childDataModel = new ChildDataModel(1,"South Africa",R.drawable.southafrica);
    africanCountries.add(childDataModel);

    childDataModel = new ChildDataModel(2,"Zimbabwe",R.drawable.zimbabwe);
    childData.put(headerData.get(1),africanCountries);


    headerData.add("NORTH AMERICA");
    //adding countries to NORTH AMERICA continent
    childDataModel = new ChildDataModel(1,"Canada",R.drawable.canada);
    nAmericanCountries.add(childDataModel);
    childData.put(headerData.get(2),nAmericanCountries);


    headerData.add("SOUTH AMERICA");
    //adding countries to SOUTH AMERICA continent
    childDataModel = new ChildDataModel(1,"Argentina",R.drawable.argentena);
    sAmericanCountries.add(childDataModel);
    childData.put(headerData.get(3),sAmericanCountries);



    //set adapter to list view
    expandableCustomAdapter = new ExpandableCustomAdapter(mContext,headerData,childData);
    expandableListView.setAdapter(expandableCustomAdapter);

    //child click listener
    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView expandableListView, View view, int headPosition, int childPosition, long id) {
            Toast.makeText(mContext,
                    headerData.get(headPosition)
                            + " has country "
                            + childData.get(
                            headerData.get(headPosition)).get(
                            childPosition).getTitle(), Toast.LENGTH_SHORT)
                    .show();
            return false;
        }
    });

    //group expanded
    expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int headPosition) {
            if (lastExpandedPosition != -1
                    && headPosition != lastExpandedPosition) {
                expandableListView.collapseGroup(lastExpandedPosition);
            }
            lastExpandedPosition = headPosition;
            Toast.makeText(mContext,
                    headerData.get(headPosition) + " continent expanded",
                    Toast.LENGTH_SHORT).show();
        }
    });

    //group collapsed
     expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
         @Override
         public void onGroupCollapse(int headPosition) {
             Toast.makeText(mContext,
                     headerData.get(headPosition) + " continent collapsed",
                     Toast.LENGTH_SHORT).show();
         }
     });

    //Group Indicator
    expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            parent.smoothScrollToPosition(groupPosition);

            if (parent.isGroupExpanded(groupPosition)) {
                ImageView imageView = v.findViewById(R.id.expandable_icon);
                imageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right));
            } else {
                ImageView imageView = v.findViewById(R.id.expandable_icon);
                imageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_down));
            }
            return false    ;
        }
    });
}
}

我希望在没有setOnChildClickListener、setOnGroupExpandListener、setOnGroupCollapseListener等事件的情况下处理它。感谢您的帮助

标签: androidandroid-studioandroid-layoutandroid-intent

解决方案


将 getGroupView 更改为:

@Override
public View getGroupView(int headPosition, boolean is_expanded, View view, ViewGroup headGroup) {
    // Heading of each group
String heading = (String) getGroup(headPosition);
if (view==null){
    view = layoutInflater.inflate(R.layout.list_header,null);

}
TextView headerTv = view.findViewById(R.id.headerTv);
headerTv.setText(heading+"");
boolean hasSelected = false;
ArrayList<ChildDataModel> childs = 
childData.get(headerData.getItemAtIndex(headPosition));
for(int i=0;i<childs.size();i++){
if(childs.get(i).isSelected){
hasSelected = true;
    }}
if(hasSelected)
headerTv.setBackgroundColor( Color.BLUE );
 else
headerTv.setBackgroundColor( Color.RED);

return view;

}


推荐阅读