簡體   English   中英

如何在Android的RecyclerView上修復addOnScrollListener的錯誤

[英]How to fix bug for addOnScrollListener on RecyclerView in Android

我想從服務器加載數據並顯示到我的應用程序( RecyclerView )中,對於此作業,當啟動應用程序時,我顯示10個帖子,滾動recyclerView顯示另一個帖子。 我寫下面的代碼,但當獲得10條帖子時,不加載其他帖子,並顯示“強制關閉”錯誤!

為了連接到互聯網,我使用Retrofit v2 ;對於recyclerView ,使用自定義的Endless方法,我使用此類: EndLess類

LogCat錯誤:

FATAL EXCEPTION: main
Process: com.tellfa.colony, PID: 25304
java.lang.ClassCastException: com.tellfa.colony.Retrofit.Model.Category.R_CatModelResponse cannot be cast to java.util.List
at com.tellfa.colony.Activities.Category_page$1$1.onResponse(Category_page.java:124)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5349)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

Api接口代碼:

public interface Retrofit_ApiInterface {

    // For load more category
    @GET("?json=get_category_posts")
    Call<R_CatModelResponse> getCatMoreResponse(@Query("id") Integer id, @Query("page") Integer page);
}

適配器代碼:(我將此代碼添加到了適配器中,以加載更多數據)

public void addNewItem(List<R_CatModel> newContent) {
    int start = this.mDateSet.size();//contents is a List of your items initialize it your constructor
    int end = newContent.size();
    mDateSet.addAll(newContent);
    notifyItemRangeInserted(start + 1, end);
}

活動代碼:

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

        // Hide StatusBar color
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

        // Initializing
        context = Category_page.this;
        toolbar = (Toolbar) findViewById(R.id.category_toolbar);
        cat_recyclerView = (RecyclerView) findViewById(R.id.category_recycler);
        toolbar_title = (TextView) toolbar.findViewById(R.id.toolbar_pages_title);
        mLayoutManager = new LinearLayoutManager(this);
        root = (RelativeLayout) findViewById(R.id.category_root);
        loadLayout = (RelativeLayout) findViewById(R.id.category_empty_layout);
        checkNetLayout = (RelativeLayout) findViewById(R.id.category_checkInternet_layout);
        categoryCheckNet_button = (Button) checkNetLayout.findViewById(R.id.checkNet_button);
        // Toolbar
        setSupportActionBar(toolbar);
        if (toolbar != null) {
            getSupportActionBar().setTitle("");
        }

        // Receive Data
        bundle = getIntent().getExtras();
        catID = bundle.getInt("categoryID");
        if (bundle != null) {
            catTitle = bundle.getString("categoryTitle");
        }
        if (catTitle != null) {
            toolbar_title.setText(catTitle);
        }

        // Load Data
        loadData();

        // Load Progress
        loadLayout.setVisibility(View.VISIBLE);

        // RecyclerView
        cat_recyclerView.setLayoutManager(mLayoutManager);
        cat_recyclerView.setHasFixedSize(true);
        cat_recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(mLayoutManager) {
            @Override
            public void onLoadMore(int current_page) {

                Retrofit_ApiInterface apiInterface = Retrofit_ApiClient.getClient().create(Retrofit_ApiInterface.class);
                Call<R_CatModelResponse> call = apiInterface.getCatMoreResponse(catID, current_page);

                call.enqueue(new Callback<R_CatModelResponse>() {
                    @Override
                    public void onResponse(Call<R_CatModelResponse> call, Response<R_CatModelResponse> response) {

                        if (response != null) {
                            mAdapter.addNewItem((List<R_CatModel>) response.body());
                            //loadLayout.setVisibility(View.GONE);
                        } else {
                            //loadLayout.setVisibility(View.VISIBLE);
                            Toast.makeText(Category_page.this, "Error 2", Toast.LENGTH_SHORT).show();
                            TastyToast.makeText(context, "خطايي رخ داده است", TastyToast.LENGTH_LONG, TastyToast.ERROR);
                        }
                    }

                    @Override
                    public void onFailure(Call<R_CatModelResponse> call, Throwable t) {

                    }
                });
            }
        });

    }

    private void loadData() {
        boolean isConnected = ConnectivityReceiver.isConnected();

        retrofitData(isConnected);
    }

    private void retrofitData(boolean isConnect) {

        if (isConnect) {
            Retrofit_ApiInterface apiInterface = Retrofit_ApiClient.getClient().create(Retrofit_ApiInterface.class);
            Call<R_CatModelResponse> call = apiInterface.getCatResponse(catID);

            call.enqueue(new Callback<R_CatModelResponse>() {
                @Override
                public void onResponse(Call<R_CatModelResponse> call, Response<R_CatModelResponse> response) {

                    if (response != null) {
                        models = response.body().getCat_posts();

                        mAdapter = new CategoryAdapter(context, cat_recyclerView, models);
                        cat_recyclerView.setAdapter(mAdapter);
                        loadLayout.setVisibility(View.GONE);

                    } else {
                        //loadLayout.setVisibility(View.VISIBLE);
                        Toast.makeText(Category_page.this, "Error 2", Toast.LENGTH_SHORT).show();
                        TastyToast.makeText(context, "خطايي رخ داده است", TastyToast.LENGTH_LONG, TastyToast.ERROR);
                    }

                    checkNetLayout.setVisibility(View.GONE);
                }

                @Override
                public void onFailure(Call<R_CatModelResponse> call, Throwable t) {

                    //loadLayout.setVisibility(View.VISIBLE);
                    //TastyToast.makeText(context, "لطفا برنامه را مجددا باز کنيد", TastyToast.LENGTH_LONG, TastyToast.ERROR);
                    Toast.makeText(Category_page.this, "Error 2", Toast.LENGTH_SHORT).show();
                    //Cat_EmptyLayout.setVisibility(View.VISIBLE);
                    Log.e("CatResponseError", "Error : " + t);

                }
            });
        } else {
            //loadLayout.setVisibility(View.GONE);
            checkNetLayout.setVisibility(View.VISIBLE);
            if (mAdapter != null) {
                mAdapter.clear();
                cat_recyclerView.setAdapter(mAdapter);
            }
            categoryCheckNet_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    loadData();
                }
            });
        }
    }

}

如何解決上述問題並通過上述代碼在recyclerView設置loadMore數據? 請幫助我親愛的朋友。 我真的需要您的幫助<3

問題是response.body()R_CatModelResponse對象,但是您將其強制轉換為List<R_CatModel>

如果R_CatModelResponse具有一個以R_CatModel列表響應的方法(例如getCatModelList() ),那么您只需要使用該方法:

mAdapter.addNewItem(response.body().getCatModelList());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM