如何在swipe上更新viewpager的视图 - android

时间:2012-12-03 04:30:06

标签: android android-viewpager updates

我正在使用view-pager.In我有三个选项卡:1。History,2。Main,3。Map。

我有秒表进入主要活动。当我按下开始然后停止时,秒表的值加入sqlite DB。在历史活动中,我从数据库中获取秒表的值并存储到列表视图中。它工作得很完美。

但是,当我从 Main>滑动时,我希望更新历史记录活动历史记录所以按下停止按钮后,我将最后一个值输入到历史活动的列表视图中。

我的问题是how can I update the view on swipe the viewpager?

public class SwipeyTabsSampleActivity extends FragmentActivity {

private static final String [] TITLES = {
    "HISTORY", 
    "MAIN",
    "MAP"  

};

private SwipeyTabs mTabs;
private ViewPager mViewPager;
//Handler handler=new Handler();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_swipeytab);

    mViewPager = (ViewPager) findViewById(R.id.viewpager);
    mTabs = (SwipeyTabs) findViewById(R.id.swipeytabs);

    SwipeyTabsPagerAdapter adapter = new SwipeyTabsPagerAdapter(this,
            getSupportFragmentManager());
    mViewPager.setAdapter(adapter);
    mTabs.setAdapter(adapter);
    mViewPager.setOnPageChangeListener(mTabs);
    mViewPager.setCurrentItem(1); 
    mViewPager.setOffscreenPageLimit(3);


}

 private class SwipeyTabsPagerAdapter extends FragmentPagerAdapter implements
        SwipeyTabsAdapter, ViewPager.OnPageChangeListener {

    private final Context mContext;
    ViewPager viewpager;



    public SwipeyTabsPagerAdapter(Context context, FragmentManager fm, ViewPager mViewPager) {
        super(fm);

        this.mContext = context;
        viewpager = mViewPager;

        //viewpager.setAdapter(this);
        viewpager.setOnPageChangeListener(this);

    }

    @Override
    public Fragment getItem(int position) {

         Fragment f = new Fragment();

        switch (position) {
        case 0:
            f = History.newInstance(position);

            break;
        case 1:
            f = Main.newInstance(position);
            break;
        case 2:
            f = Map.newInstance(position);

            break;

        }
        return f;

    }

    @Override
    public int getCount() {
        return TITLES.length;
    }


     @Override
        public void destroyItem(ViewGroup container, int position, Object object) {

            ViewPager pager = (ViewPager) container;
            View view = (View) object;

            pager.removeView(view);
        }



    public TextView getTab(final int position, SwipeyTabs root) {
        TextView view = (TextView) LayoutInflater.from(mContext).inflate(
                R.layout.swipey_tab_indicator, root, false);
        view.setText(TITLES[position]);
        view.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mViewPager.setCurrentItem(position);

            }
        });

        return view;
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageSelected(int positiion) {
        // TODO Auto-generated method stub

    }

}

}

4 个答案:

答案 0 :(得分:8)

添加ViewPager OnPageChangeListener:

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            // your code here
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

答案 1 :(得分:2)

这将影响视图寻呼机中从一个页面到另一个页面的更改。

OnPageChangeListener pagechangelistener =new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                Logger.logMessage("Called first");

                pageAdapter.notifyDataSetChanged();
                indicator.setCurrentItem(arg0);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

                Logger.logMessage("Called second");

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

                Logger.logMessage("Called third");

            }
        };
        myViewPager.setOnPageChangeListener(pagechangelistener);

在页面适配器中使用它。

@Override
    public int getItemPosition(Object object) {

        return POSITION_NONE;
    }

答案 2 :(得分:1)

取一个字符串,将Getters和setter添加到FragmentActivity类中。

喜欢......

String HistoryFragment;

public String getHistoryFragment() {
    return HistoryFragment;
}


public void setHistoryFragment(String historyFragment) {
    HistoryFragment = historyFragment;
}

然后我们可以通过调用findFragmentByTag(<tag of target fragment>)来访问当前片段中的另一个片段。

有关详细信息,请查看链接Communication between Fragments in ViewPager

答案 3 :(得分:0)

您是否尝试过实施OnPageChanged`?您可以在用户开始滑动时捕获。

执行pager.setOnPageChanged(new ....),Eclipse将向您显示可以覆盖的方法。 然后,您可以覆盖onPageScrollStateChanged(int state)

onPageScrollStateChanged(int state){
    if (state==OnPageChangeListener.SCROLL_STATE_DRAGGING){
       // update here
    }
}