2012-04-12 67 views
1

编辑:我想通了,感谢FragmentPagerAdapter与ArrayListFragment

我一直在试图永远现在实现我的每一个ViewPager的页面单独列表视图。我非常了解this tutorial,但它只创建一个用于每个页面的ListView。

我希望能够在每个页面中有多个ListView,从中我可以控制每个页面的ListView会发生什么onClick。我有我的应用程序与一个ListView一起工作(如在教程中),但我一直在努力挣扎与多个。

我的代码发布在下面,我尝试了很多不同的东西,我只需要在我的开发中继续前进。感谢您的帮助

private static class MyAdapter extends FragmentPagerAdapter implements 
     TitleProvider { 

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

    @Override 
    public Fragment getItem(int position) { 
     return ArrayListFragment.newInstance(position); 
    } 

    @Override 
    public int getCount() { 
     return Arrays.TITLES.length; 
    } 

    public String getTitle(int position) { 
     return Arrays.TITLES[position]; 
    } 

} 

public static class ArrayListFragment extends ListFragment { 
    int mNum; 

    /** 
    * Create a new instance of CountingFragment, providing "num" as an 
    * argument. 
    */ 
    static ArrayListFragment newInstance(int num) { 
     ArrayListFragment f = new ArrayListFragment(); 

     // Supply num input as an argument. 
     Bundle args = new Bundle(); 
     args.putInt("num", num); 
     f.setArguments(args); 

     return f; 
    } 

    /** 
    * When creating, retrieve this instance's number from its arguments. 
    */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mNum = getArguments() != null ? getArguments().getInt("num") : 1; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.list_layout, container, false); 
     return v; 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     setListAdapter(new ArrayAdapter<String>(getActivity(), 
       android.R.layout.simple_list_item_1, Arrays.accTEAMS)); 
    } 

    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 
     Log.i("FragmentList", "Item clicked: " + id); 
    } 
} 

这是我ListFragments之一:

public class ACCfragment extends ListFragment { 
View v; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.list_layout, null); 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), 
      android.R.layout.simple_list_item_1, Arrays.accTEAMS); 
    setListAdapter(adapter); 
} 

@Override 
public void onListItemClick(ListView l, View v, int position, long id) { 

} 
+0

你能告诉我们你所使用的API版本和支持你使用(如果有的话)包? FragmentPagerAdapter的规则可以根据这些版本而有所不同。 – losttime 2012-04-16 22:38:56

+1

您想与我们分享您的解决方案吗? – 2013-03-03 17:14:04

回答

0

尝试......

+0

这并不能解决我所遇到的问题。我无法为每个页面提供单独的ListViews – ericcarboni 2012-04-12 02:35:48

+0

将公共视图onCreateView(LayoutInflater inflater,ViewGroup容器,Bundle savedInstanceState)方法放在同一个开关中。对于每一个返回一个不同的XML。只要确保你能够很好地记录你所处的页面,或者你将会获得很多。 – jsb 2012-04-12 13:55:29

+0

但是因为我使用不同的ListView片段,所以我没有单独的xml文件。我如何返回每个片段? – ericcarboni 2012-04-12 20:11:03

0

必须使用束片段之间发送PARAMS。

从片段1发送:

Bundle args = new Bundle(); 
args.putInt("my_integer",_stationId); 
FragmentManager fragmentManager = getFragmentManager(); 
FragmentTransaction ft = fragmentManager.beginTransaction(); 
Fragment fragment = Fragment.instantiate(getActivity(), Fragment2.class.getName(), args); 
ft.replace(R.id.layout, fragment); 
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
ft.commit(); 

接收和Fragment2获得:

getArguments().getInt("my_integer") 
+0

我不是完全想要在我的片段之间发送任何东西..只是试图在viewpager中的每个页面上显示不同的ListView片段 – ericcarboni 2012-04-13 00:13:00

相关问题