如果Android Studio上至少突出显示了一个子视图,则突出显示可扩展列表视图的标题视图

如果Android Studio上至少突出显示了一个子视图,则突出显示可扩展列表视图的标题视图,android,android-studio,android-layout,android-intent,Android,Android Studio,Android Layout,Android Intent,我希望可扩展列表视图上的标题视图在初始程序后立即自动更改背景色,如果至少有一个子视图高亮显示 我从中得到这个程序作为一个例子 这是ExpandableCustomAdapter类 public class ExpandableCustomAdapter extends BaseExpandableListAdapter{ //Initializing variables private List<String> headerData; private HashMap<Str

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

我从中得到这个程序作为一个例子

这是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;
}
}
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();
}
}
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    ;
        }
    });
}
}
这里是主要活动类

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;
}
}
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();
}
}
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    ;
        }
    });
}
}
public类MainActivity扩展了AppCompatActivity{
ExpandableCustomAdapter ExpandableCustomAdapter;
ExpandableListView ExpandableListView;
列表标题数据;
哈希映射数据;
ChildDataModel ChildDataModel;
语境;
亚洲列强国家、非洲列强国家、列强国家、列强国家;
private int lastExpandedPosition=-1;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext=这个;
//初始化数组列表
headerData=新的ArrayList();
childData=newhashmap();
asianCountries=新ArrayList();
非洲国家=新ArrayList();
nAmericanCountries=newarraylist();
sAmericanCountries=新ArrayList();
//从activity_main.xml链接listview
expandableListView=findViewById(R.id.expandableListView);
//世界各大洲及其国家的人口数据。
标题数据。添加(“亚洲”);
//向亚洲大陆添加国家
childDataModel=新的childDataModel(1,“阿富汗”,R.drawable.Afghanistan);
add(儿童数据模型);
childDataModel=新的childDataModel(2,“中国”,R.drawable.China);
add(儿童数据模型);
childDataModel=新的childDataModel(3,“印度”,R.drawable.India);
add(儿童数据模型);
childDataModel=新的childDataModel(4,“巴基斯坦”,R.drawable.巴基斯坦);
add(儿童数据模型);
childData.put(headerData.get(0),亚洲国家);
headerData.add(“非洲”);
//向非洲大陆添加国家
childDataModel=新的childDataModel(1,“南非”,R.drawable.南非);
非洲国家。添加(儿童数据模型);
childDataModel=新的childDataModel(2,“津巴布韦”,R.drawable.津巴布韦);
childData.put(headerData.get(1),非洲国家);
headerData.add(“北美”);
//向北美大陆添加国家
childDataModel=新的childDataModel(1,“加拿大”,R.drawable.Canada);
nAmericanCountries.add(childDataModel);
childData.put(headerData.get(2),名称为国家);
headerData.add(“南美洲”);
//向南美大陆添加国家
childDataModel=新的childDataModel(1,“阿根廷”,R.drawable.argentena);
sAmericanCountries.add(childDataModel);
childData.put(headerData.get(3),同样的国家);
//将适配器设置为列表视图
expandableCustomAdapter=新的expandableCustomAdapter(mContext、headerData、childData);
setAdapter(expandableCustomAdapter);
//子单击侦听器
expandableListView.setOnChildClickListener(新的expandableListView.OnChildClickListener(){
@凌驾
公共布尔onChildClick(ExpandableListView ExpandableListView,View视图,int headPosition,int childPosition,long id){
Toast.makeText(mContext,
headerData.get(头位置)
+“有国家”
+childData.get(
headerData.get(headPosition)).get(
childPosition).getTitle(),Toast.LENGTH\u SHORT)
.show();
返回false;
}
});
//集团扩张
expandableListView.setOnGroupExpandListener(新的expandableListView.OnGroupExpandListener(){
@凌驾
公共职位(内部职位){
如果(lastExpandedPosition!=-1
&&头部位置!=上次展开的位置){
expandableListView.collapseGroup(上次展开位置);
}
lastExpandedPosition=头部位置;
Toast.makeText(mContext,
headerData.get(headPosition)+“大陆扩展”,
吐司。长度(短)。show();
}
});
//小组崩溃了
expandableListView.setOnGroupCollapseListener(新的expandableListView.OnGroupCollapseListener(){
@凌驾
公共空间塌陷(内部头位置){
Toast.makeText(mContext,
headerData.get(headPosition)+“大陆崩溃”,
吐司。长度(短)。show();
}
});
//组指示器
expandableListView.setOnGroupClickListener(新的expandableListView.OnGroupClickListener(){
@凌驾
公共布尔onGroupClick(ExpandableListView父视图、视图v、int-groupPosition、长id){
父.平滑滚动位置(groupPosition);
if(父级.isGroupExpanded(groupPosition)){
ImageView ImageView=v.findViewById(R.id.expandable_图标);
setImageDrawable(getResources().getDrawable(R.drawable.arrow_right));
}否则{
ImageView ImageView=v.findViewById(R.id.expandable_图标);
setImageDrawable(getResources().getDrawable(R.drawable.arrow_down));
}
返回false;
}
});
}
}

我希望在处理它时不会出现像setOnChildClickListener、setOnGroupExpandListener、setOnGroupCollapseListener这样的事件,等等。感谢您的帮助

如果childs High对所选Chiled进行签名,则在执行getGroupView时最突出显示标题视图;如果选中eny,则在执行getGroupView时最突出显示标题视图

如果childs High对所选Chiled进行签名,则在执行getGroupView时最突出显示标题视图;如果选中eny,则在执行getGroupView时最突出显示标题视图

将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;
@覆盖
公共视图getGroupView(int headPosition,布尔值为