gpt4 book ai didi

java - 我希望 RecyclerView 在滚动到结束时加载更多项目。我怎样才能得到这个?

转载 作者:行者123 更新时间:2023-11-28 23:12:20 25 4
gpt4 key购买 nike

一切都很好,直到我决定将数据库的第一行放在列表的顶部。

目前,我唯一的问题是我希望 RecyclerView 在滚动到结束时加载更多项目。

PHP

如何编写本节中的代码?

$sql= "SELECT * FROM movies_table ORDER BY id DESC";

这是完整的代码

<?php 
//Creating a connection
$con = mysqli_connect("--","---","----","-----");

/* change character set to utf8 */
$con->set_charset("utf8");

if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
/*Get the id of the last visible item in the RecyclerView from the request and store it in a variable. For the first request id will be zero.*/
$id = $_GET["id"];

$$sql= "SELECT * FROM movies_table ORDER BY id DESC";

$result = mysqli_query($con ,$sql);

while ($row = mysqli_fetch_assoc($result)) {

$array[] = $row;

}
header('Content-Type: text/html; charset=utf-8');

echo json_encode($array, JSON_UNESCAPED_UNICODE );

mysqli_free_result($result);

mysqli_close($con);

?>

.

.回收 View

.

.这部分的代码怎么写?

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {



}
});
return view;
}

和这个部分

for (int i = 0; i < array.length(); i++) {

JSONObject object = array.getJSONObject(i);

这是完整的代码

public class ThreeFragment extends Fragment{

private static final String TAG = ThreeFragment.class.getSimpleName();
private List<Movie> movies;
private RecyclerView recyclerView;
private GridLayoutManager gridLayout;
private MoviesAdapter adapter;

public ThreeFragment() {
// 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 view = inflater.inflate(R.layout.fragment_three, container, false);


recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
movies = new ArrayList<>();
getMoviesFromDB(0);

gridLayout = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(gridLayout);

adapter = new MoviesAdapter(getActivity(), movies);
recyclerView.setAdapter(adapter);

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {



}
});
return view;
}



public void fetchTimelineAsync(int page) {
// ...the data has come back, add new items to your adapter...

movies.clear();


movies = new ArrayList<>();

getMoviesFromDB(0);

gridLayout = new GridLayoutManager(getActivity(), 2);

recyclerView.setLayoutManager(gridLayout);

adapter = new MoviesAdapter(getActivity(), movies);

recyclerView.setAdapter(adapter);

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

if (gridLayout.findLastCompletelyVisibleItemPosition() == movies.size() - 1) {
getMoviesFromDB(movies.get(movies.size() - 1).getId());
}

}
});

adapter.notifyDataSetChanged();

swipeContainer.setRefreshing(false);
}





}private void getMoviesFromDB(int id) {

AsyncTask<Integer, Void, Void> asyncTask = new AsyncTask<Integer, Void, Void>() {



ProgressDialog pdLoading = new ProgressDialog(getActivity());

@Override
protected void onPreExecute() {
super.onPreExecute();

//this method will be running on UI thread
pdLoading.setMessage("\tدر حال بارگذاری...");
pdLoading.setCancelable(false);
pdLoading.show();

}


@Override
protected Void doInBackground(Integer... movieIds) {

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://astaneapp.ir/wp-php/movies.php?id=" + movieIds[0])
.build();
try {
Response response = client.newCall(request).execute();

JSONArray array = new JSONArray(response.body().string());

for (int i = 0; i < array.length(); i++) {

JSONObject object = array.getJSONObject(i);

Movie movie = new Movie(object.getInt("id"), object.getString("movie_name"),
object.getString("movie_image"), object.getString("movie_genre") , object.getString("movie_discription") , object.getString("movie_lat"), object.getString("movie_lon") , object.getString("movie_marker"));

ThreeFragment.this.movies.add(movie);
}


} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
adapter.notifyDataSetChanged();
pdLoading.dismiss();
}
};

asyncTask.execute(id);
}

最佳答案

我的项目中一直在使用这段代码:

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);

final int totalItemCount = mLinearLayoutManager.getItemCount();
final int visibleItemCount = mLinearLayoutManager.getChildCount();
final int firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();

if (!mLoadingStarted && (totalItemCount - visibleItemCount <= firstVisibleItem)) {
mLoadingStarted = true;

// fetch more data
}
}
});

关于java - 我希望 RecyclerView 在滚动到结束时加载更多项目。我怎样才能得到这个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45391052/

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