Brandon
Brandon

Reputation: 1886

SwipeRefreshLayout Issues When Using emptyView and ListView

I'm using a SwipeRefreshLayout wrapped around a ListView. When the ListView is empty, I've set a TextView to appear. This 'works' fine, but when the ListView is empty and the empty view is showing, the SwipeRefreshLayout doesn't display right. If you drag down, no circle comes down but when you release your finger the circle flashes on the screen and then disappears. If you just touch the screen anywhere, in fact, the SwipeRefreshLayout will appear in the same fashion. The following is how I've defined it in my XML:

<FrameLayout
    android:id="@+id/empty_view_requests"
    android:layout_width="match_parent"
    android:layout_centerInParent="true"
    android:layout_below="@+id/appbar"
    android:layout_height="match_parent">

    <android.support.v4.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/appbar"
        android:id="@+id/buddy_requests_swipe_layout">
        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/buddy_requests_listview"
            android:layout_gravity="center_horizontal|top"
            />
    </android.support.v4.widget.SwipeRefreshLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:layout_gravity="center_horizontal|center_vertical"
        android:id="@+id/norequests_textview"
        android:text="No buddy requests."/>
</FrameLayout>

EDIT: Activity Code

swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.buddy_requests_swipe_layout);
    swipeRefreshLayout.setColorSchemeResources(R.color.lb_green, R.color.lb_orange, R.color.lb_purple);
    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            VolleyBuddies.makeGetBuddyRequestsAPICall(BuddyRequestsActivity.this, BuddyRequestsActivity.this);
        }
    });
    FrameLayout noRequestsLayout = (FrameLayout)findViewById(R.id.empty_view_requests);
    ListView requestsListView = (ListView)findViewById(R.id.buddy_requests_listview);
    requestListAdapter = new BuddyRequestsActivityListAdapter(this, this);
    requestsListView.setAdapter(requestListAdapter);
    requestsListView.setEmptyView(noRequestsLayout);

How can I fix this?

Upvotes: 2

Views: 836

Answers (4)

ObjectiveTruth
ObjectiveTruth

Reputation: 878

Try placing your TextView inside a ScrollView. I found that the SwipeRefreshLayout requires a scrollable view of some sort

Your code would be

<FrameLayout
android:id="@+id/empty_view_requests"
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:layout_below="@+id/appbar"
android:layout_height="match_parent">

<android.support.v4.widget.SwipeRefreshLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/appbar"
    android:id="@+id/buddy_requests_swipe_layout">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/buddy_requests_listview"
        android:layout_gravity="center_horizontal|top"
        />
</android.support.v4.widget.SwipeRefreshLayout>
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:layout_gravity="center_horizontal|center_vertical"
        android:id="@+id/norequests_textview"
        android:text="No buddy requests."/>
</ScrollView>
</FrameLayout>

Should work right away

Upvotes: 1

Sree Reddy Menon
Sree Reddy Menon

Reputation: 1311

I am pasting working code of mine.Even I have faced the issues. hope this will works for you. Initially set the emptyview visibility View.GONE.and then on the API Call response callBacks, if there are no items/statuscode..etc ,setvisibility View.VISIBLE and set it as emptyviewfor listview.

findViewById(R.id.lvemptyview).setVisibility(View.GONE);



    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
             if (Utils.isInternetConnected(getApplicationContext())) {
                        CallAPICall(lat, lng);
                        mSwipeRefreshLayout.setRefreshing(false);
                    } }});

on volley response callback

 mSwipeRefreshLayout.setRefreshing(false);

            if (statuscode== 200) {
                try {

                    String content = new String(arg2, "UTF-8");
                    MemberResp memberResp;
                    Gson gson = new Gson();
                    Type type = new TypeToken<MemberResp>() {
                    }.getType();
                        memberResp = gson.fromJson(content, type);
                        if (memberResp.Status == 1) {

                            if (memberResp.bar_lists.size() > 0) {
                                      findViewById(R.id.lvemptyview).setVisibility(View.GONE);
                          setadapter();
                  } else {

               Adapter.ClearAll();
                          findViewById(R.id.lvemptyview).setVisibility(View.VISIBLE);
                             lvList.setEmptyView(findViewById(R.id.lvemptyview));


                            }

Upvotes: 1

Manikandan Selvanathan
Manikandan Selvanathan

Reputation: 915

Empty layout is text view. Please refer following code. and set adapter after SetEmptyListView.

TextView noRequestsLayout = (TextView)findViewById(R.id.norequests_textview);
    ListView requestsListView = (ListView)findViewById(R.id.buddy_requests_listview);
    requestListAdapter = new BuddyRequestsActivityListAdapter(this, this);
    requestsListView.setEmptyView(noRequestsLayout);
     requestsListView.setAdapter(requestListAdapter);

Upvotes: 0

Vaibhav Sharma
Vaibhav Sharma

Reputation: 2319

You have set your parent framelayout as an empty view of the child(listview). Try removing that line.

Upvotes: 0

Related Questions