SwipeRefresh 在片段中不起作用?

SwipeRefresh not working in fragment?

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 = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);

recyclerView.setLayoutManager(mm);

理解这个基本流程在你的代码中看到了什么你已经完成了所有的事情只是遗漏了一件事。

您请求获取新数据然后将其添加到 orderLists.add(orderListModule); 然后调用 adapter.notifyDataSetChanged(); 这里是 问题 您应该在这里调用设置适配器新数据集而不是 notifyDataSetChanged().

@Override
  public void onCreate(Bundle savedInstanceState) {
  ...
    listView.setAdapter();
    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
      @Override
      public void onRefresh() {
            refreshContent();
          ...
  }

  // fake a network operation's delayed response 
  // this is just for demonstration, not real code!
  private void refreshContent(){ 
  new Handler().postDelayed(new Runnable() {
          @Override
          public void run() {
    mAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, getNewTweets());
    mListView.setAdapter(mAdapter);
    mSwipeRefreshLayout.setRefreshing(false);
  });
  }

  // get new cat names.
  // Normally this would be a call to a webservice using async task,
  // or a database operation

 private List<String> getNewCatNames() {
        List<String> newCatNames = new ArrayList<String>();
        for (int i = 0; i < mCatNames.size(); i++) {
          int randomCatNameIndex = new Random().nextInt(mCatNames.size() - 1);
          newCatNames.add(mCatNames.get(randomCatNameIndex));
        }
        return newCatNames;
    }

更新:在您的代码中

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 = new ProgressOrderListAdapter(orderLists, getActivity());
    recyclerView.setAdapter(adapter);
}

Full Demo Here.