Viewpager滑动不适用于回收站视图

时间:2018-10-22 19:35:20

标签: android android-recyclerview android-viewpager swipe

我在同一Activity中有一个ViewPager和RecyclerView,两个视图重叠(RecyclerView在前面,ViewPager在后面)。当我在空白处滑动时,我的ViewPager滑动正常工作,但是在RecyclerView上滑动时,viewpager滑动不起作用。我在Recyclerview中有按钮和搜索栏。我还想在用户也滑动除Ridebar之外的RecyclerView时滑动ViewPager。

我的布局是:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/viewPager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />

    <RelativeLayout
        android:id="@+id/imagesLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bottomRelativeLayout"
        android:layout_below="@+id/timerLayout"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:visibility="gone">


        <android.support.v7.widget.RecyclerView
            android:id="@+id/myRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="5dp"
            android:clickable="true"
            android:fadingEdgeLength="30dp"
            android:focusable="true"
            android:gravity="center"
            android:horizontalSpacing="3dp"
            android:listSelector="@android:color/transparent"
            android:numColumns="3"
            android:overScrollMode="never"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingBottom="15dp"
            android:paddingTop="15dp"
            android:requiresFadingEdge="vertical"
            android:scrollbars="none" />


    </RelativeLayout>

我的Java代码是:

 imagesRecyclerView = (RecyclerView) findViewById(R.id.myRecyclerView);
        int numberOfColumns = 3;
        imagesRecyclerView.setLayoutManager(new GridLayoutManager(context, numberOfColumns));
        imagesRecyclerView.getItemAnimator().setChangeDuration(0);
        imagesRecyclerView.setHasFixedSize(false);

我搜索了很多有关此主题的内容,但找不到任何解决方案。我的viewpager适配器如下:

public class ViewPagerAdapter extends FragmentStatePagerAdapter {
    FragmentManager fm;
    private SparseArray<Fragment> registeredFragments = new SparseArray<>();

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
        this.fm=fm;


    }


    @Override
    public Fragment getItem(int position) {

        Fragment fragment = new ImageFragment();

        Bundle args = new Bundle();

        args.putSerializable("TabObject", Constants.tabs.get(position));

        fragment.setArguments(args);


        return fragment;
    }

    @Override
    public int getCount() {
        return Constants.tabs.size();
    }

    @SuppressLint("RestrictedApi")
    public List<Fragment> getFragments(){
        return fm.getFragments();
    }

    @Override
    public int getItemPosition(Object object) {
        if (object instanceof ImageFragment) {
            // Create a new method notifyUpdate() in your fragment
            // it will get call when you invoke
            // notifyDatasetChaged();
          //  ((SoundsFragment) object).updateGridData();
        }
        //don't return POSITION_NONE, avoid fragment recreation.
        return super.getItemPosition(object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        registeredFragments.put(position, fragment);
        return fragment;
    }

    public Fragment getRegisteredFragment(int position) {
        return registeredFragments.get(position);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "OBJECT " + (position + 1);
    }

}

1 个答案:

答案 0 :(得分:0)

您可以使用

1)

aliases

2)在您的recylerView标签内

ViewCompat.setNestedScrollingEnabled(recyclerView, false);