android - ActionbarSherlock 列表导航 - 滚动到顶部/聚焦第一行

标签 android actionbarsherlock

我需要一些有关 Sherlock 列表导航的帮助。 目标:当你打开导航时,我想滚动到顶部/聚焦到第一行,所以第一个项目总是可见的。 问题:现在当列表有更多项目时屏幕可以显示(通常在横向模式下),当我选择第 4 个项目然后打开列表时,第一个项目不可见,它聚焦到最后一个选择的项目。 它在自定义微调器中使用下面的代码对我有用,但是当我尝试在 IcsSpinner 中覆盖相同的方法时,它没有用。

代码:

/**

* Cusrom 微调器类 - 打开时始终关注第一项 * */ 类 CustomSpinnerSelection 扩展 Spinner {

private boolean mToggleFlag = true;

//some constructors here

@Override
public int getSelectedItemPosition() {
    // this toggle is required because this method will get called in other
    // places too, the most important being called for the
    // OnItemSelectedListener
    if (!mToggleFlag) {
        return 0; // get us to the first element
    }
    return super.getSelectedItemPosition();
}

@Override
public boolean performClick() {
    // this method shows the list of elements from which to select one.
    // we have to make the getSelectedItemPosition to return 0 so you can
    // fool the Spinner and let it think that the selected item is the first
    // element
    mToggleFlag = false;
    boolean result = super.performClick();
    mToggleFlag = true;
    return result;
}

最佳答案

既然您已经使用微调器完成了,那么有一种可能的方法。

您可以为操作栏使用自定义 View ,其中包含用于列表导航的微调器,并使用您的自定义微调器类而不是默认微调器。

自定义 View xml 可能如下所示:

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

    <Spinner
    android:id="@+id/actionBarSpinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:entries="@array/default_list" />

</LinearLayout>

您需要通过代码启用和设置自定义 View 。

View view = this.getLayoutInflater ().inflate (R.layout.actionbar_customview, null);
actionBar.setCustomView (view);
actionBar.setDisplayShowCustomEnabled (true);

此外,您还可以保存微调器以设置任何监听器或执行任何其他操作。

Spinner actionBarSpinner = ((Spinner) view.findViewById (R.id.actionBarSpinner));

只是不是 Spinner,而是您的 CustomSpinner 类。

关于android - ActionbarSherlock 列表导航 - 滚动到顶部/聚焦第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12857598/

相关文章:

android - onclick 按钮没有获取 api 的编辑文本字符串

android - findFragmentByTag - 在 FragmentTabHost 中寻找 fragment - 始终为 null

android - 自定义 ImageView 未实例化 java.lang.UnsupportedOperationException

android - ActionBar-PullToRefresh 和 ActionBarSherlock

android - 在访问设备库等外部应用程序并返回我的应用程序时,再次调用 OnCreate()

android - 尝试将菜单按钮居中

android - WebView 中 facebook 的登录页面中没有键盘

android-actionbar - 打开 ActionLayout 时隐藏菜单项

Android,ActionBar下的Progress Bar,并去除圆形进度

带有access_token的php facebook graph api查询