编辑:我想通了,谢谢
我现在一直在尝试在我的每个 ViewPager 页面中实现单独的 ListViews。我非常了解本教程,但它只创建一个用于每个页面的 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) {
}