2016-08-25 3 views
0

RecyclerViewを使用して一部のデータをフェッチすることになっているFragmentがあります。新しいデータがあるかどうかチェックできるようにスワイプリフレッシュジェスチャをフラグメントに追加します。 ここに私のコードです:SwipeRefreshLyoutが正しく機能していない

フラグメント:

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

私は誰かがこれを修正する方法を知っていることを願って、ありがとう:

UPDATE:更新

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); 
} 
} 
+0

swipeRefreshLayout.setOnRefreshListener(this); とonPostExecuteのコール swipeRefreshLayout.setRefreshing(false); – Rahul

答えて

1

あなたはまた、new ReadRss(getContext(), recyclerView).execute();が非同期である、またはそれは次のようになりますように、少なくとも、(はUIスレッドをブロックしない)でなければなりませんonCreateView

swipeRefreshLayout.setOnRefreshListener(this); 

でこれを忘れてしまいましたリフレッシュは瞬間的です。ここ 最善の解決策は、あなたの活動がIWithHttpRequest

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

、あなたがするために、データ呼

((IWithHttpRequest) context).onDataUpdate() 
+0

インターフェイスはフラグメントクラスの一部でなければなりませんか? と私はそれを正確に実装する必要がありますか?私のアクティビティ(私の異なるフラグメントの起動機能を提供する)または私のReadRssクラス(それはデータフェッチ機能を提供します)? –

+0

私は自分の答えを更新しました。インターフェイスは別のファイル(IWithHttpRequest)で宣言する必要があります。 –

+0

ReadRssはAsyncTaskを拡張し、3つのメソッド(onPreExecute、doInBackground、onPostExecute)をオーバーライドしました。あなたがonPostExecuteとdoInBackgroundの両方で私に与えた最後の行を通過しようとしました。 PS:データを取得するためにフラグメントが最初に起動されたときは、progressDialogを使用します。 –

0

あなたがswipeRefreshLayoutためsetOnRefreshListenerを設定する必要があります。

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

それは動作しません、私はまだ同じ問題があります! –

1

をフェッチ終えReadRss、中を実装してくださいインターフェイス

public interface IWithHttpRequest { 
    void onDataUpdate() 
} 

を作成するべきですリフレッシュする必要があります:

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

また、 YourAdapter.clearData();のようにrecyclerlerviewでデータを消去することもできます。

関連する問題