android 不可见,android-片段在ViewPager中不可见

我有一个要向其中添加片段的ViewPager.

在屏幕上可以正确查看ViewPager,但不幸的是它没有任何内容.

我已经在posts中看到了替换建议

getSupportFragmentManager()

getChildFragmentManager()

但是我需要ActionBar.不幸的是,ChildFragmentmanager在AppCompatActivity中不可用.

在调试时,我意识到Fragment的onCreateView()方法被调用,并且它返回一个布局.

问题:如何使片段可见?我的代码和sample的主要区别是什么?

活动:

public class ViewPagerActivity extends AppCompatActivity {

private ViewPager mViewPager;

private List locationList = new ArrayList<>();

private locationsBaseHandler db;

private CustomPagerAdapter mAdapter;

private Location mLocation;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_viewpager);

locationList.add(someData);

mViewPager = (ViewPager) findViewById(R.id.viewpager);

mAdapter = new CustomPagerAdapter(this, getSupportFragmentManager(), locationList);

mViewPager.setAdapter(mAdapter);

}

}

寻呼机适配器:

public class CustomPagerAdapter extends FragmentPagerAdapter {

private Context mContext;

private List locationList = new ArrayList<>();

public CustomPagerAdapter(Context mContext, FragmentManager fm, List locationList) {

super(fm);

this.locationList.addAll(locationList);

this.mContext = mContext;

}

@Override

public Fragment getItem(int position) {

return LocationFragment.newInstance(position);

}

@Override

public int getCount() {

return 5;

}

@Override

public boolean isViewFromObject(View view, Object object) {

return false;

}

}

分段:

public class LocationFragment extends Fragment {

private static final String ARG_LAYOUT_ID = "page_number";

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

View layout = inflater.inflate(R.layout.fragment_location, container, false);

return layout;

}

public static LocationFragment newInstance(int selectedIdForIndex) {

LocationFragment fragment = new LocationFragment();

Bundle args = new Bundle();

args.putInt(ARG_LAYOUT_ID, selectedIdForIndex);

fragment.setArguments(args);

return fragment;

}

public int getPageNumber() {

return getArguments().getInt(ARG_LAYOUT_ID);

}

}

解决方法:

尝试删除CustomPagerAdapter中的isViewFromObject方法.

@Override

public boolean isViewFromObject(View view, Object object) {

return false;

}

我们可以看到对isViewFromObject的注释:

Determines whether a page View is associated with a specific key object

as returned by {@link #instantiateItem(ViewGroup, int)}. This method is

required for a PagerAdapter to function properly.

标签:android-fragments,android-viewpager,adapter,android

来源: https://codeday.me/bug/20191026/1938770.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值