包含列表视图的动态选项卡显示相邻选项卡列表

时间:2017-03-01 14:02:30

标签: android

我有一个父级Fragment,其类是Scrollbaltab,其中包含许多动态子Fragment,其类是Fragment1但是当我滚动标签“two”时它显示标签“one”数据,当滚动到标签“three”时显示标签“两个”数据请帮帮我

  //Dynamic Fragment

        public class Fragment1 extends Fragment {

            RecyclerViewAdapter  recyclerViewAdapter;

            HashMap<String, ArrayList<String>> item = new HashMap<String,ArrayList<String>>();
            ArrayList  onlineData1 = new ArrayList<>();
            ArrayList  onlineData2 = new ArrayList<>();
            ArrayList  onlineData3 = new ArrayList<>();

            String titleName="";

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {

                View inflateView = inflater.inflate(R.layout.fragment_fragment1, null, false);
                RecyclerView recyclerView = (RecyclerView) inflateView.findViewById(R.id.recyclerView);

                recyclerView.removeAllViews();
                recyclerViewAdapter = new RecyclerViewAdapter(this.getActivity() ,item.get(titleName));
                recyclerView.setAdapter(recyclerViewAdapter);
                recyclerViewAdapter.notifyDataSetChanged();

                recyclerView.setHasFixedSize(true);

                //Layout manager for Recycler view
                recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity() ));
                recyclerViewAdapter.notifyDataSetChanged();
                return inflateView;

            }
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                int tab_position = Scrollbaltab.pos;

                if(onlineData1.isEmpty())
                {
                    onlineData1.clear();
                }
                if(onlineData2.isEmpty())
                {
                    onlineData2.clear();
                }

                if(onlineData3.isEmpty())
                {
                    onlineData3.clear();
                }
                item.clear();


              onlineData1.add("one");
               onlineData1.add("two");
                onlineData1.add("three");


                onlineData2.add("a");
                onlineData2.add("b");
                onlineData2.add("c");

                onlineData3.add("x");
               onlineData3.add("y");
                onlineData3.add("z");

                if(item.get("ONE")==null)
                    item.put("ONE",onlineData1);
                if(item.get("TWO")==null)
                    item.put("TWO",onlineData2);
                if(item.get("THREE")==null)
                    item.put("THREE",onlineData3);

                String [] arr= new String[]{"ONE","TWO","THREE"};

                if(tab_position>=0) {
                    titleName = arr[tab_position];
                }
                // set adpter

                Log.d("values"+tab_position,item.get(titleName).toString());
            }
        }

    //parent Fragment

        public class Scrollbaltab extends AppCompatActivity {

            Toolbar toolbar;
            ViewPager viewPager;
             TabLayout tabLayout;

            ViewPagerAdapter adapter;
            public static int pos=0;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_scrollbaltab);

                inlizeview();
            }

            private void inlizeview() {

                toolbar = (Toolbar) findViewById(R.id.toolbar);
                toolbar.setTitle("scrolling tabbar");
                setSupportActionBar(toolbar);

                //both are use for set arrow back button in toolbar
                //  getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                //toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);

                // when click on arrow button of tootbar it finish the current activity
                toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });

                viewPager = (ViewPager) findViewById(R.id.viewpager);
                setupViewPager(viewPager);

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

                viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {

                       pos=position;
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                });


            }

            private void setupViewPager(ViewPager viewPager) {
                 adapter = new ViewPagerAdapter(getSupportFragmentManager());
                adapter.addFragment(new Fragment1(), "ONE");
                adapter.addFragment(new Fragment1(), "TWO");
                adapter.addFragment(new Fragment1(), "THREE");

                viewPager.setAdapter(adapter);
            }

        }
//view pager adapter
public final List<Fragment> mFragmentList = new ArrayList<>();
    public final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        Log.e("DD","mFragmentList--"+mFragmentList.get(position));
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }


    @Override
    public int getItemPosition(Object object){
        return ViewPagerAdapter.POSITION_NONE;
    }
    }

0 个答案:

没有答案