在请求运行时权限后,片段变为空白

时间:2016-05-31 09:23:55

标签: android android-fragments android-viewpager fragmentstatepageradapter runtime-permissions

我正在使用带有fragment的viewPager,在我开始为不同模块实现运行时权限之前,它已经完成了所有工作。

这是我的viewPager适配器。

public class DashboardAdapter extends FragmentStatePagerAdapter {


  public DashboardAdapter(FragmentManager fm) {
     super(fm);
  }

  @Override
  public Fragment getItem(int position) {

      switch (position) {
         case 0:
             return FunkyInboxFragment.newInstance(position);
         case 1:
             return FunkyTakePhotoFragment.newInstance(position);
         case 2:
             return FunkyAddFriendsFragmnet.newInstance(position);
         default:
             return FunkyTakePhotoFragment.newInstance(position);
      }
  }

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

  @Override
  public Object instantiateItem(ViewGroup container, int position) {
     Fragment fragment = (Fragment) super.instantiateItem(container,   position);
      return fragment;
  }


  @Override
  public void destroyItem(ViewGroup container, int position, Object object) {
  }

}

这里不同的片段需要不同的权限,因此我为它制作了一个公共活动类。它在同一个片段中工作,但是在我刷了viewPager后,在下一个片段之后的片段和前一个片段之前的片段变为空白之后请求了许可。我已经在适配器上做了所有可能的改变但没有得到正确的结果。

任何人都可以帮忙!

2 个答案:

答案 0 :(得分:0)

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

//尝试这可能是有效的

答案 1 :(得分:0)

package com.example.madhavgor.actionbardemolast;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
        R.mipmap.ic_launcher,R.mipmap.ic_launcher_bluetooth
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //toolbar = (Toolbar) findViewById(R.id.toolbar);
    //setSupportActionBar(toolbar);

 getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    viewPager = (ViewPager) findViewById(R.id.viewPager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    setupTabIcons();
}

private void setupTabIcons() {
    tabLayout.getTabAt(0).setIcon(tabIcons[0]);
    tabLayout.getTabAt(1).setIcon(tabIcons[1]);
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new OneFragment(), "ONE");
    adapter.addFragment(new SecondFragment(), "BLUETOOTH");
    adapter.addFragment(new ThirdFragment(), "THREE");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

}

//参考此代码....可能会帮助你..