ViewPager不会自动刷新

时间:2017-07-23 07:35:36

标签: java android android-fragments android-viewpager android-tabs

我想在用户输入edittext时更改选项卡中的文本其他片段并输入此

我有一些关于viewpager的问题,当用户输入时它不会自动刷新,但是如果用户滑动选项卡刷新它的工作!

示例:

用户输入时tab1中的当前值,tab1未刷新但用户滑动到tab2,tab2刷新,用户滑动到tab1,tab1刷新!

我想viewpager自动刷新

感谢您的帮助,抱歉我的英语不好。

我的活动

public class SearchActivity extends AppCompatActivity {

    public static EditText edt_home_titlebar;
    ImageView btn_home_slidemenu;
    public static String query_text=null;
    ViewPager pager;
    SmartTabLayout tabs;
    private ArrayList<PagerItem> mTabs = new ArrayList<PagerItem>();
    MyPagerAdapter myPager;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        query_text=null;
    }

    @Override
    public void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_activity);

        edt_home_titlebar = (EditText) findViewById(R.id.edt_home_titlebar);
        btn_home_slidemenu = (ImageView) findViewById(R.id.btn_home_slidemenu);


        initTabs();
        pager = (ViewPager) findViewById(R.id.pager);
        tabs = (SmartTabLayout) findViewById(R.id.tabs);

        pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        tabs.setDistributeEvenly(true);
        tabs.setViewPager(pager);
        Log.d("SearchActivity", "onCreate: " + query_text);

        edt_home_titlebar.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {

                    query_text = edt_home_titlebar.getText().toString().trim();
                    Log.d("SearchActivity", "onEditorAction4: " + query_text);

                    return true;
                }
                return false;
            }
        });
        btn_home_slidemenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

    }

    class MyPagerAdapter extends FragmentStatePagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mTabs.get(position).createFragment(position);
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            Log.d("SearchActivity","mTabs=>"+mTabs.get(position).getTitle().toString());
            return mTabs.get(position).getTitle().toString();
        }

        @Override
        public int getItemPosition(Object object) {
            return PagerAdapter.POSITION_NONE;
        }
    }
    class PagerItem {
        private final CharSequence mTitle;

        PagerItem(CharSequence title) {
            mTitle = title;

        }

        Fragment createFragment(int i) {
            Log.d("SearchActivity","getI=>"+i);
//            Fragment fragment = null;
            switch (i) {
                case 0:
                    SearchSlideFragment fragment1 = new SearchSlideFragment();
                    Log.d("SearchActivity","(FRIEND)title=>"+mTabs.get(0).getTitle().toString());
                    return fragment1;
                case 1:
                    SearchPage fragment2 =new SearchPage();
                    Log.d("SearchActivity","get(1)title=>"+mTabs.get(1).getTitle().toString());
                    return fragment2;
                case 2:
                    SearchDiaryFragment fragment3 = new SearchDiaryFragment();
                    Log.d("SearchActivity","(DIARY)title=>"+mTabs.get(2).getTitle().toString());
                    return fragment3;
                case 3:
                    SearchQuestFragment fragment4 = new SearchQuestFragment();
                    Log.d("SearchActivity","(QUEST)title=>"+mTabs.get(3).getTitle().toString());
                    return fragment4;
                case 4:
                    Log.d("SearchActivity","get(4)title=>"+mTabs.get(4).getTitle().toString());
                    SearchReviewFragment fragment5 = new SearchReviewFragment();
                    return fragment5;
                case 5:
                    SearchTopicFragment fragment6 = new SearchTopicFragment();
                    Log.d("SearchActivity","(TOPIC)title=>"+mTabs.get(5).getTitle().toString());
                    return fragment6;
                default:
                    return null;
            }

        }

        CharSequence getTitle() {
            return mTitle;
        }
    }
    private void initTabs(){
        mTabs.clear();
        mTabs.add(new PagerItem(this.getResources().getString(R.string.tx_people)));
        mTabs.add(new PagerItem(this.getResources().getString(R.string.tx_slide_page)));
        mTabs.add(new PagerItem(this.getResources().getString(R.string.tx_diary)));
        mTabs.add(new PagerItem(this.getResources().getString(R.string.tx_quest)));
        mTabs.add(new PagerItem(this.getResources().getString(R.string.tx_review)));
        mTabs.add(new PagerItem(this.getResources().getString(R.string.tx_topic)));
    }
}

0 个答案:

没有答案