【问题标题】:Fast scroll thumb disappears while scrolling AlphabetIndexer滚动 AlphabetIndexer 时快速滚动拇指消失
【发布时间】:2012-03-20 02:56:11
【问题描述】:

我有一个ListView,其中fastScrollAlwaysVisiblefastScrollEnabled 都设置为true。在我的AdapterAlphabetIndexer 实现SectionIndexer 后,我的fast scroll thumb 将在我滚动时消失,然后在我到达列表的顶部或底部时重新出现。我对为什么会发生这种情况一无所知。我以前没有经历过。

AlphabetIndexer 而言,以下所有内容均有效。我的问题是为什么我的快速滚动拇指在滚动时会消失,我该如何阻止它消失?

fast scroll 是否始终可见并不重要。每当fast scroll 可见时,fast scroll thumb 就不在那里,它就消失了,这就是我的问题。此外,当我删除 AlphabetIndexer 时,fast scroll thumb 就像我打算的那样工作。一切都在Activity 中成功运行,但是当我在Fragment 中加载我的ListView 时,事情最终就像我解释的那样。

这是我的Adapter 我的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 帮助正确分类音乐:

class MusicAlphabetIndexer extends AlphabetIndexer {

public MusicAlphabetIndexer(Cursor cursor, int sortedColumnIndex,
        CharSequence alphabet) {
    super(cursor, sortedColumnIndex, alphabet);
}

@Override
protected int compare(String word, String letter) {
    String wordKey = MediaStore.Audio.keyFor(word);
    String letterKey = MediaStore.Audio.keyFor(letter);
    if (wordKey.startsWith(letter)) {
        return 0;
    } else {
        return wordKey.compareTo(letterKey);
    }
  }
}

【问题讨论】:

  • 我在这里做类似的事情stackoverflow.com/questions/10224233/…
  • 我看了你的帖子,没有发现任何问题。我的SectionIndexer 工作并且在您滚动时不显示,看来您遇到了相反的问题,很奇怪。我开始认为Fragments 是罪魁祸首,但我不确定现在如何。我认为是因为我的SectionIndexerListActivity 中工作。
  • 我会尝试看看能否重现您的问题。

标签: java android android-listview


【解决方案1】:

我对快速滚动条的拇指图标也有类似的问题。我正在调查 Android 源代码,并发现了一个提交,它引入了这个问题和其他问题(ArrayIndexOutOfBoundsException)。我什至在没有这个提交的情况下构建了 Android 系统,然后它就可以工作了。

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

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

很遗憾,我没有找到任何解决方案,除了还原提交,我离开了它。
我希望有人能找到解决方法。

【讨论】:

  • 是的,我怀疑这是一个框架错误。我还看到 Play Store 中的其他应用程序也有同样的体验。我很高兴你发布了这个。
【解决方案2】:

您是否将fastScrollEnabledfastScrollAlwaysVisible 都设置为trueListView 没有 fastScrollAlwaysEnabled 属性,所以我想您可能只是将 fastScrollEnabled 设置为 true,但 fastScrollAlwaysVisible 设置为其默认值,即 false

【讨论】:

  • 哦,哇。这实际上是一个错字。我的意思是fastScrollAlwaysVisible。我也将fastScrollEnabled 设置为true。我实际上更新了我的OP。感谢您的评论,我永远不会注意到我打错了。
【解决方案3】:

您可以检查此代码:

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);
}

【讨论】:

【解决方案4】:

活动

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);
    }

}

上面的类是普通任务的虚拟数据类... 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 我从你的任务中理解。

【讨论】:

  • 这没有帮助。我知道如何实现SectionIndexer。我不知道为什么滚动时快速滚动拇指消失了。
  • 这可能是我遇到过的最没用的答案。
猜你喜欢
  • 1970-01-01
  • 2022-01-08
  • 1970-01-01
  • 2011-02-26
  • 2011-11-23
  • 2016-02-20
  • 2015-09-28
  • 2023-03-28
  • 1970-01-01
相关资源
最近更新 更多