0

我想每刷一次刷新RecyclerView。所以我使用SwipeRefreshLayout,但現在我也無法刷新我的RecyclerView。 下面是我寫的代碼:在Android中執行刷新刷新RecyclerView

下面被命名爲我的XML文件:

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.v4.widget.SwipeRefreshLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:id="@+id/container" 
    android:layout_height="match_parent"> 

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

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

下面fragment_static.xml是我的java文件名爲:StaticFragment.java

public class StaticFragment extends Fragment { 
    private SwipeRefreshLayout mSwipeRefreshLayout; 
    RecyclerView recyclerView ; 
    private List<String> fileList = new ArrayList<String>(); 
    private FileAdapter mAdapter ; 

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

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

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View v = inflater.inflate(R.layout.fragment_static, container, false); 

     mSwipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.container); 
     mSwipeRefreshLayout.setColorSchemeColors(Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW); 
     mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
      @Override 
      public void onRefresh() { 
       Toast.makeText(getContext(), "Refresh", Toast.LENGTH_SHORT).show(); 
       new Handler().postDelayed(new Runnable() { 
        @Override 
        public void run() { 
         Fragment frg = null; 
         frg = getFragmentManager().findFragmentByTag("Marker"); 
         FragmentTransaction ft = getFragmentManager().beginTransaction(); 
         ft.detach(frg); 
         ft.attach(frg); 
         ft.commit(); 
         mSwipeRefreshLayout.setRefreshing(false); 
        } 
       }, 2000); 
      } 
     }); 


     recyclerView = (RecyclerView)v.findViewById(R.id.recycler_view); 
     mAdapter = new FileAdapter(fileList); 
     RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getContext().getApplicationContext()); 

     recyclerView.setLayoutManager(mLayoutManager); 
     recyclerView.setItemAnimator(new DefaultItemAnimator()); 
     recyclerView.setAdapter(mAdapter); 

     recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getContext(), recyclerView, new ClickListener() { 

      @Override 
      public void onClick(View view, int position) { 
       Intent i = new Intent(getContext(),GraphActivityStatic.class); 
       i.putExtra("file_name",fileList.get(position)); 
       startActivity(i); 
      } 

      @Override 
      public void onLongClick(View view, int position) { 

      } 
     })); 

    prepareFileData(); 
    return v; 
} 

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

private void prepareFileData() { 
    File dir = new File(Environment.getExternalStorageDirectory(),"/PulseData/"); 
    if(!dir.exists()) 
     dir.mkdir(); 
    int position = 0; 
    fileList.clear(); 
    for (File f : dir.listFiles()) { 
     if (f.isFile() && f.toString().contains(".txt")) { 
      fileList.add(f.getName()); 
      mAdapter.notifyItemInserted(position++); 
     } 
    } 

} 



public interface ClickListener { 
    void onClick(View view, int position); 

    void onLongClick(View view, int position); 
} 

public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener { 

    private GestureDetector gestureDetector; 
    private StaticFragment.ClickListener clickListener; 

    public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final StaticFragment.ClickListener clickListener) { 
     this.clickListener = clickListener; 
     gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { 
      @Override 
      public boolean onSingleTapUp(MotionEvent e) { 
       return true; 
      } 

      @Override 
      public void onLongPress(MotionEvent e) { 
       View child = recyclerView.findChildViewUnder(e.getX(), e.getY()); 
       if (child != null && clickListener != null) { 
        clickListener.onLongClick(child, recyclerView.getChildPosition(child)); 
       } 
      } 
     }); 
    } 

    @Override 
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { 

     View child = rv.findChildViewUnder(e.getX(), e.getY()); 
     if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) { 
      clickListener.onClick(child, rv.getChildPosition(child)); 
     } 
     return false; 
    } 

    @Override 
    public void onTouchEvent(RecyclerView rv, MotionEvent e) { 
    } 

    @Override 
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { 

    } 
} 
} 

據給我錯誤:

FATAL EXCEPTION: main 
    java.lang.NullPointerException 
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:726) 
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617) 
    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517) 
    at android.os.Handler.handleCallback(Handler.java:605) 
    at android.os.Handler.dispatchMessage(Handler.java:92) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:4424) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584) 
    at dalvik.system.NativeStart.main(Native Method) 
+0

你只是想在刷卡時顯示Toast消息嗎? – Adley

+0

@Adley否我想在刷新後顯示它 – Lok

回答

2

SwipeRefresh不刷新/重裝自動ATIC。你需要在你的函數中添加一些邏輯。你不需要,也不是一個很好的實踐使用延遲(它僅用於例子和測試)。

mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
      @Override 
      public void onRefresh() { 
       // Do some logic 
       // if (refresh is ok) 
       Toast.makeText(getContext(), "Refresh", Toast.LENGTH_SHORT).show(); 
      } 
     }); 

例如,你可以把你要重裝綁定內,並調用內部SwipeListner綁定的所有代碼:

function void bindMyReclyclerView(){ 
// bind here your recyclerView 
} 

mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
       @Override 
       public void onRefresh() { 
        bindMyRecyclerView(); 
        Toast.makeText(getContext(), "Refresh", Toast.LENGTH_SHORT).show(); 
       } 
      }); 

我已經做了我的應用程序有些相像,採取看This class on GitHub

您也可以重新加載所有活動,但我不推薦! (但你可以用於測試)

mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
        @Override 
        public void onRefresh() { 
         finish(); 
         startActivity(getIntent()); 
         Toast.makeText(getContext(), "Refresh", Toast.LENGTH_SHORT).show(); 
        } 
       }); 
+0

你能告訴我該如何刷新我的回收視圖,因爲我使用Fragment frg = null; frg = getFragmentManager()。findFragmentByTag(「Marker」); FragmentTransaction ft = getFragmentManager()。beginTransaction(); ft.detach(frg); ft.attach(frg); ft.commit();但它並沒有讓我的回收者看起來更清爽。 – Lok

+0

首先不要使用新的Handler()。postDelayed(new Runnable(){}會導致它凍結主線程,你不一定需要重構你的片段,你只需要重新綁定你的recyclerview。並重新綁定,你可以使用與你先使用的代碼相同的代碼)很難說如何做到這一點,因爲它是關於邏輯的,爲此我需要了解你的所有代碼以及你想做什麼。 – Adley