SwipeRefresh无法在片段中工作?

时间:2016-07-04 06:11:22

标签: android android-fragments swiperefreshlayout

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()函数&amp;它对你更有帮助

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中得到任何错误,但它不起作用。 请帮帮我朋友

2 个答案:

答案 0 :(得分:4)

 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(毫米);

答案 1 :(得分:2)

理解这个基本流程你的代码中看到的所有内容都只是遗漏了一件事。

您提出请求获取新数据,然后将其添加到orderLists.add(orderListModule);,然后在此处致电adapter.notifyDataSetChanged(); 问题,您应在此处调用set adatpter并使用新数据集代替{ {1}}。

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

Full Demo Here.