1

编辑:我想通了,谢谢

我现在一直在尝试在我的每个 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) {

}
4

2 回答 2

0

您必须使用 bundle 在片段之间发送参数。

从 Fragment1 发送:

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")
于 2012-04-12T10:19:12.603 回答
0

尝试这个...

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Log.i("FragmentList", "Item clicked: " + id);
    switch(mNum){
       case 1:
        //do what you want for first page
        Log.i("FragmentList", "Page 1: Item clicked: " + id);
        break;
       case 2:
       //do what you want for second page;
       Log.i("FragmentList", "Page 2: Item clicked: " + id);
       break;
       default:
       //default case;
       break;
   }
}
于 2012-04-12T00:54:29.590 回答