SwipeRefreshLayout setRefreshing不显示Fragment中的进度条

时间:2016-05-12 10:48:11

标签: android swiperefreshlayout

我在这个片段中使用以下代码:

public class OwnerPendingBookingsFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {

private View myView;
private ListView booking_list;
private ArrayAdapter adapter;
private SharedPreferences preferences;
private BookingList bookings;
private SwipeRefreshLayout swipe;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.owner_pending_bookings_layout, container, false);

        swipe = (SwipeRefreshLayout) myView.findViewById(R.id.swipe);
        swipe.setOnRefreshListener(this);
        swipe.post(new Runnable() {
                       @Override
                       public void run() {
                           swipe.setRefreshing(true);

                           new FindBookings().execute("");
                       }
                   }
        );

    return myView;
}

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

}


private void setUpAdapter(){
    booking_list = (ListView) myView.findViewById(R.id.pending_bookings);
    if(bookings.getBookings().size() == 0){
        booking_list.setVisibility(View.GONE);
        LinearLayout no_vehicles = (LinearLayout) myView.findViewById(R.id.no_bookings_layout);
        no_vehicles.setVisibility(View.VISIBLE);
    }else {
        new setImageTask().execute("");
        adapter = new OwnerPendingBookingsAdapter(myView.getContext(), bookings.getBookings());
        booking_list.setAdapter(adapter);
        booking_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int booking_line = bookings.getBookings().get(position).getBooking_line_id();
                Fragment booking_info = new OwnerPendingBookingInfoFragment();
                Bundle args = new Bundle();
                args.putInt("bookingLine", booking_line);
                booking_info.setArguments(args);
                FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.content_frame, booking_info);
                transaction.addToBackStack(null);
                transaction.commit();
            }
        });
        booking_list.setVisibility(View.VISIBLE);
    }
    //LinearLayout progressBar = (LinearLayout) myView.findViewById(R.id.loadingPanel);
    //progressBar.setVisibility(View.GONE);
}

@Override
public void onRefresh() {
    swipe.setRefreshing(true);
    new FindBookings().execute("");
}

private class FindBookings extends AsyncTask<String, Void, String> {
    String token = preferences.getString(Config.TOKEN, "");


    public FindBookings() {
    }

    @Override
    protected void onPreExecute() {
        swipe.setRefreshing(true);
    }

    @Override
    protected void onPostExecute(String s){
        setUpAdapter();
        swipe.setRefreshing(false);
    }

    @Override
    protected String doInBackground(String... params) {
        bookings = DBConnect.getInfo();
        return "done";
    }
}

我有以下布局:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/pending_bookings"
    android:layout_gravity="center_horizontal"
    android:divider="@null"
    android:visibility="gone"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/no_bookings_layout">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/no_vehicles"
        android:textSize="25dp"
        android:textAlignment="center"
        />
</LinearLayout>

第一次启动时,它正确地使我看到进度滚动直到它完成进程。但是每次我向下滑动它都会开始更新,但我没有看到进度条。

我检查SwipeRefreshLayout setRefreshing() not showing indicator initiallyAndroid ProgressBar styled like progress view in SwipeRefreshLayout,但这些答案都不适用于我。

2 个答案:

答案 0 :(得分:2)

如果您希望能够刷新空视图,则必须将FrameLayout(包括ListView和EmptyView)放在SwipeRefreshLayout内。另外,在FrameLayout中设置android:clickable="true"

试试这段代码:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe"
android:layout_width="match_parent"
android:layout_height="wrap_content">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true">
            <ListView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/pending_bookings"
                android:layout_gravity="center_horizontal"
                android:divider="@null"
                android:visibility="gone"/>
            <TextView
                android:id="@+id/no_bookings_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="@string/no_vehicles"
                android:textSize="25dp"
                android:textAlignment="center"/>
    </FrameLayout>

归功于antoinem

修改

如果您在滚动列表时遇到问题。每次在该视图中向上滚动时,SwipeRefreshLayout都会触发并更新,使您的应用无法在列表中向上滚动。在您的Activity或片段中添加此代码:

listView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                             int visibleItemCount, int totalItemCount) {
            int topRowVerticalPosition = (listView == null ||
                    listView.getChildCount() == 0) ? 0 : listView.getChildAt(0).getTop();
            swipeRefreshLayout.setEnabled(firstVisibleItem == 0 && topRowVerticalPosition >= 0);
        }
    });

归功于this post by Nacho Lopez

答案 1 :(得分:0)

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe"
android:layout_width="match_parent"
android:layout_height="wrap_content">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true">
            <ListView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/pending_bookings"
                android:layout_gravity="center_horizontal"
                android:divider="@null"
                android:visibility="gone"/>
            <TextView
                android:id="@+id/no_bookings_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="@string/no_vehicles"
                android:textSize="25dp"
                android:textAlignment="center"/>
    </FrameLayout>

这真的很有效,并且在“主活动”中没有任何更改,只需添加此框架布局,然后使用webview和progressbar,就可以正常工作...