3

我正在使用片段处理 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 时请求许可后,下一个片段之后的片段和前一个片段之前的片段变为空白。我已经对适配器进行了所有可能的更改,但没有得到正确的结果。

请任何人都可以帮助它!

4

2 回答 2

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

//试试这可能是它的工作

于 2016-05-31T09:36:43.857 回答
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);
    }
}

}

//参考此代码..可能会对您有所帮助..

于 2016-05-31T09:47:32.790 回答