如何使用列表更改图标到网格

时间:2016-07-25 08:15:22

标签: android android-recyclerview staggered-gridview linearlayoutmanager

我正在使用StaggeredGridlayoutmanager将列表更改为网格和网格列表,但我想在更改视图时更改图标,就像视图处于列表模式时我想显示网格图标并且视图处于网格模式我想要显示列表模式

#include <iostream>
#include <string>
#include <tuple>

#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/algorithm/auxiliary/copy.hpp>
#include <boost/fusion/adapted/std_tuple.hpp>

struct foo
{
  std::string a, b, c;
  int d, e, f;
};

BOOST_FUSION_ADAPT_STRUCT(
    foo,
    (std::string, a)
    (std::string, b)
    (std::string, c)
    (int, d)
    (int, e)
    (int, f)
)

template<std::size_t...Is, class Tup>
foo to_foo_aux(std::index_sequence<Is...>, Tup&& tup) {
  using std::get;
  return {get<Is>(std::forward<Tup>(tup))...};
}
template<class Tup>
foo to_foo(Tup&& tup) {
  using T=std::remove_reference_t<Tup>;
  return to_foo_aux(
    std::make_index_sequence<std::tuple_size<T>{}>{},
    std::forward<Tup>(tup)
  );
}

template<std::size_t...Is>
auto to_tuple_aux( std::index_sequence<Is...>, foo const& f ) {
  using boost::fusion::at_c;
  return std::make_tuple(at_c<Is>(f)...);
}
auto to_tuple(foo const& f){
  using T=std::remove_reference_t<foo>;
  return to_tuple_aux(
    std::make_index_sequence<boost::fusion::result_of::size<foo>::type::value>{},
    f
  );    
}

int main(){


    foo f{ "Hello", "World", "!", 1, 2, 3 };

    std::tuple<std::string, std::string, std::string, int, int, int> dest = to_tuple(f);
    // boost::fusion::copy(f, dest); // also valid  but less general than constructor

    std::cout << std::get<0>(dest) << ' ' << std::get<1>(dest) << std::get<2>(dest) << std::endl;
    std::cout << at_c<0>(dest) << ' ' << at_c<1>(dest) << at_c<2>(dest) << std::endl; // same as above

    foo f2 = to_foo(dest);

    std::cout << at_c<0>(f2) << ' ' << at_c<1>(f2) << at_c<2>(f2) << std::endl;
}

MyAdapter

reinterpret_cast<std::tuple<...>&>(mystructinstance.i)

1 个答案:

答案 0 :(得分:0)

您可以维护一个布尔变量

case R.id.product_grid:
isGrid = true;
                isViewWithCatalog = !isViewWithCatalog;
                supportInvalidateOptionsMenu();

                rv.setLayoutManager(isViewWithCatalog ? new LinearLayoutManager(this) : new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
                rv.setAdapter(rvAdapter);
                break;

case R.id.product_list:
isGrid = false;
invalidateOptionMenu() or getActivity.invalidateOptionMenu() // If it is fragment

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_product, menu);
        MenuItem list = menu.findItem(R.id.list);
        MenuItem grid= menu.findItem(R.id.list);
        if(isGrid)
          grid.setVisible(true);
          list.setvisible(false)
        else
          list.setVisible(true);
          grid.setVisible(false);

        return super.onCreateOptionsMenu(menu);
    }