有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

来自viewpager的java开关选项卡

我已经实现了选项卡布局设计,每当有人单击viewpager中的图像时,我希望切换到不同的选项卡。我的研究得出结论,点击事件在viewpager适配器上注册,但是我找不到从适配器获取选项卡布局id的方法。下面是我的视图寻呼机适配器代码:

    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);
    }}
  1. 显示书籍布局

    <FrameLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:gravity="center"
    安卓:orientation="vertical">
    <ImageView
    安卓:id="@+id/image"
    安卓:layout_width="wrap_content"
    安卓:padding="2dp"
    安卓:layout_height="100dp" />
    </FrameLayout>
    
  2. 选项卡布局

    <RelativeLayout 
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:background="@drawable/shattered"
    安卓:orientation="vertical">
    <安卓.support.design.widget.TabLayout
    安卓:id="@+id/tabs"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:background="@color/qb"
    安卓:elevation="6dp"
    app:tabGravity="fill"
    app:tabIndicatorColor="#ffffff"
    app:tabIndicatorHeight="3dp"
    app:tabMode="scrollable"
    app:tabSelectedTextColor="@color/tabSelectedTextColor"
    app:tabTextColor="@color/tabTextColor">
    </安卓.support.design.widget.TabLayout>
    <安卓.support.v4.view.ViewPager
    安卓:id="@+id/viewpagertabs"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:layout_below="@+id/tabs">
    </安卓.support.v4.view.ViewPager>
    </RelativeLayout>
    

共 (2) 个答案

  1. # 1 楼答案

    您可以使用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));
        }
    });
    

    您可以在这里找到更多信息:

    How to get started with EventBus in 3 steps

  2. # 2 楼答案

    Simple, in OnClick of image you should try this

    viewPager.setCurrentItem(pageNumber);