3ISvZFqczFBVUtz 3ISvZFqczFBVUtz - 2 months ago 7
Java Question

ActionBarPullToRefresh doesn't work when list is empty

I am using Chris Banes ActionBar-PullToRefresh on my ListFragment.

Everything works fine except when the ListView is empty. When the ListView is empty (and the empty text is visible), the pull to refresh functionality doesn't work altough I did set

getListView().getEmptyView()
in
theseChildrenArePullable()
.

I also tried
allChildrenArePullable()
which didn't work.

The code from my 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);
}


The code from my 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>

Answer

So I fixed this by using the SwipeRefreshLayout which is native.

To get this working you need your view:

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

And some code in your onViewCreated function:

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