PagerView与片段

时间:2014-08-21 14:51:58

标签: android android-fragments android-viewpager

我有一本带有书籍的 ListView 。当我点击该行时,我可以在新的片段中查看书籍详细信息,类似于Training Android。我想滑动书籍(右边 - >列表中的下一个元素,左边< - 列表中的前一个元素)。

问题
如何将 ViewPager 与fragements集成?当我单击列表中的元素时,会调用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;
        }
    }
}

MyActivity:

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)

1 个答案:

答案 0 :(得分:1)

好的,所以你有一个活动推迟了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实例, 根据数据获取要从数据源(数组,光标...)显示的信息 position,使用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;
}  

这基本上就是你能做到的。