我正在尝试在我的ListView上快速滚动时显示字母。出于某种原因,出现的字母始终是我的字母表中的第一个字母(空格)。似乎getSectionForPosition()
总是返回0,即使它不应该返回0。以下是我的代码:
activity_host.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".HostActivity" >
<ListView android:id="@+id/song_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fastScrollEnabled="true" />
</RelativeLayout>
HostActivity.java
public class HostActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_host);
loadSongs();
}
private void loadSongs() {
Uri songsUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] projection = SongCursorAdapter.SONG_COLUMNS;
String songFilter = MediaStore.Audio.Media.IS_MUSIC + " != 0";
String orderBy = SongCursorAdapter.ORDERED_BY;
Cursor songCursor = new CursorLoader(this, songsUri, projection, songFilter, null, orderBy).loadInBackground();
SongCursorAdapter songsAdapter = new SongCursorAdapter(this, songCursor, 0);
ListView songView = (ListView) this.findViewById(R.id.song_list);
songView.setAdapter(songsAdapter);
}
}
SongCursorAdapter.java
class SongCursorAdapter extends CursorAdapter implements SectionIndexer {
public final static String[] SONG_COLUMNS = { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.TITLE_KEY, MediaStore.Audio.Media.DATA };
public final static String ORDERED_BY = MediaStore.Audio.Media.DEFAULT_SORT_ORDER;
private Context context;
private AlphabetIndexer indexer;
SongCursorAdapter(Context context, Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
this.context = context;
setupIndexer(c);
}
SongCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
this.context = context;
setupIndexer(c);
}
private void setupIndexer(Cursor c) {
int sortedByColumn = c.getColumnIndexOrThrow(ORDERED_BY);
String alphabet = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
indexer = new AlphabetIndexer(c, sortedByColumn, alphabet);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// code irrelevant
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.song_layout, null);
return view;
}
@Override
public Cursor swapCursor(Cursor c) {
if (c != null) {
setupIndexer(c);
}
return super.swapCursor(c);
}
@Override
public int getPositionForSection(int section) {
return indexer.getPositionForSection(section);
}
@Override
public int getSectionForPosition(int position) {
return indexer.getSectionForPosition(position);
}
@Override
public Object[] getSections() {
return indexer.getSections();
}
}
编辑:现在没有任何信件出现。不过,导航栏表现得很奇怪。每次向下滚动时,它都会跳回到顶部。