Właśnie znalazł obejście, aby zapobiec system wyświetlania tego złego zachowania.
Istnieją dwa scenariusze, które używają innego kodu, aby zadziałał SectionIndexer
.
Pierwszy scenariusz polega na użyciu FastScrollbar-Thumb w celu przejścia do następnej sekcji. Zakładając, że grupy są twoi Sekcje nadpisane metody realizacji SectionIndexer
będzie wyglądać tak:
@Override
public int getPositionForSection(int section) {
return section;
}
// Gets called when scrolling the list manually
@Override
public int getSectionForPosition(int position) {
return ExpandableListView.getPackedPositionGroup(
expandableListView
.getExpandableListPosition(position));
}
Drugi scenariusz jest tak, że podczas przewijania listy ręcznie i szybkie przewijania poruszać według sekcji, nie wszystkie przedmioty. Kod wygląda więc tak:
@Override
public int getPositionForSection(int section) {
return expandableListView.getFlatListPosition(
ExpandableListView.getPackedPositionForGroup(section));
}
// Gets called when scrolling the list manually
@Override
public int getSectionForPosition(int position) {
return ExpandableListView.getPackedPositionGroup(
expandableListView
.getExpandableListPosition(position));
}
Jak widać, te dwa zachowania nie mogą grać razem bez dalszej adopcji.
Obejście problemu polegające na tym, że oba działają, polega na przechwyceniu sprawy, gdy ktoś przewija jedną rękę (to jest przewijanie za pomocą dotyku). Można to zrobić przy wdrażaniu interfejs OnScrollListener
z klasy adaptera i ustawić ją na ExpandableListView
:
public class MyExpandableListAdapter extends BaseExpandableListAdapter
implements SectionIndexer, AbsListView.OnScrollListener {
// Your fields here
// ...
private final ExpandableListView expandableListView;
private boolean manualScroll;
public MyExpandableListAdapter(ExpandableListView expandableListView
/* Your other arguments */) {
this.expandableListView = expandableListView;
this.expandableListView.setOnScrollListener(this);
// Other initializations
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
this.manualScroll = scrollState == SCROLL_STATE_TOUCH_SCROLL;
}
@Override
public void onScroll(AbsListView view,
int firstVisibleItem,
int visibleItemCount,
int totalItemCount) {}
@Override
public int getPositionForSection(int section) {
if (manualScroll) {
return section;
} else {
return expandableListView.getFlatListPosition(
ExpandableListView.getPackedPositionForGroup(section));
}
}
// Gets called when scrolling the list manually
@Override
public int getSectionForPosition(int position) {
return ExpandableListView.getPackedPositionGroup(
expandableListView
.getExpandableListPosition(position));
}
// Your other methods
// ...
}
To Naprawiono błąd dla mnie.
Czy wiersze na liście mają różne rozmiary? –
Czy możesz ... proszę ... wstawić swój kod XML – Rockin
Prześlij swój kod/xml –