2

我有一個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); 
 
     } 
 
    } 
 

 
}

+2

你有沒有試過從LinearLayoutManager更改爲GridLayoutManager上按鈕單擊然後通知()適配器? –

+0

我主要關心的是如何將activity的onOptionsItemSelected中的點擊操作傳遞給fragment。 –

+2

您也可以在片段中使用onOptionItemSelected,只需傳遞該ID即可。或者它顯示無法訪問? –

回答

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

在您的兩個片段覆蓋方法onOptionsItemSelected()和裏面,根據用戶選擇更改佈局。代碼講多..見下:

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     super.onOptionsItemSelected(item); 

     //change your layout from linear to grid here 

     return true; 
    } 

upvoted我的答案,如果它可以幫助你。

0

OK,這裏是你可以做什麼:

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列。檢查是否有幫助,並讓我知道。 :)

+0

感謝您的答案。我已經實現了它。我在問如何改變適配器的佈局。喜歡列表:R.layout.list_row和網格:R.layout_grid。我看到一個帖子顯示類似的東西http://stackoverflow.com/questions/28581712/android-recyclerview-change-layout-file-list-to-grid-onoptionitemselected @Bhavesh –

+0

@SomnathPal只需創建兩個不同的適配器並定義佈局在那裏。那麼,高興可以幫助:) –