当列表为空时,ActionBarPullToRefresh 不起作用

ActionBarPullToRefresh doesn't work when list is empty

我在我的 ListFragment 上使用 Chris Banes ActionBar-PullToRefresh

一切正常,但 ListView 为空时除外。当 ListView 为空(并且空文本可见)时,虽然我在 theseChildrenArePullable().

中设置了 getListView().getEmptyView(),但下拉刷新功能不起作用

我也试过 allChildrenArePullable() 没用。

来自我的 ListFragment 的代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    listAdapter = new ListsAdapter(getActivity(), items);
    setListAdapter(listAdapter);

    Cursor cursor = ListProvider.getInstance().selectAll();
    ArrayList<BaseModel> cursorItems = ListProvider.getInstance().rowsToArrayList(cursor);
    for(BaseModel cursorItem : cursorItems) {
        items.add((ListModel) cursorItem);
    }
}

public void onActivityCreated(Bundle savedState) {
    super.onActivityCreated(savedState);
    registerForContextMenu(getListView());
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View layout = inflater.inflate(R.layout.adapter_lists, container, false);
    return layout;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    getListView().setOnItemLongClickListener(this);

    mPullToRefreshLayout = new PullToRefreshLayout(getActivity());

    ActionBarPullToRefresh.from(getActivity())
            .insertLayoutInto((ViewGroup) view)
            .theseChildrenArePullable(getListView(), getListView().getEmptyView())
            .listener(this)
            .setup(mPullToRefreshLayout);
}

代码来自我的 adapter_lists.xml:

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

    <ListView android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:drawSelectorOnTop="false"/>

    <TextView android:id="@id/android:empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:text="@string/message_my_lists_empty"/>

</LinearLayout>

所以我使用原生的 SwipeRefreshLayout 解决了这个问题。

要使这项工作正常进行,您需要有自己的看法:

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:text="@string/hello_world"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:gravity="center"/>

    </ScrollView>

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

以及您的 onViewCreated 函数中的一些代码:

swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorScheme(
    android.R.color.holo_blue_bright, 
    android.R.color.holo_green_light, 
    android.R.color.holo_orange_light, 
    android.R.color.holo_red_light);