滚动AlphabetIndexer时,快速滚动缩略图消失

时间:2012-03-20 02:56:12

标签: java android android-listview

我有一个ListView fastScrollAlwaysVisiblefastScrollEnabled都设置为true。在我SectionIndexerAdapter实施AlphabetIndexer后,我的fast scroll thumb会在我滚动时消失,然后在我到达列表的顶部或底部后重新出现。对于为什么会这样,我很无能为力。我之前没有经历过。

以下所有内容都与AlphabetIndexer有关。我的问题是为什么我的快速滚动拇指在我滚动时消失了,我怎么能阻止它消失呢?

fast scroll 始终是否可见无关紧要。每当fast scroll可见时,fast scroll thumb就不存在,它就会消失,这就是我的问题。此外,当我删除AlphabetIndexer fast scroll thumb作品时,就像我想要的那样。一切都在Activity成功运作,但当我将ListView加载到Fragment时,最终会像我解释一样。

这是我Adapter的{​​{1}}:

ListView

private class AlbumsAdapter extends SimpleCursorAdapter implements SectionIndexer { private AlphabetIndexer mIndexer; // I have to override this because I'm using a `LoaderManager` @Override public Cursor swapCursor(Cursor cursor) { if (cursor != null) { mIndexer = new MusicAlphabetIndexer(cursor, mAlbumIdx, getResources().getString(R.string.fast_scroll_alphabet)); } return super.swapCursor(cursor); } @Override public Object[] getSections() { return mIndexer.getSections(); } @Override public int getPositionForSection(int section) { return mIndexer.getPositionForSection(section); } @Override public int getSectionForPosition(int position) { return 0; } } 有助于正确排序音乐:

MusicAlphabetIndexer

4 个答案:

答案 0 :(得分:7)

我在快速滚动的拇指图标上遇到了类似的问题。我正在研究Android源代码并发现了一个引入此问题的提交和其他(ArrayIndexOutOfBoundsException)。我甚至在没有这个提交的情况下构建了Android系统,然后就可以了。

我在六月提交了这个问题:https://code.google.com/p/android/issues/detail?id=33293
当我读它时,我知道我可以更好地描述这个问题:)

这是提出问题的提交:https://github.com/android/platform_frameworks_base/commit/32c3a6929af9d63de3bf45a61be6e1a4bde136d3

不幸的是我没有找到任何解决方案,除了恢复提交,我离开了它 我希望有人能找到解决方法。

答案 1 :(得分:2)

您是否将fastScrollEnabledfastScrollAlwaysVisible设置为truefastScrollAlwaysEnabled没有ListView属性,所以我想也许您只是将fastScrollEnabled设置为true,但fastScrollAlwaysVisible设置为其默认值,即{ {1}}。

答案 2 :(得分:0)

您可以查看以下代码:

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion <= android.os.Build.VERSION_CODES.FROYO){
    // Do something for froyo and above versions
    list.setFastScrollEnabled(true);


} else if(currentapiVersion > android.os.Build.VERSION_CODES.HONEYCOMB){
    // do something for phones running an SDK before froyo
    list.setFastScrollEnabled(true);
    list.setFastScrollAlwaysVisible(true);
}

答案 3 :(得分:-2)

活动

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.AlphabetIndexer;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.SimpleCursorAdapter;

public class TestAct extends Activity {
    /** Called when the activity is first created. */
    ListView test_listView;
    Cursor myCursor;
    String[] proj;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_app_test);

        TestDummyData cTestDummyData = new TestDummyData(
                getApplicationContext());
        cTestDummyData.open();
        cTestDummyData.insertRandomNames();
        myCursor = cTestDummyData.fetchAllData();

        test_listView = (ListView) findViewById(R.id.pager_list_test);
        test_listView.setFastScrollEnabled(true);

        test_listView.setAdapter(

        new MyCursorAdapter(getApplicationContext(),
                android.R.layout.simple_list_item_1, myCursor,
                new String[] { TestDummyData.KEY_NAME },// names
                new int[] { android.R.id.text1 })

        );

        cTestDummyData.close();

    }

    class MyCursorAdapter extends SimpleCursorAdapter implements SectionIndexer {

        AlphabetIndexer alphaIndexer;

        // HashMap<String, Integer> alphaIndexer;
        // String[] sections;
        public MyCursorAdapter(Context context, int layout, Cursor c,
                String[] from, int[] to) {
            super(context, layout, c, from, to);

            alphaIndexer = new AlphabetIndexer(c,
                    myCursor.getColumnIndex(TestDummyData.KEY_NAME),
                    " ABCDEFGHIJKLMNOPQRSTUVWXYZ");
            // ======optional way to get alphaindexer from data
            // alphaIndexer = new HashMap<String, Integer>();
            // int size = items.size();
            //
            // for (int x = 0; x < size; x++) {
            // String s = items.get(x);
            //
            // String ch = s.substring(0, 1);
            //
            // ch = ch.toUpperCase();
            //
            // alphaIndexer.put(ch, x);
            // }
            //
            // Set<String> sectionLetters = alphaIndexer.keySet();
            //
            // ArrayList<String> sectionList = new ArrayList<String>(
            // sectionLetters);
            //
            // Collections.sort(sectionList);
            //
            // sections = new String[sectionList.size()];
            //
            // sectionList.toArray(sections);

        }

        @Override
        public int getPositionForSection(int section) {
            // TODO Auto-generated method stub
            return alphaIndexer.getPositionForSection(section);
        }

        @Override
        public int getSectionForPosition(int position) {
            // TODO Auto-generated method stub
            return alphaIndexer.getSectionForPosition(position);
        }

        @Override
        public Object[] getSections() {
            // TODO Auto-generated method stub
            return alphaIndexer.getSections();
        }

    }

}

类使用虚拟数据进行列表

import java.util.Random;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class TestDummyData {

    static final String KEY_ID = "_id";
    static final String KEY_NAME = "name";

    private static final String DB_NAME = "tutorial";
    private static final String TABLE_NAME = "names";
    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE = "create table " + TABLE_NAME
            + " (" + KEY_ID + " integer primary key autoincrement, " + KEY_NAME
            + " varchar not null);";

    private Context context;
    private DatabaseHelper dbHelper;
    private SQLiteDatabase db;

    public TestDummyData(Context context) {
        this.context = context;
        this.dbHelper = new DatabaseHelper(this.context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
            super(context, DB_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.v("DBUTIL", "Upgrading database from version " + oldVersion
                    + " to " + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);
        }
    }

    public void open() {
        db = dbHelper.getWritableDatabase();
    }

    public void close() {
        dbHelper.close();
    }

    public void insertRandomNames() {
        db.execSQL("DELETE FROM " + TABLE_NAME);
        String s = "ANDROIDDEVELOPER";
        Random r = new Random();

        ContentValues values = new ContentValues();
        for (int i = 0; i < 200; i++) {
            values.clear();
            values.put(KEY_NAME, s.substring(r.nextInt(s.length())));
            db.insert(TABLE_NAME, null, values);
        }
    }

    public Cursor fetchAllData() {
        return db.rawQuery("SELECT * FROM " + TABLE_NAME + " ORDER BY "
                + KEY_NAME + " ASC", null);
    }

}

上面的类是用于常见任务的虚拟数据clss ... list_app_test.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<ListView 
android:id="@+id/pager_list_test" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</ListView>

</LinearLayout>

你刚刚给出了test_listView.setFastScrollEnabled(true); gvn ans我从你的任务中理解。