如何以编程方式调用ViewPager的下一页?

时间:2016-08-16 07:06:36

标签: android android-layout android-fragments android-viewpager android-view

我正在创建ViewPager,并希望在Pager上拨打下一个Button.OnClickListener。从Activity中的以下代码设置适配器:

@EActivity(R.layout.activity_city)
public class CountrySelectionActivity extends Activity {

@ViewById ViewPager viewPager;
@Bean CustomAdapter customAdapter;

@AfterViews
void afterViewCreated(){
    viewPager.setAdapter(customAdapter);
  }
}

以下是CustomAdapter类的代码:

@EBean
public class CustomAdapter extends PagerAdapter {

public CustomAdapter(Context context) {
    mContext = context;
    loadData();   // loading data as in List
}

......
......

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
}

@Override
public void destroyItem(View container, int position, Object object) {
    ((ViewPager) container).removeView((View) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    TextView firstCountry;
    TextView secondCountry;

    LayoutInflater layoutInflater = LayoutInflater.from(mContext);
    View view = layoutInflater.inflate(R.layout.country_pager_item, container, false);
    ....
    ....
}

一切都很好。我可以看到每个ViewPager pages加载两个国家/地区。

我也可以滑动pages(上一页和下一页)。但是我想在点击TextView后立即转到下一个寻呼机,依此类推。是否可以在ViewPager

中执行此操作

1 个答案:

答案 0 :(得分:2)

当然!您只需致电%.so.target: %.so.5 echo $< >> $(BUILD)/so.targets SO_TARGETS=$(basename $(shell cat $(BUILD)/so.targets)) makegen: $(TEAM_ROOT)HMI_FORGF/src/src.pro module_dirs $(SO_TARGETS)

编辑:

如果您想知道如何在viewPager.setCurrentItem(...)中调用此方法,请参阅以下示例:

Adapter

并在您的活动中:

public class CustomAdapter extends PagerAdapter {

    // ... other methods

    public static interface Callback {
        public void onNextClick(int nextPage);
    }

    private Callback callback;

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        TextView firstCountry;
        TextView secondCountry;

        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        View view = layoutInflater.inflate(R.layout.country_pager_item, container, false);

        secondCountry.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (callback != null){
                        callback.onNextClick(2);
                    }
                }
            });
    }
}