我试图在用户触摸可扩展列表视图中的子项时设置背景颜色。我设法通过使用此代码来完成此操作:
ArrayList<ChildView> childViewList;
ExpandableListView expandablelistview_options = (ExpandableListView) view.findViewById(R.id.expandablelistview_options);
expandablelistview_options.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
{
int index = removeChildView(groupPosition);
ArrayList<ConciergeOptionsPairs> children=data.Options.get(groupPosition).childrens;
if (index != childPosition)
{
v.setBackgroundColor(ContextCompat.getColor(getActivity(),R.color.orange));
v.invalidate();
addChildView(new ChildView(groupPosition, childPosition, v, children.get(childPosition)));
}
return false;
}
});
private int removeChildView(int groupPosition)
{
int index = 0;
int childpos = 0;
boolean exists = false;
for (ChildView c : childViewList)
{
if (c.groupPosition == groupPosition)
{
c.view.setBackgroundColor(ContextCompat.getColor(getActivity(),R.color.transparent));
c.view.invalidate();
childpos = c.childPosition;
exists = true;
break;
}
index++;
}
if (exists)
{
childViewList.remove(index);
return childpos;
}
return -1;
}
private void addChildView(ChildView child)
{
childViewList.add(child);
}
除了这种奇怪的行为外,一切正常:
我有2组视图,如果我扩展第二组,选择第二个孩子,然后展开第一个组,第一组的第一个孩子的背景变色,第二个组的孩子的背景得到透明的。
当我在没有选择任何孩子的情况下扩展第一组时:
如果我折叠第一组,第二组孩子的背景会再次变色。
这是我的ExpandableListView适配器代码:
public class ConciergeOptionsExpandableListAdapter extends BaseExpandableListAdapter
{
private Context context;
private ArrayList<ConciergeOptions> data;
private HotelStay hotelStay;
private int lastExpandedGroupPosition;
public ConciergeOptionsExpandableListAdapter(Context context, ArrayList<ConciergeOptions> data) {
this.context = context;
this.data = data;
hotelStay=HotelStay.getInstance(context);
}
@Override
public int getGroupCount() {
return this.data.size();
}
@Override
public Object getGroup(int groupPosition) {
return this.data.get(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
ConciergeOptions options = (ConciergeOptions) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.concierge_options_item, null);
}
RelativeLayout container = (RelativeLayout) convertView.findViewById(R.id.container);
TextView option_name = (TextView) convertView.findViewById(R.id.option_name);
ImageView arrow = (ImageView) convertView.findViewById(R.id.arrow);
option_name.setText(options.name);
if(isExpanded){
convertView.setBackgroundResource(R.color.bluebutton);
}else{
convertView.setBackgroundResource(R.color.transparent);
}
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return (this.data.get(groupPosition).childrens.size());
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
ArrayList<ConciergeOptionsPairs> children=this.data.get(groupPosition).childrens;
if(children!=null){
return children.get(childPosititon);
}
return null;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.concierge_options_inner_items, null);
}
TextView option_type = (TextView) convertView.findViewById(R.id.option_type);
TextView price = (TextView) convertView.findViewById(R.id.price);
TextView decimal = (TextView) convertView.findViewById(R.id.decimal);
TextView currency = (TextView) convertView.findViewById(R.id.currency);
ConciergeOptionsPairs option_pair = (ConciergeOptionsPairs) getChild(groupPosition, childPosition);
option_type.setText(option_pair.name);
double t_price = Double.parseDouble(option_pair.value);
double dec = t_price - (long) t_price;
long decim = (long) dec;
int decimal_int = (int) t_price;
price.setText("+" + String.valueOf(decimal_int));
decimal.setText("." + String.valueOf(decim));
currency.setText("$");
return convertView;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public boolean hasStableIds() {
return false;
}
}
知道如何解决这个问题?
答案 0 :(得分:1)
我将 ConciergeOptionsPairs 视为您的子数据,因此在 ConciergeOptionsPairs 类中添加一个布尔变量 isSelect ,然后在 onChildClick
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
{
ArrayList<ConciergeOptionsPairs> children=data.Options.get(groupPosition).childrens; //i consider this is your child view array, need to be same dataset which used as child array
int count=0;
for(ConciergeOptionsPairs objChild:children)
{
if(count==childPosition)
objChild.isSelect=true;
else
objChild.isSelect=false;
count++;
}
yourexpandableadpater.notifyDataSetChanged();
return false;
}
});
现在位于 getChildView
中的适配器中 @Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.concierge_options_inner_items, null);
}
TextView option_type = (TextView) convertView.findViewById(R.id.option_type);
TextView price = (TextView) convertView.findViewById(R.id.price);
TextView decimal = (TextView) convertView.findViewById(R.id.decimal);
TextView currency = (TextView) convertView.findViewById(R.id.currency);
ConciergeOptionsPairs option_pair = (ConciergeOptionsPairs) getChild(groupPosition, childPosition);
option_type.setText(option_pair.name);
double t_price = Double.parseDouble(option_pair.value);
double dec = t_price - (long) t_price;
long decim = (long) dec;
int decimal_int = (int) t_price;
price.setText("+" + String.valueOf(decimal_int));
decimal.setText("." + String.valueOf(decim));
currency.setText("$");
if(option_pair.isSelect)
convertView.setBackgroundColor(ContextCompat.getColor(context,R.color.orange));
else
convertView.setBackgroundColor(ContextCompat.getColor(context,R.color.transparent));
return convertView;
}
这就是我理解您可以使用此
检查的代码