2016-06-16 11 views
2

このコミュニティは私の個人的なプロジェクトや学校のプロジェクトにとって非常に有益であることが証明されているので、私は参加することに決めました。これは私の最初の質問です。現在、私は夏に私の自由な時間にAndroidアプリケーションを構築しています。私はアプリケーションのフラグメントの1つにSwipeRefreshLayoutを実装しようとしていますが、未知の理由で、リフレッシュするためにプルするときにフラグメントがOnRefresh()メソッドを呼び出せないようです。Android:SwipeRefreshLayoutはOnRefresh()を呼び出さない

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

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/rv" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

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

そして、ここでは私のAndroidのモニター上の結果である:

import android.app.Fragment; 
import android.content.Context; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.v4.widget.SwipeRefreshLayout; 
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

import com.parse.FindCallback; 
import com.parse.ParseException; 
import com.parse.ParseObject; 
import com.parse.ParseQuery; 

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 

public class Notifications extends Fragment implements OnRefreshListener{ 
    private OnFragmentInteractionListener mListener; 
    private RecyclerView mRecyclerView; 
    private RecyclerView.Adapter mAdapter; 
    private RecyclerView.LayoutManager mLayoutManager; 
    private SwipeRefreshLayout swipeLayout; 
    private View view; 

    public Notifications() { 

    } 

    public static Notifications newInstance() { 
     Notifications fragment = new Notifications(); 
     Bundle args = new Bundle(); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     accessDataBase(); 
     view = inflater.inflate(R.layout.fragment_notifications, container, false); 
     swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeContainer); 
     swipeLayout.setOnRefreshListener(this); 
     swipeLayout.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); 
     return inflater.inflate(R.layout.fragment_notifications, container, false); 

    } 

    @Override 
    public void onRefresh() { 
     Log.d("Notifications", "onRefresh called from SwipeRefreshLayout"); 
    } 

    // Used to access my Parse Server database 
    public void accessDataBase() { 
     ParseQuery query = new ParseQuery("OurPushes"); 
     query.setCachePolicy(ParseQuery.CachePolicy.CACHE_THEN_NETWORK); 
     query.findInBackground(new FindCallback<ParseObject>() { 
      public void done(List<ParseObject> objects, ParseException e) { 
       if (e == null) { 
        List<String> subject = new ArrayList<>(); 
        List<String> body = new ArrayList<>(); 
        for (ParseObject pushObject : objects) { 
         if(pushObject.getString("title") != "") 
          subject.add(pushObject.getString("title")); 
         else 
          subject.add(""); 
         if(pushObject.getString("alert") != "") 
          body.add(pushObject.getString("alert")); 
         else 
          body.add(""); 
        } 

        Collections.reverse(subject); 
        Collections.reverse(body); 

        if(getActivity() != null) { 
         mAdapter = new CustomAdapter(subject, body); 
         mRecyclerView = (RecyclerView) getActivity().findViewById(R.id.rv); 
         mLayoutManager = new LinearLayoutManager(getActivity()); 
         mRecyclerView.setLayoutManager(mLayoutManager); 
         mRecyclerView.setAdapter(mAdapter); 
        } 

       } 
       else { 
        Log.d("Brand", "Error: " + e.getMessage()); 
       } 
      } 
     }); 
    } 

    public void onButtonPressed(Uri uri) { 
     if (mListener != null) { 
      mListener.onFragmentInteraction(uri); 
     } 
    } 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     if (context instanceof OnFragmentInteractionListener) { 
      mListener = (OnFragmentInteractionListener) context; 
     } else { 
      throw new RuntimeException(context.toString() 
        + " must implement OnFragmentInteractionListener"); 
     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mListener = null; 
    } 

    public interface OnFragmentInteractionListener { 
     void onFragmentInteraction(Uri uri); 
    } 
} 

はここに私のXMLコード(fragment_notifications.xml)です:

