ListView 内的 ViewPager [FragmentPagerAdapter]
ViewPager [FragmentPagerAdapter] inside ListView
我正在尝试将 ViewPager
放入 ListView
这里是ListView AdapterClass
的代码:
public class AdapterPager extends BaseAdapter {
.....
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.component_pager, parent, false);
new ViewHolder(view, itemData.get(position));
}
return view;
}
视图持有者:
private class ViewHolder {
ViewPager pager;
PagerAdapter pagerAdapter;
public ViewHolder(View view, ModelItem model){
pager = (ViewPager) view.findViewById(R.id.pager);
pager.setId(model.getId()); /// uniq ID
pagerAdapter = new MyFragmentPagerAdapter(((FragmentActivity)view.getContext()).getSupportFragmentManager());
pager.setAdapter(pagerAdapter);
}
然后,寻呼机适配器:
static final int PAGE_COUNT = 2;
private class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return FragmentComponentPager.newInstance(position, mModel);
}
@Override
public float getPageWidth(int position) {
float f_width = 1f;
return f_width;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
}
最后,Pager Fragment
:
public class FragmentComponentPager extends Fragment {
static final String ARGUMENT_PAGE_NUMBER = "arg_page_number";
static final String ARGUMENT_NAME = "arg_name";
int pageNumber;
public static FragmentComponentPager newInstance(int page, ModelItem data) {
FragmentComponentPager pageFragment = new FragmentComponentPager();
Bundle arguments = new Bundle();
arguments.putInt(ARGUMENT_PAGE_NUMBER, page);
if (data != null){
arguments.putString(ARGUMENT_NAME, data.getName());
}
pageFragment.setArguments(arguments);
return pageFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pageNumber = getArguments().getInt(ARGUMENT_PAGE_NUMBER);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.component_list, null);
TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
tvTitle.setText(getArguments().getString(ARGUMENT_NAME));
return view;
}
}
当我 运行 它 ListView
显示我添加的所有项目,但所有这些项目,除了最后一个 ,都是空的 ViewPager
.
而且,如果我删除了 pager.setId(model.getId());
,第一个 项显示视图寻呼机,其他项为空。
猜想 FragmentManager 有问题,但不知道如何处理:(
在您的 ViewHolder 中,您需要调用 getChildFragmentManager 而不是 getSupportFragmentManager。
我正在尝试将 ViewPager
放入 ListView
这里是ListView AdapterClass
的代码:
public class AdapterPager extends BaseAdapter {
.....
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.component_pager, parent, false);
new ViewHolder(view, itemData.get(position));
}
return view;
}
视图持有者:
private class ViewHolder {
ViewPager pager;
PagerAdapter pagerAdapter;
public ViewHolder(View view, ModelItem model){
pager = (ViewPager) view.findViewById(R.id.pager);
pager.setId(model.getId()); /// uniq ID
pagerAdapter = new MyFragmentPagerAdapter(((FragmentActivity)view.getContext()).getSupportFragmentManager());
pager.setAdapter(pagerAdapter);
}
然后,寻呼机适配器:
static final int PAGE_COUNT = 2;
private class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return FragmentComponentPager.newInstance(position, mModel);
}
@Override
public float getPageWidth(int position) {
float f_width = 1f;
return f_width;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
}
最后,Pager Fragment
:
public class FragmentComponentPager extends Fragment {
static final String ARGUMENT_PAGE_NUMBER = "arg_page_number";
static final String ARGUMENT_NAME = "arg_name";
int pageNumber;
public static FragmentComponentPager newInstance(int page, ModelItem data) {
FragmentComponentPager pageFragment = new FragmentComponentPager();
Bundle arguments = new Bundle();
arguments.putInt(ARGUMENT_PAGE_NUMBER, page);
if (data != null){
arguments.putString(ARGUMENT_NAME, data.getName());
}
pageFragment.setArguments(arguments);
return pageFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pageNumber = getArguments().getInt(ARGUMENT_PAGE_NUMBER);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.component_list, null);
TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
tvTitle.setText(getArguments().getString(ARGUMENT_NAME));
return view;
}
}
当我 运行 它 ListView
显示我添加的所有项目,但所有这些项目,除了最后一个 ,都是空的 ViewPager
.
而且,如果我删除了 pager.setId(model.getId());
,第一个 项显示视图寻呼机,其他项为空。
猜想 FragmentManager 有问题,但不知道如何处理:(
在您的 ViewHolder 中,您需要调用 getChildFragmentManager 而不是 getSupportFragmentManager。