gpt4 book ai didi

android - onRefresh Android SwipeRefreshLayout 不调用我的方法

转载 作者:行者123 更新时间:2023-11-29 15:41:46 24 4
gpt4 key购买 nike

我正在学习 android 滑动刷新布局。我在 swipeRefreshLayout“setRefreshing(true)”上发布了 runabble,然后在 onRefresh 方法上我写了 fetchMovie()。但是 fetchMovie() 没有调用。如何解决?

package com.example.zihadrizkyef.belajarswiperefresh;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.example.zihadrizkyef.belajarswiperefresh.app.MyApplication;
import com.example.zihadrizkyef.belajarswiperefresh.helper.Movie;
import com.example.zihadrizkyef.belajarswiperefresh.helper.SwipeListAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
private String TAG = MainActivity.class.getSimpleName();
private String URL_TOP_250 = "http://api.androidhive.info/json/imdb_top_250.php?offset=";
private SwipeRefreshLayout swipeRefreshLayout;
private ListView listView;
private SwipeListAdapter adapter;
private List<Movie> movieList;
private int offSet = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listView = (ListView) findViewById(R.id.listView);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);

movieList = new ArrayList<Movie>();
adapter = new SwipeListAdapter(this, movieList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "aaaaaaaaaa", Toast.LENGTH_SHORT).show();
}
});

swipeRefreshLayout.setOnRefreshListener(this);

swipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(true);
}
});
}

@Override
public void onRefresh() {
fetchMovie();
}

private void fetchMovie() {
swipeRefreshLayout.setRefreshing(true);
String url = URL_TOP_250+offSet;

JsonArrayRequest req = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
System.out.println(TAG+response.toString());

if (response.length() > 0) {
for (int i=0; i<=response.length(); i++) {
try {
JSONObject movieObject = response.getJSONObject(i);
int rank = movieObject.getInt("rank");
String title = movieObject.getString("title");

Movie movie = new Movie(rank, title);
movieList.add(0, movie);

if (rank > offSet) {
offSet = rank;
}
} catch (JSONException e) {
System.out.println(TAG + " : JSON Parsing error : "+e.getMessage());
}
}
adapter.notifyDataSetChanged();
}
swipeRefreshLayout.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println(TAG+" : Server error : "+error.getMessage());
Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
swipeRefreshLayout.setRefreshing(false);
}
});

MyApplication.getInstance().addToRequestQueue(req);
}
}

最佳答案

如有关SwipeRefreshLayout 的文档中所示:

If an activity wishes to show just the progress animation, it should call setRefreshing(true).

调用 setRefreshing() 不会调用 OnRefreshListener。你可以在它之后调用 fetchMovie() :

swipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(true);
fetchMovie();
}
});

相关文档:https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html

关于android - onRefresh Android SwipeRefreshLayout 不调用我的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39229611/

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