は、ここに私のJavaコードです。私は、解析が終了寿命に近づいている知っている、

06-16 17:57:13.022 2668-2668/xxxxxxx.xxxxxx W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 
06-16 17:57:13.314 2668-2718/xxxxxxx.xxxxxx D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true 
06-16 17:57:13.317 2668-2668/xxxxxxx.xxxxxx D/Atlas: Validating map... 
06-16 17:57:13.353 2668-2718/xxxxxxx.xxxxxx I/Adreno: EGLInit: QTI Build: 03/04/15, eeab148, 
06-16 17:57:13.365 2668-2718/xxxxxxx.xxxxxx I/OpenGLRenderer: Initialized EGL, version 1.4 
06-16 17:57:13.366 2668-2718/xxxxxxx.xxxxxx I/Adreno: GetNativeFormatFromQctPixelFormat: Invalid qct format (611) 
06-16 17:57:13.366 2668-2718/xxxxxxx.xxxxxx I/Adreno: GetNativeFormatFromQctPixelFormat: Invalid qct format (611) 
06-16 17:57:13.366 2668-2718/xxxxxxx.xxxxxx I/Adreno: GetNativeFormatFromQctPixelFormat: Invalid qct format (611) 
06-16 17:57:13.366 2668-2718/xxxxxxx.xxxxxx I/Adreno: GetNativeFormatFromQctPixelFormat: Invalid qct format (611) 
06-16 17:57:13.366 2668-2718/xxxxxxx.xxxxxx I/Adreno: GetNativeFormatFromQctPixelFormat: Invalid qct format (612) 
06-16 17:57:13.366 2668-2718/xxxxxxx.xxxxxx I/Adreno: GetNativeFormatFromQctPixelFormat: Invalid qct format (612) 
06-16 17:57:13.366 2668-2718/xxxxxxx.xxxxxx I/Adreno: GetNativeFormatFromQctPixelFormat: Invalid qct format (612) 
06-16 17:57:13.366 2668-2718/xxxxxxx.xxxxxx I/Adreno: GetNativeFormatFromQctPixelFormat: Invalid qct format (612) 
06-16 17:57:13.373 2668-2718/xxxxxxx.xxxxxx D/OpenGLRenderer: Enabling debug mode 0 
06-16 17:57:13.394 2668-2668/xxxxxxx.xxxxxx E/RecyclerView: No adapter attached; skipping layout 
06-16 17:57:13.445 2668-2719/xxxxxxx.xxxxxx I/OpenGLRenderer: WorkerThread::readyToRun:hwuiTask1 
06-16 17:58:46.962 2668-4140/xxxxxxx.xxxxxx I/OpenGLRenderer: WorkerThread::readyToRun:hwuiTask2 

と私はあなたのいくつかは言及して知っている:私は私のAndroidデバイス上でリフレッシュするために引っ張ったにもかかわらず呼び出されなかったOnRefresh内の私のLog.d文を()に注意してください。私はParse Serverを使用しています。私の解析サーバはParse.comではなく、Herokuでホストされています。

ご協力いただきありがとうございます。追加情報をご希望の場合は、お問い合わせください。

+0

ちょっと男性 'pushObject.getString( "タイトル")= ""'これは動作しない..コンパ文字列uが対等必要があるので、( "" yourtextを)...優れている使用のisEmptyあれば! () – marlonpya

答えて

2

OnCreateViewでは、OnRefreshListenerで設定したビューとは別のビューを返します。 inflater.inflate(...)を2回呼び出して、2つの別々のビューを作成したことに注目してください。代わりにこれを試してみてください:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    accessDataBase(); 
    view = inflater.inflate(R.layout.fragment_notifications, container, false); 
    swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeContainer); 
    swipeLayout.setOnRefreshListener(this); 
    swipeLayout.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); 
    return view; 

} 
+0

どのように簡単です。それは働いて、ありがとう! – jwf5426

関連する問題