swiperefresh不能在片段中工作。
我的XML代码:

<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pagerefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:id="@+id/progress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical" /></android.support.v4.widget.SwipeRefreshLayout>

我的fragmnet代码:
View drawer = inflater.inflate(R.layout.fragment_progress, container, false);

    swipeRefreshLayout = (SwipeRefreshLayout) drawer.findViewById(R.id.pagerefresh);
    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            swipeRefreshLayout.setRefreshing(true);
            getProgressData();
        }
    });
    swipeRefreshLayout.setColorSchemeColors(android.R.color.holo_green_dark,
            android.R.color.holo_red_dark,
            android.R.color.holo_blue_dark,
            android.R.color.holo_orange_dark);    adapter = new ProgressOrderListAdapter(orderLists, getActivity());
    recyclerView.setAdapter(adapter);
    return recyclerView;

getprogressdata()函数&对您更有帮助
private void getProgressData(){
    String token = SharedPreferencesManager.readPreferenceString("token", "D/N");
    JSONObject progressData = new JSONObject();
    try{
        progressData.put("token", token);
        JsonObjectRequest progressObject = new JsonObjectRequest(1, Common.OrderDetails + "progress", progressData, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject progressResponse) {
                Log.d("Responseprogress", progressResponse.toString());
                try {
                    int status = progressResponse.getInt("status");
                    if(status == 1) {
                        progressOrderProgress(progressResponse);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                Log.d("Response", "PROGRESS ERROR");
            }
        });
        progressObject.setShouldCache(false);
        ServiceBellApp.getInstance().addToRequestQueue(progressObject);
    }
    catch (JSONException localJSONException){
        localJSONException.printStackTrace();
        return;
    }
}

private void progressOrderProgress(JSONObject progressResponse) throws JSONException {
    JSONArray result = progressResponse.getJSONArray("orderdata");
    for(int i=0; i<result.length(); i++){
        OrderList orderListModule = new OrderList();
        JSONObject orderData = null;
        try {
            orderData = result.getJSONObject(i);
            orderListModule.setPackage_name(orderData.getString("name"));
            orderListModule.setOrderdate(orderData.getString("date"));
            orderListModule.setServicedate(orderData.getString("service"));
            orderListModule.setServicetime(orderData.getString("time"));
            orderListModule.setOrderid(orderData.getString("id"));
            orderListModule.setOrdstatus(orderData.getString("status"));
            orderListModule.setOrderamount(orderData.getInt("ramount"));
        }catch (JSONException e) {
            e.printStackTrace();
        }
        orderLists.add(orderListModule);
    }
    swipeRefreshLayout.setRefreshing(false);
    adapter.notifyDataSetChanged();
}

这是我的密码。
我在logcat中没有任何错误,但它不起作用。
请帮我朋友们

最佳答案

 mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swiperefresh);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mArrayList = new ArrayList<CabsETA>();

                mSwipeRefreshLayout.post(new Runnable() {
                                             @Override
                                             public void run() {
                                                 mSwipeLayout = true;

                                                 mSwipeRefreshLayout.setRefreshing(true);

                                                 getNear();
                                             }
                                         }
                );

            }
        });





public void getNear() {

        latLongSevenKms();
        mSwipeRefreshLayout.setRefreshing(true);
}


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


        <android.support.v7.widget.RecyclerView
            android:id="@+id/listView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentBottom="true"
            android:layout_gravity="start"
            android:layout_marginTop="8dp"
            android:background="#f5f5f5"
            android:choiceMode="singleChoice"
            android:divider="@null"
            android:dividerHeight="0dp" />
    </android.support.v4.widget.SwipeRefreshLayout>

用法:linearlayoutmanager mm=新建linearlayoutmanager(this,linearlayoutmanager.vertical,false);
RecyclerView.SetLayoutManager(毫米);

10-06 01:46