我有一个带书的ListView。当我单击该行时,我可以看到新片段中的书籍详细信息,类似于Training Android中的内容。我想滑动书籍(从列表中向右->下一个元素,向左
问题
如何将ViewPager与片段整合在一起?当我单击列表上的元素时,将调用FragmentTransaction。

活动:

public class MyActivity extends FragmentActivity implements RecyclerViewFragment.OnFragmentInteractionListener {

    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private ViewPager mPager;
    private PagerAdapter mPagerAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        final Fragment mFragment = new RecyclerViewFragment();
        if (savedInstanceState == null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.container, mFragment)
                    .commit();
        }
    }

    @Override
    public void onFragmentInteraction(int id) {

        Log.d("Element Id ", String.valueOf(id));

        PagerViewFragment newFragment = new PagerViewFragment();
        Bundle args = new Bundle();
        args.putInt(PagerViewFragment.ARG_POSITION, id);
        newFragment.setArguments(args);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
        transaction.replace(R.id.container, newFragment);
        transaction.addToBackStack(null);

        transaction.commit();
    }


如何告知应该改变观点的片段?
我应该使用此代码吗?

mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);


编辑
 在ILovemyPancho之后再次回答堆栈。 java.lang.NullPointerException:

PagerViewFragment:

public class PagerViewFragment extends Fragment {
    final static String ARG_POSITION = "position";
    private ViewPager mPager;
    private PagerAdapter mPagerAdapter;

    public PagerViewFragment() {
    }

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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        ViewGroup viewRoot = (ViewGroup) inflater.inflate(R.layout.pager_fragment, container, false);
        mPager = (ViewPager) container.findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getChildFragmentManager());
        mPager.setAdapter(mPagerAdapter);  //line 39. Error is here

        return viewRoot;
    }

    @Override
    public void onStart() {
        super.onStart();
    }

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

        @Override
        public Fragment getItem(int position) {
            PagerViewFragment frag = new PagerViewFragment();

            Bundle args = new Bundle();
            args.putString("BookTitle", "FD");

            DummyContent dummyContent = new DummyContent(); //elements

            TextView street = (TextView) getView().findViewById(R.id.street_article);
            street.setText(dummyContent.murals.get(position).getStreet());

            TextView artist = (TextView) getView().findViewById(R.id.artist_article);
            artist.setText(dummyContent.murals.get(position).getArtists().get(0).getName());
            frag.setArguments(args);

            return frag;
        }

        @Override
        public int getCount() {
            return DummyContent.murals.size();
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
    }
}


我的活动:

public class MyActivity extends FragmentActivity implements RecyclerViewFragment.OnFragmentInteractionListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        final Fragment mFragment = new RecyclerViewFragment();
        if (savedInstanceState == null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.container, mFragment)
                    .commit();
        }
    }

    @Override
    public void onFragmentInteraction(int id) {

        Log.d("Element Id ", String.valueOf(id));

        PagerViewFragment newFragment = new PagerViewFragment();
        Bundle args = new Bundle();
        args.putInt(PagerViewFragment.ARG_POSITION, id);
        newFragment.setArguments(args);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
        transaction.replace(R.id.container, newFragment);
        transaction.addToBackStack(null);

        transaction.commit();
    }
}


activity_screen_slide.xml

<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />


pager_fragmet.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/blank_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.szymon.fragmentexample.BlankFragment">

        <TextView
            android:id="@+id/street_article"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:layout_marginTop="50dp"
            android:text="hello_blank_fragment"
            android:textSize="23dp" />

        <TextView
            android:id="@+id/artist_article"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/street_article"
            android:layout_marginLeft="50dp"
            android:layout_marginTop="10dp"
            android:text="hello_blank_fragment"
            android:textSize="15dp" />


        <TextView
            android:id="@+id/picture"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/artist_article"
            android:layout_marginLeft="50dp"
            android:layout_marginTop="100dp"
            android:text="here will be the picture"
            android:textSize="15dp" />


    </RelativeLayout>
</ScrollView>


我收到一个错误:


  java.lang.NullPointerException
              在com.example.szymon.recyclerview.PagerViewFragment.onCreateView(PagerViewFragment.java:39)
              在android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)

最佳答案

好的,所以您有一个活动正在布置ListFragment(RecyclerViewFragment)。当您单击列表项时,ListFragment将替换为PagerViewFragment,显示该书的详细信息。

尝试这个。创建一个包含ViewPager的片段。单击某个项目时,此片段将替换您的ListFragment。让我们称之为BookDetailsContainer,它将接收列表项ID:

 BookDetailsContainer newFragment = BookDetailsContainer.newInstance(id);


在此片段内,您设置了viewpager:

mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getChildFragmentManager());
mPager.setAdapter(mPagerAdapter);


ViewPager需要getChildFragmentManager()来处理嵌套的片段(BookDetailsContainer中的PagerViewFragment)。并且由于将列表位置ID传递给BookDetailsContainer,因此可以设置位置:

mPager.setCurrentItem(positionId);


在您的ScreenSlidePagerAdapter中,您将创建一个PagerViewFragment实例,
根据以下信息从数据源(数组,游标...)获取要显示的信息
位置,使用Bundle将数据传递给片段,然后返回片段:

@Override
public Fragment getItem(int position) {
    PagerViewFragment frag = new PagerViewFragment();
    Bundle args = new Bundle();

    //get data from your data source based on the "position"

    args.putString("BookTitle", ...);
    args.putString("BookAuthor", ...);
    ...

    frag.setArguments(args);
    return frag;
}


这基本上就是您可以做到的。

10-08 04:02