选项卡动态添加,如何设置文本

时间:2016-06-12 07:40:09

标签: android dynamic tabs

headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }

我想根据位置设置文字,我成功定位

package com.agri.kishan;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;
import android.widget.Toast;
import com.agri.kishan.OneFragment;

import java.util.ArrayList;
import java.util.List;

import com.agri.kishan.OneFragment;

public class ScrollableTabsActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    public static int tabsele;
    public static String realqus="this is a sample qus";


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

        //
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

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

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
                Toast.makeText(getApplicationContext(),tab.getPosition()+"",Toast.LENGTH_SHORT).show();
                 tabsele=tab.getPosition();
                /////////////////////////////////////////
                /////////////////////////////////
                ///////////////////
  

片段页面   片段页面是一个简单的片段页面,里面有文本视图,   我从活动中得到了标签的位置   我想根据活动中的位置将文本更改为片段中的textview

Fragement页面     包com.agri.kishan;

                if(tabsele==0)
                {
                   OneFragment.tv.setText("hai");



                } else if(tabsele==1)
                {
                    OneFragment.tv.setText("bye");
                }
                //////////////////
                ////////////////////////////////
                ///////////////////////////////////////////



            }

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

            }

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

            }
        });

    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        for(int i=0;i<=Card_display.counttabs;i++) {
            adapter.addFrag(new OneFragment(), "Qus:"+i);
        }

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

}

1 个答案:

答案 0 :(得分:0)

尝试这种方法 在onTabSelected

  OneFragment page = (OneFragment)viewPager.getAdapter().getItem(tab.getPosition());
page.tv.setText("Hi");