我在活动中有一个TabLayout,并且有两个片段附加到viewpager。这两个片段都有recyclerview。我想要的是在活动的菜单按钮点击中将两个片段中的recyclerview的布局管理器从列表更改为网格,反之亦然。我在考虑使用接口但无法将其添加到片段中。有没有办法实现这个目标?
// Fragment
public class FragOne extends Fragment {
RecyclerView recycle;
FragOneAdapter adapter;
ArrayList<Model> arrayList = new ArrayList<>();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragone, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recycle = (RecyclerView) view.findViewById(R.id.recycle);
preparedata();
adapter = new FragOneAdapter(arrayList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL, false);
recycle.setLayoutManager(mLayoutManager);
ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(getActivity(), R.dimen.spacing_normal);
recycle.addItemDecoration(itemDecoration);
recycle.setHasFixedSize(true);
recycle.setAdapter(adapter);
}
private void preparedata(){
Model model = new Model();
model.setName("Name1");
model.setMobile("1234567890");
arrayList.add(model);
Model model1 = new Model();
model1.setName("Name2");
model1.setMobile("1234567891");
arrayList.add(model1);
Model model2 = new Model();
model2.setName("Name3");
model2.setMobile("1234567892");
arrayList.add(model2);
Model model3 = new Model();
model3.setName("Name4");
model3.setMobile("1234567893");
arrayList.add(model3);
Model model4 = new Model();
model4.setName("Name5");
model4.setMobile("1234567894");
arrayList.add(model4);
Model model5 = new Model();
model5.setName("Name6");
model5.setMobile("1234567890");
arrayList.add(model5);
}
}
&#13;
// Activity
public class MainActivity extends AppCompatActivity {
int icon = 0;
ViewPager viewPager;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Parallax Tabs");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
final CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapse_toolbar);
collapsingToolbarLayout.setTitleEnabled(false);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new FragOne(), "ONE");
adapter.addFrag(new FragTwo(), "TWO");
viewPager.setAdapter(adapter);
}
static class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item:
if(icon==0){
icon = 1;
item.setIcon(getResources().getDrawable(R.drawable.ic_grid));
}else if(icon==1){
icon = 0;
item.setIcon(getResources().getDrawable(R.drawable.ic_list));
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
&#13;
答案 0 :(得分:0)
在你的片段中你也有这种方法。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.xyz:
// now change your layout from LinearLayoutManager to GridLayoutManager here
return true;
}
return super.onOptionsItemSelected(item);
}
答案 1 :(得分:0)
在你的两个片段覆盖方法onOptionsItemSelected()和内部基于用户选择更改布局。代码说得更多..见下文:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
//change your layout from linear to grid here
return true;
}
如果对你有所帮助,我会给你答案。
答案 2 :(得分:0)
好的,这是你可以做的:
GridLayoutManager layoutManager;
RecyclerView recycle;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
layoutManager = new GridLayoutManager(getContext(), 4);
recycle.setLayoutManager(layoutManager);
recycler.setItemAnimator(new DefaultItemAnimator());
recycle.setHasFixedSize(true);
recycle.setAdapter(adapter);
return true;
}
我没有连接适配器,因为你可以自己做,整数4表示你想要多少列。在这种情况下,将有4列。检查它是否有帮助,让我知道。 :)