我创建了一个ExpandableListView
搜索活动,它显示 3 个组 -曲目、艺术家、专辑。当用户在 中键入文本时EditText
,相应的搜索结果会显示在 ExpandableListView 中。
问题:
当用户单击任何子项时,我想执行一些操作。起初这个功能工作正常,但是当我向上或向下滚动列表时,OnChildClickListener
停止工作。并且没有孩子在点击时被选中。
这是视图更新和位置的问题吗?
注意:我正在使用BaseExpandableListAdapter
这是代码:(我已经粘贴了我认为必要的内容。如果需要其他部分,请告诉我):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
etSearchBar = (EditText) findViewById(R.id.etSearchBar);
etSearchBar.addTextChangedListener(this);
lvExpSearchResult = (ExpandableListView) findViewById(R.id.lvExpSearchResult);
lvExpSearchResult.setGroupIndicator(null);
lvExpSearchResult.setChildIndicator(null);
lvExpSearchResult.setOnTouchListener(this);
lvExpSearchResult.setOnChildClickListener(this);
}
@Override
public boolean onChildClick(ExpandableListView parent, View view, int groupPosition,
int childPosition, long id) {
lvExpSearchResult.smoothScrollToPosition(groupPosition);
Toast.makeText(SearchActivity.this, group[groupPosition], Toast.LENGTH_SHORT).show();
//Toast.makeText(SearchActivity.this, children[groupPosition][childPosition], Toast.LENGTH_LONG).show();
return true;
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > 0) {
group = new String[] { "Tracks", "Albums", "Artists" };
ArrayList<String> result = null;
result = getQueryResult(MediaStore.Audio.Media.TITLE);
result = new Utilities().removeDuplicatesAL(result);
String[] tracks = result.toArray(new String[result.size()]);
result = getQueryResult(MediaStore.Audio.Media.ALBUM);
result = new Utilities().removeDuplicatesAL(result);
String[] albums = result.toArray(new String[result.size()]);
result = getQueryResult(MediaStore.Audio.Media.ARTIST);
result = new Utilities().removeDuplicatesAL(result);
String[] artists = result.toArray(new String[result.size()]);
children = new String[][] { tracks, albums, artists };
adapter = new SearchAdapter(this, group, children);
} else {
adapter = new SearchAdapter(this, new String[0], new String[0][0]);
}
}
@Override
public void afterTextChanged(Editable s) {
lvExpSearchResult.setAdapter(adapter);
}
// BaseExpandableListAdapter code - getGroupView & getChildView
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView childRow = (TextView) convertView;
if (childRow == null) {
LayoutInflater inflator = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
try {
childRow = (TextView) inflator.inflate(
android.R.layout.simple_expandable_list_item_1, null);
} catch (Exception e) {
Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT)
.show();
}
}
childRow.setText(children[groupPosition][childPosition]);
return childRow;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView headerRow = (TextView) convertView;
if (headerRow == null) {
LayoutInflater inflator = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
headerRow = (TextView) inflator.inflate(
android.R.layout.simple_expandable_list_item_1, null);
}
headerRow.setTypeface(Typeface.DEFAULT_BOLD);
headerRow.setText(group[groupPosition]);
ExpandableListView eLV = (ExpandableListView)parent;
eLV.expandGroup(groupPosition);
return headerRow;
}