所以我有这个ExpandableListView,它应该突出显示被点击的组。 我这样做是通过运行
eList.getChildAt(eList.getFlatListPosition(ExpandableListView.getPackedPositionForGroup(i))).setBackgroundColor(Color.LTGRAY);
而i是被点击的群组视图的索引。
但是,正如您在此处提供的图像中所看到的,当我向上遍历列表时,它只会返回正确的视图。
http://i.imgur.com/I9e943A.gif
我应该注意,当我将onGroupClick()设置为返回false时,getChildAt返回正确的值并突出显示正确的组视图。但是,将其设置为false会导致组点击时打开两次(子元素的两倍),因为我“手动”折叠/扩展组作为/ searchterm /更改。
出于调试目的,我尝试在单击任何组时突出显示getChildAt(1)。除了点击“0”组时,“1”组在所有情况下都突出显示,而最后一组则突出显示。
课程可以在下面找到
MainActivity:
public class MainActivity extends ActionBarActivity {
HashMap<String, List<String>> categoryName;
List<String> categoryList;
ExpandableListView eList;
CategoryAdapter adapter;
EditText textField;
int lastHighlightParent = -1;
int lastHighlightChild = -1;
int groupToHighlight;
String prevDirName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Laboration 2");
setContentView(R.layout.activity_main);
textField = (EditText) findViewById(R.id.etTextField);
textField.setText("€/€");
eList = (ExpandableListView) findViewById(R.id.exp_list);
textField.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (lastHighlightChild != -1 && lastHighlightParent != -1) {
eList.getChildAt(
eList.getFlatListPosition(ExpandableListView
.getPackedPositionForChild(
lastHighlightParent,
lastHighlightChild)))
.setBackgroundColor(Color.TRANSPARENT);
lastHighlightChild = -1;
lastHighlightParent = -1;
}
for (int i = 0; i < eList.getChildCount(); i++) {
eList.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
}
boolean match = false;
for (int i = 0; i < adapter.getGroupCount(); i++) {
for (int j = 0; j < adapter.getChildrenCount(i); j++) {
String tempPath = "/" + adapter.getGroupText(i) + "/"
+ adapter.getChildText(i, j);
if (tempPath.startsWith(s.toString())
&& s.toString().length() > 1 && match != true) {
groupToHighlight = eList.getFlatListPosition(ExpandableListView
.getPackedPositionForGroup(i));
eList.getChildAt(eList.getFlatListPosition(ExpandableListView
.getPackedPositionForGroup(i))).setBackgroundColor(Color.LTGRAY);
textField.setBackgroundColor(Color.TRANSPARENT);
match = true;
} else if (s.toString().equals("/") || s.length() == 0) {
textField.setBackgroundColor(Color.TRANSPARENT);
} else {
if (match != true) {
eList.getChildAt(i).setBackgroundColor(
Color.TRANSPARENT);
textField.setBackgroundColor(Color.RED);
}
}
}
}
for (int i = 0; i < adapter.getGroupCount(); i++) {
if (s.toString().startsWith(
"/" + adapter.getGroupText(i) + "/")) {
if (!eList.isGroupExpanded(i)) {
eList.expandGroup(i);
}
for (int j = 0; j < adapter.getChildrenCount(i); j++) {
if (s.toString().endsWith(
adapter.getChildText(i, j))) {
eList.getChildAt(
eList.getFlatListPosition(ExpandableListView
.getPackedPositionForChild(i, j)))
.setBackgroundColor(Color.GRAY);
lastHighlightParent = i;
lastHighlightChild = j;
}
}
} else {
eList.collapseGroup(i);
}
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
eList.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
textField.setText("/" + adapter.getGroupText(groupPosition)
+ "/"
+ adapter.getChildText(groupPosition, childPosition));
return false;
}
});
eList.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
if (adapter.getGroupText(groupPosition).equals(prevDirName)
&& eList.isGroupExpanded(groupPosition)) {
eList.collapseGroup(groupPosition);
} else {
textField.setText("/" + adapter.getGroupText(groupPosition)
+ "/");
}
prevDirName = adapter.getGroupText(groupPosition);
return true;
}
});
categoryName = listData();
categoryList = new ArrayList<String>(categoryName.keySet());
adapter = new CategoryAdapter(this, categoryName, categoryList);
eList.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private HashMap<String, List<String>> listData() {
HashMap<String, List<String>> categoryDetail = new HashMap<String, List<String>>();
List<String> drinks = new ArrayList<String>();
drinks.add("Tea");
drinks.add("Coffee");
drinks.add("Water");
drinks.add("Lemonade");
drinks.add("Beer");
List<String> animals = new ArrayList<String>();
animals.add("Cat");
animals.add("Dog");
animals.add("Dromedary");
animals.add("Boar");
List<String> vegetables = new ArrayList<String>();
vegetables.add("Lettuce");
vegetables.add("Tomato");
vegetables.add("Spinach");
vegetables.add("Cucumber");
List<String> colors = new ArrayList<String>();
colors.add("Blue");
colors.add("Red");
colors.add("Pink");
colors.add("Brown");
List<String> sodas = new ArrayList<String>();
sodas.add("Cola");
sodas.add("Pepsi");
sodas.add("Fanta");
sodas.add("7-up");
categoryDetail.put("Animals", animals);
categoryDetail.put("Vegetables", vegetables);
categoryDetail.put("Colors", colors);
categoryDetail.put("Sodas", sodas);
categoryDetail.put("Drinks", drinks);
return categoryDetail;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
textField.setText(item.getTitle());
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
适配器:
public class CategoryAdapter extends BaseExpandableListAdapter {
private Context context;
private HashMap<String, List<String>> categories;
private List<String> categoryList;
public CategoryAdapter(Context m_context, HashMap<String, List<String>> m_categories, List<String> m_categoryList){
this.context = m_context;
this.categories = m_categories;
this.categoryList = m_categoryList;
}
public String getGroupText(int groupPosition){
Object[] keys = this.categories.keySet().toArray();
return (String) keys[groupPosition];
}
public String getChildText(int groupPosition, int childPosition){
Object[] keys = this.categories.keySet().toArray();
return (String) categories.get(keys[groupPosition]).get(childPosition);
}
@Override
public int getGroupCount() {
return categoryList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return categories.get(categoryList.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return categoryList.get(groupPosition);
}
@Override
public Object getChild(int parent, int child) {
return categories.get(categoryList.get(parent)).get(child);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int parent, int child){
return child;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int parent, boolean isExpanded,
View convertView, ViewGroup parentView) {
String groupTitle = (String) getGroup(parent);
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.parent_layout, parentView, false);
}
TextView parentTextView = (TextView) convertView.findViewById(R.id.parent_txt);
parentTextView.setTypeface(null, Typeface.BOLD);
parentTextView.setText(groupTitle);
return convertView;
}
@Override
public View getChildView(int parent, int child,
boolean isLastChild, View convertView, ViewGroup parentView) {
String childTitle = (String) getChild(parent, child);
if (convertView == null) {
LayoutInflater inflat = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflat.inflate(R.layout.child_layout, parentView, false);
}
TextView childTextView = (TextView) convertView.findViewById(R.id.child_txt);
childTextView.setText(childTitle);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}