我想在每次向下滑动时刷新RecyclerView
。所以我使用SwipeRefreshLayout
但现在我也无法刷新RecyclerView
。
下面是我写的代码:
下面是我的XML文件:fragment_static.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>
下面是我的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 :(得分:2)
SwipeRefresh不会自动刷新/重新加载。您需要在函数中添加一些逻辑。你不需要也不是一个好的实践使用延迟(它仅用于例子和测试)。
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();
}
});