简体   繁体   中英

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. Now I want that header view is displayed only when a user scrolls down the 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.
  • 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.

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:

Use following xml layout:

<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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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