0
我創建了ExpandableListView
搜索活動,其中顯示3組 - 曲目,藝術家,專輯。 當用戶輸入EditText
中的文本時,相應的搜索結果顯示在ExpandableListView中。Android:ExpandableListView ChildClickListener無法在列表滾動後工作
問題: 當用戶點擊任何子項時,我想執行一些操作。起初這個功能工作正常,但是當我向上或向下滾動列表時,OnChildClickListener
停止工作。沒有孩子被點擊選擇。
這是視圖更新&位置的問題嗎?
注:我使用的是BaseExpandableListAdapter
下面是代碼:(我貼什麼,我認爲是必要的PLZ讓我知道,如果需要其他的部分。):
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;
}
當你在EditText中輸入文本會發生什麼?也許你每次更改文本時都會初始化你的列表?嘗試使用自定義的ExpandableList並覆蓋getChildView並在其上設置點擊監聽器。 – 2012-07-31 09:02:54
是的,每次用戶鍵入一個新列表時,都會傳遞給一個自定義的'BaseExpandListListAdapter'並且結果會被更新。我在'EditText'上添加了一個'addTextChangeListener',它調用適配器並顯示結果。代碼已更新。 – reiley 2012-07-31 09:20:03
嘗試在你的適配器的getChildView方法的childView上設置onClickListener,像這樣https://github.com/gobozov/myshows/blob/master/src/ru/myshows/fragments/EpisodesFragment.java – 2012-07-31 09:27:23