【问题标题】:Android: How to scroll header to top when there is no/limited items in ListView?Android:当ListView中没有/有限的项目时如何将标题滚动到顶部?
【发布时间】:2014-06-06 18:45:54
【问题描述】:

我正在开发一个 Android 应用程序,在其中我在 ListView 中添加了一个标题视图。 现在我希望仅在用户向下滚动时显示标题视图 列表显示。我尝试了以下操作,但未能成功实现所需的输出。

  • listView.setSelection(1) 效果很好,但仅在数据大小为 在可视区域上方,即 ListView 是可滚动的。
  • android:scrollY 正在提供所需的 UI 输出,但向下滚动 标题的列表视图不流畅,即搜索栏出现 混蛋。

即使列表中只有一项,我也需要相同的行为。但是当列表中的数据 是有限的,标题总是可见的。

我们将不胜感激任何建议。

谢谢, 阿马尔

【问题讨论】:

  • 你可以用图片更好地解释它吗?
  • @Nun'e Chai,图片分享
  • 您说“即使列表中只有一项,我也需要相同的行为。”但后来你说“当列表中的数据有限时,标题总是可见的。”你能再解释一下吗?这两种说法似乎相互矛盾。

标签: android android-layout listview android-listview


【解决方案1】:

这个效果可以通过一个简单的技巧轻松实现,确保在build.gradle 中包含compile 'com.android.support:support-v13:20.0.0',或者如果您使用的是 Eclipse,请下载最新的支持 jar 文件:

使用以下 xml 布局:

<android.support.v4.widget.SwipeRefreshLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pullToShowBar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="false">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="false"
    android:orientation="vertical">

    <TextView
        android:id="@+id/searchBar"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_marginTop="-48dp"
        android:background="@android:color/darker_gray"
        android:gravity="center"
        android:text="My Hidden Search Bar" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_world" />
</LinearLayout>

</android.support.v4.widget.SwipeRefreshLayout>

这是您的Activitys 代码:

public class MyActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    final View searchBar = findViewById(R.id.searchBar);
    final SwipeRefreshLayout sw = (SwipeRefreshLayout) findViewById(R.id.pullToShowBar);
    sw.setColorSchemeColors(Color.TRANSPARENT, Color.TRANSPARENT, Color.TRANSPARENT, Color.TRANSPARENT);
    sw.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            sw.setRefreshing(false);
            ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) searchBar.getLayoutParams();
            layoutParams.setMargins(0, 0, 0, 0);
            searchBar.setLayoutParams(layoutParams);
        }
    });
}
}

最后一张:

import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.widget.AbsListView;


public class PullToSearchBarLayout extends SwipeRefreshLayout {
private AbsListView mListView;

public PullToSearchBarLayout(Context context) {
    super(context);
}

public PullToSearchBarLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}


@Override
public boolean canChildScrollUp() {
    if (mListView == null) {
        mListView = (AbsListView) findViewById(android.R.id.list);
    }
    if (android.os.Build.VERSION.SDK_INT < 14) {
        final AbsListView absListView = mListView;
        return absListView.getChildCount() > 0
                && (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
                .getTop() < absListView.getPaddingTop());
    } else {
        return ViewCompat.canScrollVertically(mListView, -1);
    }
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 2019-12-17
    相关资源
    最近更新 更多