标签滑动无法正常工作

时间:2017-01-27 07:29:09

标签: android tabs android-viewpager

我在viewpager中有5个标签。 选择默认的第一个选项卡。第一个选项卡打开系统默认摄像头。然后我从第一个移动到第二个,第二个到第三个,依此类推。 但问题是,当我从第3,第4或第5个选项卡返回到第2个选项卡时,它会打开相机,但我在第2个选项卡中没有相机。它在第一个标签上。为什么会发生这种情况?我不明白......

请帮帮我......

我的代码如下:

TabsPagerAdapterLoginSuccess.java:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class TabsPagerAdapterLoginSuccess extends FragmentPagerAdapter {
    public TabsPagerAdapterLoginSuccess(FragmentManager supportFragmentManager) {
        super(supportFragmentManager);
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                // Top Rated fragment activity
                return new CameraFragment();
            case 1:
                // Games fragment activity
                return new FriendRequestFragment();

            case 2:
                // Movies fragment activity
                return new FriendFragment();

            case 3:
                // Movies fragment activity
                return new SearchFragment();

            case 4:
                return new ProfileFragment();
        }

        return null;
    }

    @Override
    public int getCount() {
        return 5;
    }
}

LoginSuccess.java

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public class LoginSuccess extends AppCompatActivity {

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bottomtabslider);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar1);
        setSupportActionBar(toolbar);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs1);
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.cameraicon1));
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.friendrequest));
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.friend));
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.search));
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.profileicon));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager1);
        final TabsPagerAdapterLoginSuccess adapterLoginSuccess = new TabsPagerAdapterLoginSuccess(getSupportFragmentManager());
        viewPager.setAdapter(adapterLoginSuccess);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

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

            }

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

            }
        });
    }
}

CameraFragment.java

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class CameraFragment extends Fragment {

    public String mycropimg,encodedImage;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.camerafragment, container, false);
        ((AppCompatActivity)getActivity()).getSupportActionBar().show();

        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivityForResult(intent, 0);

        return rootView;

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == getActivity().RESULT_OK) {
            Bitmap bp = (Bitmap) data.getExtras().get("data");
             String partFilename = "image_" + System.currentTimeMillis()+UUID.randomUUID().toString().substring(0,8) + ".jpg";

            storeCameraPhotoInSDCard(bp, partFilename);

            ByteArrayOutputStream stream = new ByteArrayOutputStream();

            bp.compress(Bitmap.CompressFormat.PNG, 50, stream);
            byte[] byte_arr = stream.toByteArray();

            encodedImage = Base64.encodeToString(byte_arr, 0);
            Intent i=new Intent(getActivity(),CameraPostStatus.class);
            i.putExtra("encoded_image",encodedImage);
            i.putExtra("imgfilename",partFilename);

            startActivity(i);
        }
    }


    private void storeCameraPhotoInSDCard(Bitmap bitmap, String currentDate){
        File outputFile = new File(Environment.getExternalStorageDirectory(), "image_" + System.currentTimeMillis()+UUID.randomUUID().toString().substring(0,8) + ".jpg");
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:3)

您需要在代码中执行此操作

 final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager1);
viewPager.setOffscreenPageLimit(5);

目前正在发生的事情是默认情况下,ViewPager会将当前页面,上一页和下一页保留在内存中。 但是在您的情况下,因为有5个选项卡,您可以将所有5个选项卡保留在内存中。你可以通过设置offScreenPageLimit来实现。