gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 09:43:14 25 4
gpt4 key购买 nike

我正在创建 ViewPager 并希望在 Button.OnClickListener 上调用下一个 Pager。从 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 上加载了这两个国家。

我还可以滑动页面(上一页和下一页)。但我想在点击 TextView 等后立即转到下一个寻呼机。是否可以在 ViewPager 中实现?

最佳答案

当然!您只需调用 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);
}
}
});
}
}

在你的 Activity 中:

@AfterViews
void afterViewCreated(){
customAdapter.setCallback(new Callback() {
@Override
public void onNextClick(int nextPage) {
viewPager.setCurrentItem(nextPage);
}
});
viewPager.setAdapter(customAdapter);
}
}

关于android - 如何以编程方式调用 ViewPager 的下一页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38968574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com