RecyclerView - 交替布局

时间:2016-09-07 02:31:56

标签: java android android-recyclerview

我很陌生,忍受我。这是我的简单RecyclerView的代码。我想在LayoutManager中交替使用StaggeredGridLayout和LinearLayout。

我是如何启动这两种布局的。

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

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    recyclerView.setHasFixedSize(true);

    staggeredGrid = new StaggeredGridLayoutManager(2, 1);
    cardView = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(staggeredGrid);

    List<ItemObjects> staggeredList = getListItemData();

    RecyclerViewAdapter adapter = new RecyclerViewAdapter(MainActivity.this, staggeredList);
    recyclerView.setAdapter(adapter);
}

我现在用一个简单的按钮尝试在它们之间进行交替。我不知道所有的代码是否都是必要的,但我被告知每次切换视图并重建它时,我最好擦除/ null适配器。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.view_settings) {
        if (recyclerView.getLayoutManager() == staggeredGrid) {
            recyclerView.setAdapter(null);
            recyclerView.setLayoutManager(cardView);
            recyclerView.setAdapter(adapter);
            return true;
        }

        if (recyclerView.getLayoutManager() == cardView) {
            recyclerView.setAdapter(null);
            recyclerView.setLayoutManager(staggeredGrid);
            recyclerView.setAdapter(adapter);
            return true;
        }

    }

    return super.onOptionsItemSelected(item);
}

错误。

09-06 22:17:37.769 11853-11853/com.example.wsdw E/AndroidRuntime: FATAL EXCEPTION: main
                                                              Process: com.example.wsdw, PID: 11853
                                                              java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v7.widget.RecyclerView$LayoutManager android.support.v7.widget.RecyclerView.getLayoutManager()' on a null object reference
                                                                  at com.example.wsdw.MainActivity.onOptionsItemSelected(MainActivity.java:50)
                                                                  at android.app.Activity.onMenuItemSelected(Activity.java:2908)
                                                                  at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:406)
                                                                  at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)
                                                                  at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:103)
                                                                  at android.support.v7.app.AppCompatDelegateImplV9.onMenuItemSelected(AppCompatDelegateImplV9.java:667)
                                                                  at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:810)
                                                                  at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
                                                                  at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:957)
                                                                  at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:947)
                                                                  at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:616)
                                                                  at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:153)
                                                                  at android.view.View.performClick(View.java:5198)
                                                                  at android.view.View$PerformClick.run(View.java:21147)
                                                                  at android.os.Handler.handleCallback(Handler.java:739)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                  at android.os.Looper.loop(Looper.java:148)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

对于我所知道的一切,我本可以做到这一点完全错误,但如果有人能提供帮助,我正在寻找一个起点。拜托,谢谢你。

1 个答案:

答案 0 :(得分:0)

 RecyclerView recyclerView;
//Your Activity Name
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    recyclerView.setHasFixedSize(true);

    staggeredGrid = new StaggeredGridLayoutManager(2, 1);
    cardView = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(staggeredGrid);

    List<ItemObjects> staggeredList = getListItemData();

    RecyclerViewAdapter adapter = new RecyclerViewAdapter(MainActivity.this, staggeredList);
    recyclerView.setAdapter(adapter);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.view_settings) {
        if (recyclerView.getLayoutManager() == staggeredGrid) {
            recyclerView.setAdapter(null);
            recyclerView.setLayoutManager(cardView);
            recyclerView.setAdapter(adapter);
            adapter. notifyDataSetChanged();
            return true;
        }

        if (recyclerView.getLayoutManager() == cardView) {
            recyclerView.setAdapter(null);
            recyclerView.setLayoutManager(staggeredGrid);
            recyclerView.setAdapter(adapter);
             adapter. notifyDataSetChanged();
            return true;
        }

    }

    return super.onOptionsItemSelected(item);
}