Android:ListView详细分页

时间:2013-09-29 20:25:10

标签: android android-listview android-fragments android-pageradapter

以下是我的设置:我有MainActivity,其中包含以下代码:

public class MainActivity extends FragmentActivity {
    FragmentList listFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        listFragment = new FragmentList();

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.list_container, listFragment);

        fragmentTransaction.commit();
    }
}

然后我有FragmentList,这是ListView片段和FragmentDetail,这是选择特定ListView行时显示的片段。

这是FragmentList代码的片段:

public class FragmentList extends Fragment {
    private ListView list;
    private FragmentDetail detailFragment;
    private long detailId;
    private ViewPager mPager;
    private PagerAdapter mPagerAdapter;

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

        // ListView gets set via an adapter

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                detailId = id;
                mPager = (ViewPager)getActivity().findViewById(R.id.pager);
                mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
                mPager.setAdapter(mPagerAdapter);

                final FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

                fragmentTransaction.replace(R.id.list_container, detailFragment);
                fragmentTransaction.commit();
            }
        });
    }

    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            detailFragment = new FragmentDetail();
            Bundle mBundle = new Bundle();
            mBundle.putLong("detailId", detailId);
            detailFragment.setArguments(mBundle);
            return detailFragment;
        }

        @Override
        public int getCount() {
            return 5;
        }
    }
}

FragmentDetail

public class FragmentDetail extends Fragment {
    private long id;

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

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();
        id = bundle.getLong("detailId");

        // Set views based on id
    }
}

以下是我的问题:

  1. fragmentTransaction.replace(R.id.list_container, detailFragment);中的行FragmentList错误地说我无法更改容器的ID。那是为什么?
  2. 在我对问题#1中的行进行评论后,细节片段加载正常并让我滑动。但是,只有一个详细信息片段页面包含任何数据;其余的都是空白的。如何实现以下内容:说我的ListView包含&#34; iOS&#34;,&#34; Android&#34;,&#34; Windows&#34;行。当我点击&#34; iOS&#34;时,它会显示iOS的详细信息片段。当我向左滑动时,我想要&#34; Android&#34;细节片段出现;然后当我再次刷卡时,&#34; Windows&#34;细节片段等
  3. 谢谢!

0 个答案:

没有答案