如果在 Android Studio 上突出显示至少一个子视图,则突出显示可扩展列表视图的标题视图
Posted
技术标签:
【中文标题】如果在 Android Studio 上突出显示至少一个子视图,则突出显示可扩展列表视图的标题视图【英文标题】:Highlight Header View of expandable list view if Its at least one Child View is highlighted on Android Studio 【发布时间】:2020-01-06 01:02:59 【问题描述】:如果至少有一个子视图被突出显示,我希望可扩展列表视图上的标题视图在初始程序后立即自动更改背景颜色
我以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、 等事件的情况下处理它。感谢您的帮助
【问题讨论】:
ingetGroupView
遍历组的孩子并检查是否需要突出显示其中的任何一个。如果有的话 - 突出显示该组。
【参考方案1】:
将 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;
【讨论】:
【参考方案2】:如果 childs highl sign 选择 chileds 并且如果选择 eny 然后突出显示 header,则执行 getGroupView 时,您最突出显示标题视图
【讨论】:
公共类 ChildDataModel long id;整型图像;字符串标题; boolean isSelected【参考方案3】:谢谢@hunixa siuri 它确实运作良好。但是代码必须进行如下编辑
@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.get(headPosition));
for(int i=0;i<childs.size();i++)
if(childs.get(i).getTitle().equalsIgnoreCase( "China" ))
hasSelected = true;
if(hasSelected)
view.setBackgroundColor( Color.BLUE );
else
view.setBackgroundColor( Color.RED);
return view;
【讨论】:
以上是关于如果在 Android Studio 上突出显示至少一个子视图,则突出显示可扩展列表视图的标题视图的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Visual Studio Code 中因缺少导入而突出显示错误?
有没有办法在 Visual Studio 2010 中突出显示当前活动的代码块?