gpt4 book ai didi

java - Android:SwipeRefreshLayout 不会调用 OnRefresh()

转载 作者:数据小太阳 更新时间:2023-10-29 02:56:09 24 4
gpt4 key购买 nike

事实证明,这个社区对我的个人和学校项目非常有帮助,所以我决定加入。这是我的第一个问题。目前,我正在暑假空闲时间构建一个 Android 应用程序。我正在尝试在我的应用程序 fragment 之一中实现 SwipeRefreshLayout,但由于未知原因,当我拉动刷新时,我似乎无法让我的 fragment 调用 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 即将结束。我正在使用解析服务器。我的解析服务器托管在 Heroku 上,而不是 Parse.com。

在此先感谢您的帮助。如果您需要任何其他信息,请询问。

最佳答案

在 onCreateView 中,您将返回一个与您使用 OnRefreshListener 设置的 View 不同的 View 。请注意,您调用了 inflater.inflate(...) 两次,创建了两个单独的 View 。试试这个:

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

}

关于java - Android:SwipeRefreshLayout 不会调用 OnRefresh(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37870096/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com