具有Viewpager的android标签中的运行时权限

时间:2018-11-01 07:01:13

标签: android android-fragments android-viewpager

在我的包含标签中包含viewpager片段。应用程序中有3个标签。每个选项卡都需要不同的运行时权限。我为tab1的第一次运行应用程序权限询问。我单击了tab2,没有权限要求。如果我单击tab3权限询问。如果我第二次启动应用程序对tab2的许可,请询问。我该如何解决?

    mTab1 = new Tab1();
    mTab2 = new Tab2();
    mTab3 = new Tab3();
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(mTab1, tabTitles[0]);
    adapter.addFragment(mTab1, tabTitles[1]);
    adapter.addFragment(mTab1, tabTitles[2]);
    viewPager.setAdapter(adapter);

标签的片段

public class Tab1 extends Fragment {

    private RecyclerView.LayoutManager layoutManager;
    private RecyclerView.Adapter mAdapter;
    private List<ListAdapter.Item> items = new ArrayList<>();
    private Context mContext = null;
    private static boolean mbPermission = false;
    private int mnCount = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        Log.e("Calls", "onCreate " + mbPermission);

        if( PackageManager.PERMISSION_GRANTED = ContextCompat.checkSelfPermission(this,Manifest.permission.READ_CALL_LOG){
          LoadViews();
        } else {
            requestPermissions(this, new String[]{Manifest.permission.READ_CALL_LOG},MULTIPLE_PERMISSIONS );
        } 
    }

        @Override
    public void onRequestPermissionsResult(int requestCode, String permissionsList[], int[] grantResults) {
        switch (requestCode) {
            case MULTIPLE_PERMISSIONS:{
                for( int i = 0; i < grantResults.length; i++){
                    if( grantResults[i] == PackageManager.PERMISSION_GRANTED ){
                        LoadViews();
                    } else {
                        Log.e("MainActivity", " Permitted NOT OK: " + permissionsList[i]);
                    }
                }
                return;
            }
        }
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.e("Calls", "onCreateView " + mbPermission);
        View Root = inflater.inflate(R.layout.fragment_one, container, false);
        RecyclerView recyclerView = (RecyclerView) Root.findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new ListAdapter(items, 0, getContext());
        recyclerView.setAdapter(mAdapter);
        return Root;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        setUserVisibleHint(true);
    }
}

2 个答案:

答案 0 :(得分:1)

使用下面链接中提供的PageRefreshViewPager,它将仅加载在内存中可见的当前页面,并且 您的问题将得到解决,因为viewpager默认将两个页面加载到内存中

https://gist.github.com/r00786/0dc136a716e8027da33b08ea3154ade1

答案 1 :(得分:1)

您必须在活动中写权限代码,不要在片段中

在viewPager.setAdapter(adapter)下面编写;像这样

 mTab1 = new Tab1();
    mTab2 = new Tab2();
    mTab3 = new Tab3();
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(mTab1, tabTitles[0]);
    adapter.addFragment(mTab1, tabTitles[1]);
    adapter.addFragment(mTab1, tabTitles[2]);
    viewPager.setAdapter(adapter);
if( PackageManager.PERMISSION_GRANTED = ContextCompat.checkSelfPermission(this,Manifest.permission.READ_CALL_LOG){
          LoadViews();
        } else {
            requestPermissions(this, new String[]{Manifest.permission.READ_CALL_LOG},MULTIPLE_PERMISSIONS );
        }