如何在ViewPager中更改幻灯片上的片段内容?

时间:2014-05-25 08:54:23

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

我已经在我的活动中实现了THIS并且它工作得非常好,但每当用户在屏幕上滑动手指时,我想更改片段的内容。现在它显示了不考虑幻灯片的相同文本(从左到右或从右到左)。我该怎么处理?我知道我在这里错过了一小部分,但我无法理解发生了什么。我应该在哪里添加监听器并处理操作?

这是我到目前为止所做的:

public class ViewrerAct extends FragmentActivity {

    private static final int NUM_PAGES = 5;
    private ViewPager mPager;
    private PagerAdapter mPagerAdapter;

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

        // Instantiate a ViewPager and a PagerAdapter.
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.bulgarian_sayings, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(
                    R.layout.fragment_viewer, container, false);
            return rootView;
        }
    }

    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            System.out.println("inside!");
            return new ScreenSlidePageFragment();
        }

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

public class ScreenSlidePageFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(
                R.layout.fragment_viewer, container, false);

        return rootView;
    }
}

1 个答案:

答案 0 :(得分:2)

您应该发布ScreenSlidPagerAdapter

的代码

然而,您需要覆盖每次滑动寻呼机并更改显示页面时调用的寻呼机适配器的setPrimaryItem方法。

代表:

   @Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
    super.setPrimaryItem(container, position, object);
    currentPage = position;
}

您可以将字符串存储在数组中并将其传递给适配器和内部适配器,您可以根据所选位置更改片段的内容。

如果想要更合适的示例,也可以发布寻呼机适配器。

在你的情况下,你可以简单地传递字符串数组,假设你只是想要显示不同的测试,适用于ex:

   private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        String[] displayText;
        public ScreenSlidePagerAdapter(FragmentManager fm,String[] displayText) {
            super(fm);
            this.displayText=displayText;
        }

        @Override
        public Fragment getItem(int position) {
            System.out.println("inside!");
            ScreenSlidePageFragment fragment = new ScreenSlidePageFragment(); 
            fragment.setText(displayTest[position]);
            return fragment;
        }

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

你的Fragment类将改为:

public class ScreenSlidePageFragment extends Fragment {
    private String text;    
    public void setText(String text){

     this.text=text;
     }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(
                R.layout.fragment_viewer, container, false);
        ((TextView)rootView.findViewById(textViewId)).setText(Text);
        return rootView;
    }
}