如何在可扩展列表视图中实现下拉刷新和上拉刷新

时间:2013-09-10 13:38:44

标签: android android-layout android-intent android-listview

我想实现Pull down to Refresh并使用Expandable List View进行刷新。 我正在使用可扩展列表视图。以下是包含可扩展列表视图的Java代码。

public class LoadMoreListView extends ExpandableListView implements OnScrollListener {

private static final String TAG = "LoadMoreListView";

/**
 * Listener that will receive notifications every time the list scrolls.
 */
private OnScrollListener mOnScrollListener;
private LayoutInflater mInflater;

// footer view
private RelativeLayout mFooterView;
// private TextView mLabLoadMore;
private ProgressBar mProgressBarLoadMore;

// Listener to process load more items when user reaches the end of the list
private OnLoadMoreListener mOnLoadMoreListener;
// To know if the list is loading more items
private boolean mIsLoadingMore = false;
private int mCurrentScrollState;

public LoadMoreListView(Context context) {
    super(context);
    init(context);
}

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

public LoadMoreListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context);
}

private void init(Context context) {

    mInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    // footer
    mFooterView = (RelativeLayout) mInflater.inflate(
            R.layout.load_more_footer, this, false);
    /*
     * mLabLoadMore = (TextView) mFooterView
     * .findViewById(R.id.load_more_lab_view);
     */
    mProgressBarLoadMore = (ProgressBar) mFooterView
            .findViewById(R.id.load_more_progressBar);

    addFooterView(mFooterView);

    super.setOnScrollListener(this);
}

@Override
public void setAdapter(ListAdapter adapter) {
    super.setAdapter(adapter);
}

/**
 * Set the listener that will receive notifications every time the list
 * scrolls.
 * 
 * @param l
 *            The scroll listener.
 */
@Override
public void setOnScrollListener(AbsListView.OnScrollListener l) {
    mOnScrollListener = l;
}

/**
 * Register a callback to be invoked when this list reaches the end (last
 * item be visible)
 * 
 * @param onLoadMoreListener
 *            The callback to run.
 */

public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
    mOnLoadMoreListener = onLoadMoreListener;
}

public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {

    if (mOnScrollListener != null) {
        mOnScrollListener.onScroll(view, firstVisibleItem,
                visibleItemCount, totalItemCount);
    }

    if (mOnLoadMoreListener != null) {

        if (visibleItemCount == totalItemCount) {
            mProgressBarLoadMore.setVisibility(View.GONE);
            // mLabLoadMore.setVisibility(View.GONE);
            return;
        }

        boolean loadMore = firstVisibleItem + visibleItemCount >= totalItemCount;

        if (!mIsLoadingMore && loadMore
                && mCurrentScrollState != SCROLL_STATE_IDLE) {
            mProgressBarLoadMore.setVisibility(View.VISIBLE);
            // mLabLoadMore.setVisibility(View.VISIBLE);
            mIsLoadingMore = true;
            onLoadMore();
        }

    }

}

public void onScrollStateChanged(AbsListView view, int scrollState) {
    mCurrentScrollState = scrollState;

    if (mOnScrollListener != null) {
        mOnScrollListener.onScrollStateChanged(view, scrollState);
    }

}

public void onLoadMore() {
    Log.d(TAG, "onLoadMore");
    if (mOnLoadMoreListener != null) {
        mOnLoadMoreListener.onLoadMore();
    }
}

/**
 * Notify the loading more operation has finished
 */
public void onLoadMoreComplete() {
    mIsLoadingMore = false;
    mProgressBarLoadMore.setVisibility(View.GONE);
}

/**
 * Interface definition for a callback to be invoked when list reaches the
 * last item (the user load more items in the list)
 */
public interface OnLoadMoreListener {
    /**
     * Called when the list reaches the last item (the last item is visible
     * to the user)
     */
    public void onLoadMore();
}

}

下面是我将使用的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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="15dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:orientation="vertical" >

        <ExpandableListView
            android:id="@+id/listViewMeeting"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
             android:cacheColorHint="@color/White"
            android:listSelector="@color/transprent"
            android:groupIndicator="@color/transprent" >

        </ExpandableListView>

    </LinearLayout>

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

此库支持带有PullToRefresh的ExpandableListView。

<ExpandableListView>替换为<com.handmark.pulltorefresh.library. PullToRefreshExpandableListView>

答案 1 :(得分:-1)

对于仍然想要这个的人:

https://github.com/chrisbanes/Android-PullToRefresh

转到xml文件并删除行“pullFromBottom”以正常从顶部拉出(至少我认为它是“pullFromBottom”,但你会知道它)

相关问题