ViewPager视图无法更新页面

时间:2016-05-10 09:39:28

标签: android android-viewpager

我目前有ViewPager页面很多。现在我还有一个充满字符串的数组,以填充每页上的TextView。但是当我在TextView中加载字符串时,它不会显示字符串。但是,当我滑到下一页并返回时,它会在那里。但是,在我滑动到下一页并返回之前,下一页没有字符串。

这是我的onPageSelected方法中的代码。但是这段代码也在我的onCreateView方法中。

@Override
public void onPageSelected(int position) {
    position02 = position;
    date = dates[position];
    Log.d("POSITION", String.valueOf(position));
    Log.d("POSITION02", String.valueOf(mViewPager.getCurrentItem()));


    TextView actionBarDay = (TextView) findViewById(R.id.actionBarDay);
    TextView actionBarMonth = (TextView) findViewById(R.id.actionBarMonth);
    TextView actionBarYear = (TextView) findViewById(R.id.actionBarYear);


    String date = dates[mViewPager.getCurrentItem()];

    actionBarDay.setText(date.substring(0,2));
    actionBarMonth.setText(date.replaceAll ("[0-9]", ""));
    actionBarYear.setText(date.substring(Math.max(0, date.length() - 4)));

    TextView textView = (TextView) findViewById(R.id.section_label);
    try {
        //date = date.replaceAll("\\s", "");
        //Log.d("TEST1", "d" + date);
        //int resId = getResources().getIdentifier("d" + date, "string", packageName);
        //String text = getResources().getString(resId);
        String text = texts[position];
        textView.setText(text);
    } catch (Exception e){
        e.printStackTrace();
    }
}

我的PagerAdapter课程:

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        position01 = position;
        return PlaceholderFragment.newInstance(position);
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 366;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "SECTION 1";
            case 1:
                return "SECTION 2";
            case 2:
                return "SECTION 3";
        }
        return null;
    }

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

0 个答案:

没有答案