我已经遵循了一些stackoverflow线程,教程以及我可以从文档中收集的内容,但却无法让AplhabetIndexer
在Android中运行。目标是建立一个索引ListView
,用户可以根据手机上的标准联系人应用程序使用右侧的字母快速滚动。最后我会在列表中添加节标题,并将其作为用户类型进行过滤,但现在我只想让基本列表正常工作。
我可以加载列表并从光标中获取所有结果,但我永远不会在ListView
的右侧显示这些字母。我尝试过设置适配器的不同组合,包括在onCreateView
中使用空游标,然后在changeCursor(cursor)
回调中调用onLoadFinished()
,以及下面设置的当前版本适配器完全在onLoadFinished()
回调中。
有没有人可以分享他们的设置和适配器代码的完整工作版本?最好先使用创建适配器的方法,然后在changeCursor(cursor)
回调中调用onLoadFinished()
。
到目前为止我所拥有的:
StoreListAdapter.java
public class StoreListAdapter extends SimpleCursorAdapter implements SectionIndexer {
private AlphabetIndexer mAlphabetIndexer;
public StoreListAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to, int flags) {
super(context, layout, cursor, from, to, flags);
if(cursor != null){
mAlphabetIndexer = new AlphabetIndexer(cursor,
cursor.getColumnIndex(StoreEntry.TABLE_ALIAS + StoreEntry.COLUMN_NAME),
"ABCDEFGHIJKLMNOPQRTSUVWXYZ");
mAlphabetIndexer.setCursor(cursor);
}
}
@Override
public void changeCursor(Cursor cursor) {
super.changeCursor(cursor);
if(cursor != null){
mAlphabetIndexer = new AlphabetIndexer(cursor,
cursor.getColumnIndex(StoreEntry.TABLE_ALIAS + StoreEntry.COLUMN_NAME),
"ABCDEFGHIJKLMNOPQRTSUVWXYZ");
mAlphabetIndexer.setCursor(cursor);
}
}
@Override
public Object[] getSections() {
if(mAlphabetIndexer != null){
return mAlphabetIndexer.getSections();
}else{
return null;
}
}
@Override
public int getPositionForSection(int sectionIndex) {
if(mAlphabetIndexer != null){
return mAlphabetIndexer.getPositionForSection(sectionIndex);
}else{
return 0;
}
}
@Override
public int getSectionForPosition(int position) {
if(mAlphabetIndexer != null){
return mAlphabetIndexer.getSectionForPosition(position);
}else{
return 0;
}
}
}
StoreListFragment.java
public class StoreListFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
private ListView mListView;
private StoreListAdapter mAdapter;
public static StoreListFragment newInstance() {
StoreListFragment fragment = new StoreListFragment();
return fragment;
}
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public StoreListFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLoaderManager().initLoader(0, null, this);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_store_search, container, false);
mListView = (ListView) view.findViewById(R.id.search_result_list);
return view;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(
getActivity(), // Parent activity context
StoreProvider.CONTENT_URI, // Table to query
null, // Projection to return
null, // No selection clause
new String[]{getString(R.string.centre_id)}, // No selection arguments
null // Default sort order
);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mListView.setFastScrollEnabled(true);
mListView.setScrollingCacheEnabled(true);
mAdapter = new StoreListAdapter(getActivity().getApplicationContext(), R.layout.store_list_item, data, new
String[]{StoreEntry.TABLE_ALIAS + StoreEntry.COLUMN_NAME}, new int[]{R.id.item_name}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
mListView.setAdapter(mAdapter);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.changeCursor(null);
}
}
答案 0 :(得分:2)
确切的行为可以在FastScroller
类中找到,它是AbsListView
的帮助类。那里有一段代码决定&#34;列表是否很长&#34;
final boolean longList = childCount > 0 && itemCount / childCount >= MIN_PAGES;
MIN_PAGES
定义为值为4.如果您的列表项计数不是至少4倍,则子数(可见行)快速滚动,因此不会出现字母索引器。
答案 1 :(得分:1)
实际上,当我添加更多测试数据时,它开始工作。如果你的搜索结果很小(我大约使用了20左右),那么看起来似乎并没有开始。一旦我添加了大约100左右的虚拟数据,它就开始工作了。