如何通过点击Expandable list子项目打开viewpager中的特定选项卡?

问题描述:

我正在使用Expandable列表,其中当我点击子列表项打开一个Viewpager与选定的第一个选项卡,但我想打开一个特定的选项卡或片段在viewpager,我单击。 假设我点击了 “移动开发” 我想打开viewpager.here Mobile开发Tab是我的代码: 扩展listiview: -如何通过点击Expandable list子项目打开viewpager中的特定选项卡?

公共类Services_fragment扩展片段{

ExpandableListAdapter listAdapter; 
ExpandableListView expListView; 
List<String> listDataHeader; 
HashMap<String, List<String>> listDataChild; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    final View view = inflater.inflate(R.layout.services_fragment, container, false); 
    ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Services"); 

    expListView = (ExpandableListView) view.findViewById(R.id.service_listview); 

    prepareListData(); 

    listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild); 


    expListView.setAdapter(listAdapter); 

    expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
     @Override 
     public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 
      Toast.makeText(
        getActivity(), 
        listDataHeader.get(groupPosition) 
          + " : " 
          + listDataChild.get(
          listDataHeader.get(groupPosition)).get(
          childPosition), Toast.LENGTH_SHORT) 
        .show(); 

      if (groupPosition == 0) { 
       Container_Activity.showFragment(new Development_fragment_viewpager(), ""); 
      } else if (groupPosition == 1) { 
       Container_Activity.showFragment(new Designing_fragment_viewpager(), ""); 
      } else if (groupPosition == 2) { 
       Container_Activity.showFragment(new Seo_fragment_viewpager(), ""); 
      } 
      return false; 
     } 
    }); 

    expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { 
     @Override 
     public void onGroupExpand(int groupPosition) { 

      Toast.makeText(getActivity(), 
        listDataHeader.get(groupPosition) + " Expanded", 
        Toast.LENGTH_SHORT).show(); 


     } 
    }); 
    return view; 
} 

private void prepareListData() { 
    listDataHeader = new ArrayList<String>(); 
    listDataChild = new HashMap<String, List<String>>(); 


    listDataHeader.add("Development"); 
    listDataHeader.add("Designing"); 
    listDataHeader.add("SEO & SEM"); 


    List<String> development = new ArrayList<String>(); 
    development.add("Web Development"); 
    development.add("Mobile Development"); 
    development.add("Software Development"); 


    List<String> designing = new ArrayList<String>(); 
    designing.add("Logo"); 
    designing.add("Brochure"); 
    designing.add("Catalog"); 
    designing.add("Other 3D Design"); 

    List<String> seosem = new ArrayList<String>(); 
    seosem.add("SEO"); 
    seosem.add("SMM"); 


    listDataChild.put(listDataHeader.get(0), development); // Header, Child data 
    listDataChild.put(listDataHeader.get(1), designing); 
    listDataChild.put(listDataHeader.get(2), seosem); 
} 

}

ViewPager代码: -

公共类Development_fragment_viewpager延伸片段{

TabLayout tabLayout; 
ViewPager viewPager; 
@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    final View view=inflater.inflate(R.layout.development_fragment_viewpager,container,false); 

    tabLayout= (TabLayout) view.findViewById(R.id.tabLayout_development); 
    tabLayout.setupWithViewPager(viewPager); 

    tabLayout.addTab(tabLayout.newTab().setText("Web Development")); 
    tabLayout.addTab(tabLayout.newTab().setText("Mobile Development")); 
    tabLayout.addTab(tabLayout.newTab().setText(" Software Development")); 
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 


    viewPager= (ViewPager) view.findViewById(R.id.development_pager); 

    Development_Adapter adapter = new Development_Adapter(getChildFragmentManager(), tabLayout.getTabCount()); 

    viewPager.setAdapter(adapter); 

    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 

      viewPager.setCurrentItem(tab.getPosition()); 

     } 

     @Override 
     public void onTabUnselected(TabLayout.Tab tab) { 

     } 

     @Override 
     public void onTabReselected(TabLayout.Tab tab) { 

     } 
    }); 

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 

    return view; 
} 

}

适配器类别: -

公共类Development_Adapter扩展FragmentStatePagerAdapter {

int TabCount; 
public Development_Adapter(FragmentManager fm,int TabCount) { 
    super(fm); 
    this.TabCount=TabCount; 
} 

@Override 
public Fragment getItem(int position) { 

    switch (position){ 

     case 0: 
      Web_development_fragment web_development_fragment=new Web_development_fragment(); 
     return web_development_fragment; 

     case 1: 
      Mobileapplication_fragment mobileapplication_fragment=new Mobileapplication_fragment(); 
      return mobileapplication_fragment; 
     case 2: 
      Software_development software_development=new Software_development(); 
      return software_development; 
     default: 
     return null; 
    } 

} 

@Override 
public int getCount() { 
    return TabCount; 
} 

}

+0

使用viewpager.setCurrentItem(指数)函数 – Keerthivasan

+0

@ Keerthivasan你可以用示例 –

您可以在片段参数和片段

获取数据
Fragment fragment=new new Development_fragment_viewpager(); 
Bundle bundle1=new Bundle(); 
bundle.putInt("PagePosition",2); 
fragment.setArguments(bundle1); 
Container_Activity.showFragment(fragment,""); 

,现在在你片段覆盖onCreate方法

int pageno=0; 
@Override 
public void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Bundle bundle=getArguments(); 
    if (bundle!=null){ 
     pageno=bundle.getInt("PagePosition"); 
    } 
} 

,现在你onCrateView只是调用一个方法

viewPager.setCurrentItem(pageno, false); 

和对其做

+0

解释不完全正确,,我选择任何项目在子列表中每次打开页面2项 –

+0

在Bundle我已经通过页面号码,所以你必须通过你想打开它的包的页码 –

+0

我也通过捆绑页码,但所有的时间显示,我没有通过任何改变的位置。 –