我有一個TabLayout活動和2片段附加到viewpager。這兩個片段都會有回收站查看。我想要的是將活動的菜單按鈕單擊中的回收站視圖的佈局管理器從列表更改爲網格,反之亦然。我正在考慮使用接口,但無法將其添加到碎片。有什麼辦法可以做到這一點?更改recycleview listview到gridview片段上的activity按鈕點擊
// 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);
}
}
// 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);
}
}
}
你有沒有試過從LinearLayoutManager更改爲GridLayoutManager上按鈕單擊然後通知()適配器? –
我主要關心的是如何將activity的onOptionsItemSelected中的點擊操作傳遞給fragment。 –
您也可以在片段中使用onOptionItemSelected,只需傳遞該ID即可。或者它顯示無法訪問? –