如何在 Fragment 中从 LinearLayoutManager 更改为 StaggeredGridLayoutManager--Android

How to Change from LinearLayoutManager to StaggeredGridLayoutManager in Fragment--Android

我的 Fragment 中的 RecyclerView 中有 StaggeredGridLayoutManagerLinearLayoutManager。当我从设置中选择模式时,我试图将布局从 grid 更改为 linear 或从 linear 更改为 grid。如果我选择 grid,视图将从 linear 变为 StaggeredGridLayoutManager。如果我选择 linear,视图将从 grid 变为 linear

这是我的 onCreateViewonOptionsItemSelected 函数:

public class NewsFragment extends Fragment{     

    private int previousTotal = 0;
    private boolean loading = true;
    private int visibleThreshold = 5;
    int firstVisibleItem, visibleItemCount, totalItemCount;
    int mode=1;

    ImageLoader imageLoader;
    DisplayImageOptions options;
    RecyclerAdapter mAdapter;
    ProgressBar progressBar;
    View rootView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        new JSONGetNews().execute();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.fragment_news, container, false);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        final StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);

        RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));

        switch (mode) {
            case 0:
                recyclerView.setLayoutManager(linearLayoutManager);
                break;
            case 1:
                recyclerView.setLayoutManager(gridLayoutManager);
                break; 
        }

        mAdapter = new RecyclerAdapter(getActivity(),itemsData);
        recyclerView.setAdapter(mAdapter);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar);

        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

                super.onScrolled(recyclerView, dx, dy);
                switch (mode){
                    case 0:
                        visibleItemCount = linearLayoutManager.getChildCount();
                        totalItemCount = linearLayoutManager.getItemCount();
                        firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
                        break;
                    case 1:
                        visibleItemCount = gridLayoutManager.getChildCount();
                        totalItemCount = gridLayoutManager.getItemCount();
                        progressBar.setVisibility(View.INVISIBLE);
                        break;
                }

                if (loading) {
                    if (totalItemCount > previousTotal) {
                        loading = false;
                        previousTotal = totalItemCount;
                        progressBar.setVisibility(View.VISIBLE);
                    }
                }
                if (!loading && (totalItemCount - visibleItemCount)
                <= (firstVisibleItem + visibleThreshold)) {

                    first=first+4;
                    last=last+4;

                    new JSONGetNews().execute();
                    progressBar.setVisibility(View.GONE);
                    loading = true;
                }
            }
        });

        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(getActivity()));

        options = new DisplayImageOptions.Builder()
        .showStubImage(R.drawable.uranus)
        .showImageForEmptyUri(R.drawable.earth)
        .cacheOnDisc()
        .cacheInMemory()
        .build();


        return rootView;
    }



    public boolean onOptionsItemSelected(MenuItem item) {

        if(item.getItemId() == R.id.grid_view){
            mode=1; 
        }

        if(item.getItemId() == R.id.linear_view){
            mode=0; 
        }

        return super.onOptionsItemSelected(item);
    }
}

我正在将模式从 1 更改为 0 或从 0 更改为 1,但视图没有更改。如何将一个布局更改为另一个布局?

感谢您的帮助。

您只是在更改变量中的整数值。没有人调用 onCreateView(这是正确的),更重要的是没有人设置不同的布局管理器。以下应该对您有所帮助(稍作改动):

public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId() == R.id.grid_view) {
        if (mRecyclerView.getLayoutManager() == mGridLayoutManger) return true; // nothing to do
        mRecyclerView.setAdapter(null); // clear recycler view before tampering with its layout manager
        mRecyclerView.setLayoutManager(mGridLayoutManager); // set appropriate layout manager
        mRecyclerView.setAdapter(mAdapter); // reattach adapter
        return true;
    }

    if(item.getItemId() == R.id.linear_view) {
        // ditto with mLinearLayoutManager
        return true;
    }

    return super.onOptionsItemSelected(item);
}