我已经实现了选项卡布局设计,并且每当有人单击ViewPager中的图像时,我都想切换到其他选项卡。我的研究得出的结论是,在ViewPager适配器上注册了点击事件,但是我找不到从适配器获取标签布局ID的方法。BELOW是我的视图Pager适配器代码:
public class BooksPagerAdapter extends PagerAdapter {
private Context mContext;
ViewPager viewPager;
private ViewPager pager = null;
ImageView image1;
private int images[] = {R.drawable.lagers11, R.drawable.stout11, R.drawable.malts11, R.drawable.lagers11, R.drawable.stout11, R.drawable.malts11};
public BooksPagerAdapter(Context context) {
mContext = context;
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
LayoutInflater inflater = LayoutInflater.from(mContext);
RelativeLayout relativeLayout = (RelativeLayout) ViewGroup.inflate(mContext, R.layout.tab_layout, null);
View itemView = inflater.inflate(R.layout.shop_books, container, false);
ImageView image1 = (ImageView) itemView.findViewById(R.id.image);
image1.setImageResource(images[position]);
itemView.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//this will log the page number that was click
Toast.makeText(mContext, position + " pagess", Toast.LENGTH_SHORT).show();
}
});
((ViewPager) container).addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return (6);
}
@Override
public float getPageWidth(int position) {
return (0.3f);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == object);
}}
show_books布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:padding="2dp" android:layout_height="100dp" /> </FrameLayout>
选项卡布局
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/shattered" android:orientation="vertical"> <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/qb" android:elevation="6dp" app:tabGravity="fill" app:tabIndicatorColor="#ffffff" app:tabIndicatorHeight="3dp" app:tabMode="scrollable" app:tabSelectedTextColor="@color/tabSelectedTextColor" app:tabTextColor="@color/tabTextColor"> </android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager android:id="@+id/viewpagertabs" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/tabs"> </android.support.v4.view.ViewPager> </RelativeLayout>
简单,在图像的
OnClick
中,您应该尝试此
viewPager.setCurrentItem(pageNumber);
您可以使用 EventBus
在活动或片段的子视图之间进行共识。首先定义事件:
public class ImageSelectedEvent {
int index;
public MessageEvent(int index) {
this.index = index;
}
}
比注册此事件,例如:
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(ImageSelectedEvent event) {
//TODO: make tab selected for event.index
}
添加寄存器/未注册代码:
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
}
当图像单击时,最终使用这样的某个问题:
itemView.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
int index = //get image index
EventBus.getDefault().post(new ImageSelectedEvent(index));
}
});
您可以从这里找到更多:
如何从3个步骤中开始EventBus