这个社区已经证明对我的个人和学校项目非常有帮助,所以我决定加入。这是我的第一个问题。目前我正在夏季的空闲时间构建一个Android应用程序。我试图在我的一个应用程序的片段中实现SwipeRefreshLayout,但由于未知的原因,当我拉动刷新时,我似乎无法让我的片段调用OnRefresh()方法。
这是我的Java代码:
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):
<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监视器上的结果。注意我的OnRefresh()中的Log.d语句没有被调用,尽管我在我的Android设备上刷新:
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
我知道有些人可能会提到,我知道Parse已接近终点。我正在使用Parse Server。我的解析服务器托管在Heroku上,而不是Parse.com。
提前感谢您的帮助。如果您想了解更多信息,请询问。
答案 0 :(得分:5)
在onCreateView中,您将返回与使用OnRefreshListener设置的视图不同的视图。请注意,您已经两次调用inflater.inflate(...)
,创建了两个单独的视图。试试这个:
@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;
}