4

我正在使用 view-pager。我有三个标签:1. History,2. Main,3. Map。

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

但是现在我想在从Main > History滑动时更新 History 活动,所以在按下停止按钮后,我将最后一个值放入历史活动的列表视图中。

我的问题是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

4 回答 4

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) {

        }
    });
于 2014-02-17T23:46:44.330 回答
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;
    }
于 2014-10-08T05:48:25.560 回答
1

取一个字符串并将 Getter 和 setter 添加到您的FragmentActivity类中。

喜欢...

String HistoryFragment;

public String getHistoryFragment() {
    return HistoryFragment;
}


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

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

有关更多信息,请查看ViewPager 中片段之间的通信链接。

于 2012-12-25T09:22:44.083 回答
0

您是否尝试过实现 OnPageChanged ?您可以捕获用户开始滑动的时间。

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

onPageScrollStateChanged(int state){
    if (state==OnPageChangeListener.SCROLL_STATE_DRAGGING){
       // update here
    }
}
于 2012-12-03T04:45:22.743 回答