トラック、アーティスト、アルバムのExpandableListView
3 つのグループを表示する検索アクティビティを作成しました。ユーザーが にテキストを入力すると、対応する検索結果が ExpandableListView に表示されます。EditText
問題:
ユーザーが子アイテムをクリックしたときに、何らかの操作を実行したいと考えています。最初はこの機能は正常に機能していますが、リストを上下にスクロールすると機能しなくなり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;
}