SwipeRefreshLyout无法正常工作

时间:2016-08-25 08:12:37

标签: android android-fragments swiperefreshlayout

所以我有Fragment应该使用RecyclerView从Internet获取一些数据我想将刷卡刷新手势添加到我的片段中,以便用户可以查看是否有任何新数据是上 。 这是我的代码:

片段:

public class HomeFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener{


public HomeFragment() {
    // Required empty public constructor
}

RecyclerView recyclerView;
SwipeRefreshLayout swipeRefreshLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View view = inflater.inflate(R.layout.fragment_home, container, false);

    swipeRefreshLayout=(SwipeRefreshLayout) view.findViewById(R.id.swipeRefreshLayout);
    recyclerView = (RecyclerView) view.findViewById(R.id.recylerview);

    ReadRss readRss = new ReadRss(getContext(), recyclerView);
    readRss.execute();


    return view;
}


@Override
public void onRefresh() {

    new ReadRss(getContext(), recyclerView).execute();
    if(swipeRefreshLayout.isRefreshing())swipeRefreshLayout.setRefreshing(false);

}
}

XML布局:

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/swipeRefreshLayout"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.essat.essat.HomeFragment"
    >


    <!-- TODO: Update blank fragment layout -->
    <android.support.v7.widget.RecyclerView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/recylerview"
        />

>
</android.support.v4.widget.SwipeRefreshLayout>

问题在于SwipeRefreshLayout即使使用这段代码也永远不会停止转动:

if(swipeRefreshLayout.isRefreshing())swipeRefreshLayout.setRefreshing(false);

我希望有人知道如何解决这个问题,谢谢你:)

更新

public class ReadRss extends AsyncTask<Void, Void, Void> {
Context context;
String address = "http://www.sciencemag.org/rss/news_current.xml";
ProgressDialog progressDialog;
URL url;
ArrayList<FeedItem>feedItems;
RecyclerView recyclerView;


public ReadRss(Context context,RecyclerView recyclerView  ) {
    this.recyclerView=recyclerView;
    this.context = context;

    progressDialog = new ProgressDialog(context);
    progressDialog.setMessage("Loading...");
}

@Override
protected void onPreExecute() {
    progressDialog.show();
    super.onPreExecute();
}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    progressDialog.dismiss();
    ((IWithHttpRequest) context).onDataUpdate();
    Adapter adapter = new Adapter(context, feedItems);
    recyclerView.setLayoutManager(new LinearLayoutManager(context));
    recyclerView.addItemDecoration(new VerticalSpace(20));
    recyclerView.setAdapter(adapter);

}

@Override
protected Void doInBackground(Void... params) {
    ProcessXml(Getdata());
    return null;
}

private void ProcessXml(Document data) {
    if (data != null) {
        feedItems=new ArrayList<>();
        Element root = data.getDocumentElement();
        Node channel = root.getChildNodes().item(1);
        NodeList items = channel.getChildNodes();
        for (int i = 0; i < items.getLength(); i++) {
            Node cureentchild = items.item(i);
            if (cureentchild.getNodeName().equalsIgnoreCase("item")) {
                FeedItem item=new FeedItem();
                NodeList itemchilds = cureentchild.getChildNodes();
                for (int j = 0; j < itemchilds.getLength(); j++) {
                    Node cureent = itemchilds.item(j);
                    if (cureent.getNodeName().equalsIgnoreCase("title")){
                        item.setTitle(cureent.getTextContent());
                    }else if (cureent.getNodeName().equalsIgnoreCase("description")){
                        item.setDescription(cureent.getTextContent());
                    }else if (cureent.getNodeName().equalsIgnoreCase("pubDate")){
                        item.setPubDate(cureent.getTextContent());
                    }else if (cureent.getNodeName().equalsIgnoreCase("link")){
                        item.setLink(cureent.getTextContent());
                    }else if (cureent.getNodeName().equalsIgnoreCase("media:thumbnail")) {
                        String url = cureent.getAttributes().item(0).getTextContent();
                        item.setThumbnailUrl(url);
                    }
                }
                feedItems.add(item);
            }
        }
    }
}

public Document Getdata() {
    try {
        url = new URL(address);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        InputStream inputStream = connection.getInputStream();
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        Document xmlDoc = builder.parse(inputStream);
        return xmlDoc;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
}

片段已更新:

public class HomeFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener,IWithHttpRequest{


public HomeFragment() {
    // Required empty public constructor
}

RecyclerView recyclerView;
SwipeRefreshLayout swipeRefreshLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View view = inflater.inflate(R.layout.fragment_home, container, false);

    swipeRefreshLayout=(SwipeRefreshLayout) view.findViewById(R.id.swipeRefreshLayout);
    swipeRefreshLayout.setOnRefreshListener(this);
    recyclerView = (RecyclerView) view.findViewById(R.id.recylerview);

    ReadRss readRss = new ReadRss(getContext(), recyclerView);
    readRss.execute();


    return view;
}



@Override
public void onRefresh() {

    new ReadRss(getContext(), recyclerView).execute();


}


@Override
public void onDataUpdate() {
    swipeRefreshLayout.setRefreshing(false);
}
}

3 个答案:

答案 0 :(得分:1)

您在 onCreateView

中忘记了这一点
swipeRefreshLayout.setOnRefreshListener(this);

此外,new ReadRss(getContext(), recyclerView).execute();是异步的,或者至少应该是(不阻止UI线程),因此它看起来像是瞬间刷新。 这里最好的解决方案应该是创建一个接口

public interface IWithHttpRequest {
    void onDataUpdate()
}

让您的活动实施IWithHttpRequest

public class HomeFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener, IWithHttpRequest {
    ...
    @Override
    public void onDataUpdate() {
        swipeRefreshLayout.setRefreshing(false);
    }
}

并在ReadRss中,当您完成提取数据时

((IWithHttpRequest) context).onDataUpdate()

答案 1 :(得分:1)

为了刷新你应该这样做:

mSwipeRefreshLayout = (SwipeRefreshLayout) myFragmentView.findViewById(R.id.refreshContainer);
mSwipeRefreshLayout.setOnRefreshListener(new    SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
      // refresh logic
       if (mSwipeRefreshLayout.isRefreshing()) {
                mSwipeRefreshLayout.setRefreshing(false);
        }
}

您也可以尝试在recyclerview中清除您的数据 YourAdapter.clearData();

答案 2 :(得分:0)

您需要为setOnRefreshListener设置swipeRefreshLayout

swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefreshLayout);
swipeRefreshLayout.setOnRefreshListener(this);