简体   繁体   English

Android:当ListView中没有/限制项目时,如何将标题滚动到顶部?

[英]Android: How to scroll header to top when there is no/limited items in ListView?

I'm developing an Android app in which I added a header view in ListView. 我正在开发一个Android应用程序,其中在ListView中添加了标题视图。 Now I want that header view is displayed only when a user scrolls down the ListView. 现在,我希望仅当用户向下滚动ListView时显示该标题视图。 I tried following, but was not successful to achieve required output. 我尝试了以下操作,但未成功实现所需的输出。

  • listView.setSelection(1) works well but only when the data size is above the viewable area, ie ListView is scrollable. listView.setSelection(1)可以很好地工作,但是仅当数据大小在可见区域之上时,即ListView是可滚动的。
  • android:scrollY is giving the required UI output but scroll down of list view is not smooth for header, ie search bar comes down with jerk. android:scrollY提供了所需的UI输出,但列表视图的向下滚动对于标题而言并不平滑,即搜索栏因混蛋而下降。

I need the same behavior even when there is only one item in list. 即使列表中只有一项,我也需要相同的行为。 But when data in list is limited, the header is always visible. 但是,当列表中的数据受到限制时,标题始终可见。

在此处输入图片说明

Any suggestion(s) will be highly appreciated. 任何建议将不胜感激。

Thanks, Ammar 谢谢阿玛

This effect can be achieved easily with a simple trick, make sure to include compile 'com.android.support:support-v13:20.0.0' in build.gradle or if you are on Eclipse then download latest support jar file: 通过一个简单的技巧就可以轻松实现此效果,请确保在build.gradle包含compile 'com.android.support:support-v13:20.0.0' ;如果您使用的是Eclipse,则下载最新的支持jar文件:

Use following xml layout: 使用以下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>

This is your Activity s code: 这是您的Activity的代码:

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

One last piece: 最后一件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM