动态更改GridLayoutManager的SpanCount

 收藏

I'm trying to change SpanCount (The number of columns in the grid) of GridLayoutManager between 2 and 3.

setting SpanCount in onCreate()

 if (getActivity() != null)
            layoutManager = new GridLayoutManager(getActivity(), getPreferenceSpanCount(getActivity()));

get SpanCount saved using SharedPreferences and change Layout (row of RecyclerView item) depends of SpanCount :

private int getSpanCount() {
    if (getPreferenceSpanCount(getActivity()) == 2) {
        return R.layout.pic_items_two;
    } else {
        return R.layout.picture_item;
    }
}

get SpanCount saved using SharedPreferences :

public static Integer getPreferenceSpanCount(Context context) {
    SharedPreferences sharedPreferences = context.getSharedPreferences(SettingsActivity.SHARED_PREFS_SPAN_COUNT, MODE_PRIVATE);
    return sharedPreferences.getInt(SettingsActivity.KEY_SPAN_COUNT, 3);
}

set SpanCount when i get data:

private void refreshContent(Boolean setIsServerFilaire) {
    fetchRemoteData(new DataStatus() {
        @Override
        public void onSuccess(List<Post> posts) {
            loading.setVisibility(View.GONE);
            pb.setVisibility(View.GONE);
            if (adapter == null && getActivity() != null) {
                adapter = new MyAdapter(posts, getActivity(), new RecyclerViewClickListener() {
                    @Override
                    public void onClick(View view, Post post) {
                        Intent intent = new Intent(getActivity(), Pop.class);
                        intent.putExtra("img", post.getImgUrl());
                        intent.putExtra("cat", "Gamewave"); 
                        startActivity(intent);

                    }
                }, getSpanCount());
                recyclerView.setAdapter(adapter);
            } else {
                adapter.getItems().clear();
                adapter.getItems().addAll(posts);
                adapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onError(Exception e) {
            slowInternetConnection();
        }
    }, setIsServerFilaire);
}

一切正常,但是我在Google Play控制台中的ANR和崩溃下收到了很多报告:

java.lang.NullPointerException:在   maa.app.Fragments.fragmentAnime $ 3.onSuccess(fragmentAnime.java:87)   在maa.app.Fragments.fragmentAnime $ 6.a(fragmentAnime.java:52)在   maa.app.Fragments.fragmentAnime $ 6.onResponse(fragmentAnime.java:2)   在com.android.volley.request.StringRequest.c(StringRequest.java:4)   在com.android.volley.request.StringRequest.a(StringRequest.java:2)   在com.android.volley.ExecutorDelivery $ ResponseDeliveryRunnable.run   (ExecutorDelivery.java:30)位于android.os.Handler.handleCallback   (Handler.java:789)位于android.os.Handler.dispatchMessage   (Handler.java:98)位于android.os.Looper.loop(Looper.java:164)位于   android.app.ActivityThread.main(ActivityThread.java:6944)位于   java.lang.reflect.Method.invoke(Method.java)在   com.android.internal.os.Zygote $ MethodAndArgsCaller.run   (Zygote.java:327)在com.android.internal.os.ZygoteInit.main   (ZygoteInit.java:1374)
回复