1

我有一個名爲navigation的活動和兩個名爲OneFragment,Description Fragment的碎片。在RecyclerView中替換ViewPager中的碎片點擊

導航活動包含這兩個片段如下代碼顯示:

navigation.java

public class navigation extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener { 
private TabLayout tabLayout; 
private ViewPager viewPager; 
private int[] tabIcons = { 
     R.drawable.ic_newsfeed, 
     R.drawable.ic_livestream, 
     R.drawable.ic_matches, 
     R.drawable.ic_sort 
}; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_navigation); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 



    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 
    viewPager = (ViewPager) findViewById(R.id.viewpager); 
    setupViewPager(viewPager); 

    tabLayout = (TabLayout) findViewById(R.id.tabs); 
    tabLayout.setupWithViewPager(viewPager); 
    setupTabIcons(); 
} 

private void setupTabIcons() { 
    tabLayout.getTabAt(0).setIcon(tabIcons[0]); 
    tabLayout.getTabAt(1).setIcon(tabIcons[1]); 
    tabLayout.getTabAt(2).setIcon(tabIcons[2]); 
    tabLayout.getTabAt(3).setIcon(tabIcons[3]); 

} 

private void setupViewPager(ViewPager viewPager) { 
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); 
    adapter.addFrag(new OneFragment(),getString(R.string.newsfeed)); 
    adapter.addFrag(new TwoFragment(), getString(R.string.livestream)); 
    adapter.addFrag(new ThreeFragment(),getString(R.string.matches)); 
    adapter.addFrag(new FourFragment(),getString(R.string.schedule)); 
    viewPager.setAdapter(adapter); 
} 
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 void onBackPressed() { 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    if (drawer.isDrawerOpen(GravityCompat.START)) { 
     drawer.closeDrawer(GravityCompat.START); 
    } else { 
     super.onBackPressed(); 
    } 
} 



@SuppressWarnings("StatementWithEmptyBody") 
@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 

    if (id == R.id.share) { 
     // Handle the camera action 
    } else if (id == R.id.check) { 

    } else if (id == R.id.rateus) { 

    } else if (id == R.id.aboutus) { 

    } 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 
    return true; 
} 

這是我的適配器類:

public class PostAdapter2 extends RecyclerView.Adapter<PostAdapter2.ViewHolder>{ 

public Context mContext; 
public ArrayList<News_data> original_items = new ArrayList<>(); 
public ArrayList<News_data> filtered_items = new ArrayList<>(); 
// ItemFilter mFilters = new ItemFilter(); 

public PostAdapter2(Context mContext, ArrayList<News_data> postList) { 
    this.mContext = mContext; 
    this.original_items = postList; 
    this.filtered_items = postList; 

} 

@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_news, parent, false); 


    return new ViewHolder(view); 

} 

@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 

    try { 

     final News_data post = filtered_items.get(position); 

     holder.txt_news_title.setText(post.getName_news()); 
     holder.setItemClickListener(new ItemClickListener() { 
      @Override 
      public void onItemClick() { 

       //what to do to replace the OneFragment with the DescriptionFragment 

      } 
     }); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 



@Override 
public int getItemCount() { 
    return filtered_items.size(); 
} 





public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 

    Context c; 
    ItemClickListener itemClickListener; 
    TextView txt_news_title,txt_desc; 
    ImageView image_news; 

    public ViewHolder(View itemView) { 
     super(itemView); 
     txt_news_title = (TextView) itemView.findViewById(R.id.txt_news_title); 
     itemView.setOnClickListener(this); 









    } 


    @Override 
    public void onClick(View v) { 
     this.itemClickListener.onItemClick(); 

    } 
    public void setItemClickListener(ItemClickListener itemClickListener) 
    { 
     this.itemClickListener=itemClickListener; 
    } 
} 

我想要什麼要做的是取代OneFragment說明片段上的Recyclerview項目點擊。

回答

1

遺憾的是沒人能取代Fragment的內線ViewPager,但你可以使用NestedFragmentChildFragment添加到您的主Fragment像你Activity做。 只需在您內部定義FrameLayoutOneFragmentLayout並呼叫getChildFragmentManager而不是getSupportFragmentManager。例如:

Fragment descriptionFragment = new DescriptionFragment(); 
FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); 
transaction.add(R.id.nested_fragment_container, descriptionFragment).commit(); 

欲瞭解更多信息,你可以點擊這裏查看:

https://developer.android.com/about/versions/android-4.2.html#NestedFragments

PS:如果你有比這多個片段,或者您希望您的片段是獨立你可以使用這個庫得到instagram like tabs:

https://github.com/ncapdevi/FragNav

+0

能否請你告訴我一些代碼,如果吳唐介意 –

+0

感謝你的鏈接,我已經看到,當u擊中recyclerview項目進行匹配父屬性的片段打開一個包含所有細節的新聞很多新聞應用程式.. 我想用DescriptionFragment –

+0

替換OneFragmet totaly,不幸的是目前沒有辦法在viewpager內部替換片段..所以我們必須更新片段。 我用一個例子更新了答案,就像你如何在活動中使用片段..只是有點不同^^ –