Android 每个选项卡中的浮动按钮不同的工作

Android Floating Button different jobs in every Tab

我需要使用浮动按钮作为相机并在每个选项卡上查看图像: 选项卡 1 图像视图 1 选项卡 2 图像视图 2 选项卡 3 图像视图 3 在我按下它并捕捉到第一张图片后,然后显示图片 然后在选项卡 2 上我再次按下并拍摄照片并将其显示在图像视图 2 上 我最大的问题我不知道如何切换(位置)选项卡或类似的东西来做不同的点击每个选项卡中的侦听器

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_layout1);

    imageView1 = (ImageView) findViewById(R.id.img1);
    imageView2 = (ImageView) findViewById(R.id.img2);
    imageView3 = (ImageView) findViewById(R.id.img3);

    tabLayout = (TabLayout) findViewById(R.id.tablayout1);
    tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab3"));

    tabLayout.setTabGravity(tabLayout.GRAVITY_FILL);

    viewPager = (ViewPager) findViewById(R.id.viewpager1);
    Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    viewPager.setAdapter(adapter);
    tabLayout.setOnTabSelectedListener(this);
    viewPager.getCurrentItem();

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab1);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            tabLayout.getSelectedTabPosition();
            Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
           File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
            String pictureName= getPictureName();
            File imageFile = new File(file,pictureName);
            Uri pictureUri = Uri.fromFile(imageFile);
            viewPager.getCurrentItem();
            i.putExtra(MediaStore.EXTRA_OUTPUT,pictureUri);
                startActivityForResult(i, 0);
        }
    });

}
public String getPictureName() {
    SimpleDateFormat sdf = new SimpleDateFormat("HHmm");
    String timestamp = sdf.format(new Date());
    return "image" + ".jpg";
    // yyyyMMdd_HHmmss
}

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        imageView1 = (ImageView) findViewById(R.id.img1);
        imageView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageView1.buildDrawingCache();
                Bitmap image = imageView1.getDrawingCache();

                Bundle extras = new Bundle();
                Intent o = new Intent(Layout1.this, Information.class);
                extras.putParcelable("Bitmap", image);
                o.putExtras(extras);
                startActivity(o);
            }
        });

        if (requestCode == 0 && resultCode == RESULT_OK) {

            String photoPath = Environment.getExternalStorageDirectory() + "/Download/image.jpg";
            Bitmap bitmap1 = BitmapFactory.decodeFile(photoPath);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 8;
            final Bitmap b = BitmapFactory.decodeFile(photoPath, options);
            imageView1.setImageBitmap(b);
        }
    }

            @Override
            public void onTabSelected (TabLayout.Tab tab){
            }

            @Override
            public void onTabUnselected (TabLayout.Tab tab){
            }

            @Override
            public void onTabReselected (TabLayout.Tab tab){
            }
        }    

您可以使用 TabLayoutgetSelectedTabPosition() 方法来根据当前选择的选项卡执行您的操作。

例如:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab1);
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        int position = tabLayout.getSelectedTabPosition();

        switch(position) {
            case 0:
                // first tab is selected
                break;
            case 1:
                // second tab is selected
                break;
            case 2:
                // third tab is selected
                break;
        }
    }
});