连续重复执行addOnScrollListener函数吗?

时间:2018-12-20 15:08:09

标签: java android

我尝试执行我创建的任务分页,但是不知道设备功能上是否显示了正确或不正确的代码,所以我关闭应用程序时试图清除代码并且不继续打印是并按住设备上的字样

enter image description here

查看我的代码:

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerview;
    PostsAdapter adapter;
    Retrofit retrofit;
    int p;
    private List<Post> itemList;
    private boolean itShouldLoadMore = true;
    // private EndlessRecyclerViewScrollListener scrollListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerview = (RecyclerView) findViewById(R.id.recycler_view);
        p = 1;
        retrofit = new Retrofit.Builder().baseUrl("http://192.168.1.3/Pagination/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //Rtrive1(p);
        itemList = new ArrayList<>();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerview.setLayoutManager(linearLayoutManager);
        adapter = new PostsAdapter(itemList, MainActivity.this);
        recyclerview.setAdapter(adapter);
        recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManager llManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                if (dy > 0) {
                    //  adapter.showLoading();
                    Toast.makeText(MainActivity.this, "Yes " + p, Toast.LENGTH_LONG).show();
                    // itShouldLoadMore = false;
                    p++;
                    Rtrive2(p);
                }
            }
        });
        Rtrive2(p);
    }

    public void Rtrive2(int page) {
        itShouldLoadMore = false;
        retrofit.create(API.class).getPosts(page).enqueue(new Callback<List<Post>>() {
            @Override
            public void onResponse(Call<List<Post>> call, Response<List<Post>> response) {
                // final List<Post> postList = response.body();
                itemList.addAll(response.body());
                adapter.notifyItemRangeInserted(adapter.getItemCount(), itemList.size() - 1);
                Log.i("INFO_", itemList.get(itemList.size() - 1).getPost_writer());
                // itShouldLoadMore = true;
            }

            @Override
            public void onFailure(Call<List<Post>> call, Throwable t) {
                Log.i("Error", t.toString());
            }
        });
    }


    public interface API {
        //  @GET("view.php")
        //  Call<List<Post>>getPosts();
        @GET("vPage.php")
        Call<List<Post>> getPosts(@Query("page") int sort);
    }
}

0 个答案:

没有答案