我已經實現了選項卡布局設計,我想切換到不同的選項卡,只要有人點擊viewpager中的圖像。我的研究結論是,點擊事件註冊的viewpager適配器上,但是我不能找到一種方法,抓住從adapter.Below標籤佈局ID是我的看法尋呼機適配器代碼:從viewpager切換選項卡
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>
請添加您的佈局。目前還不清楚你想要做什麼。 –
你是否試圖使用ViewPager與圖像來選擇標籤?對於檢查,如果我選擇索引1的圖像,它應該使選項卡索引1選擇? –
是@OlcayErtaş這就是我想要的 –