samedi 23 mai 2015

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

I have StaggeredGridLayoutManager and LinearLayoutManager in recyclerView in my Fragment. I am trying to change layout from grid to linear or from linear to grid when I choose mode from settings. If I choose grid, view will change from linear to StaggeredGridLayoutManager. If I choose linear view will change from grid to linear.

Here is my onCreateView and onOptionsItemSelected functions:

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);
    }
}

I am changing mode from 1 to 0 or from 0 to 1 but view is not changing. How can I change layout one to another?

Thanks for help. Sorry for bad english.

Aucun commentaire:

Enregistrer un commentaire