在ViewPager里面的RecyclerView里面另一个RecyclerView里面没有滚动

时间:2017-01-01 18:04:27

标签: java android android-fragments android-viewpager android-recyclerview

我试图制作像Vine / Instagram这样的用户个人资料。我有个人资料片段,里面有一个包含两个不同ViewHolders的RecyclerView。其中一个是标题(用户的个人资料图片,用户名等),另一个是TabLayout和ViewPager。

ProfileAdapter.java

private static final int TYPE_HEADER = 1;
private static final int TYPE_PAGER = 2;

private Context mContext;
private FragmentManager mFragmentManager;

public ProfileAdapter(Context context, FragmentManager fragmentManager) {
    this.mContext = context;
    this.mFragmentManager = fragmentManager;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder viewHolder;
    LayoutInflater inflater = LayoutInflater.from(mContext);

    if (viewType == TYPE_HEADER) {
        View view = inflater.inflate(R.layout.layout_profile_header, parent, false);
        viewHolder = new ProfileHeaderViewHolder(view);
    } else {
        View view = inflater.inflate(R.layout.layout_profile_view_pager, parent, false);
        viewHolder = new ProfileViewPagerViewHolder(view);
    }

    return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    if (holder instanceof ProfileHeaderViewHolder) {

    } else if (holder instanceof ProfileViewPagerViewHolder) {
        ((ProfileViewPagerViewHolder) holder).viewPager
                .setAdapter(new ProfileViewPagerViewHolder.ProfilePagerAdapter(mFragmentManager));
    }
}

@Override
public int getItemViewType(int position) {

    if (position == 0) {
        return TYPE_HEADER;
    } else {
        return TYPE_PAGER;
    }
}

@Override
public int getItemCount() {
    return 2;
}

ProfileHeaderViewHolder.java

public class ProfileHeaderViewHolder extends RecyclerView.ViewHolder {

private CircleImageView profilePic;

private TextView nameText;
private TextView bioText;
private TextView urlText;

private TextView postsCountText;
private TextView followersCountText;
private TextView followingsCountText;

private TabLayout tabLayout;

public ProfileHeaderViewHolder(View itemView) {
    super(itemView);

    profilePic = (CircleImageView) itemView.findViewById(R.id.iv_profile_pic);
    nameText = (TextView) itemView.findViewById(R.id.tv_name);
    urlText = (TextView) itemView.findViewById(R.id.tv_url);
    bioText = (TextView) itemView.findViewById(R.id.tv_bio);

    postsCountText = (TextView) itemView.findViewById(R.id.tv_posts);
    followersCountText = (TextView) itemView.findViewById(R.id.tv_followers);
    followingsCountText = (TextView) itemView.findViewById(R.id.tv_followings);
}
}

ProfileViewPagerViewHolder.java

public class ProfileViewPagerViewHolder extends RecyclerView.ViewHolder {

public TabLayout tabLayout;
public ViewPager viewPager;

public ProfileViewPagerViewHolder(View itemView) {
    super(itemView);

    tabLayout = (TabLayout) itemView.findViewById(R.id.tab_layout);
    viewPager = (ViewPager) itemView.findViewById(R.id.vp_profile);

    tabLayout.addTab(tabLayout.newTab().setText("Posts"));
    tabLayout.addTab(tabLayout.newTab().setText("Dares"));

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
}

public static class ProfilePagerAdapter extends FragmentPagerAdapter {

    public ProfilePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {

            case 0:
                return new ProfilePostsFragment();

            case 1:
                return new ProfileDaresFragment();

            default:
                return null;
        }
    }

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

我不确定这是否是获得我想要达到的目标的好方法。我希望用户能够在用户配置文件片段中的两个选项之间切换。

fragment_profile.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<android.support.v7.widget.RecyclerView
    android:id="@+id/rv_profile"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

layout_profile_view_pager.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/colorGreyLight" />

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    app:tabGravity="fill"
    app:tabMode="fixed" />

<android.support.v4.view.ViewPager
    android:id="@+id/vp_profile"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

到目前为止,我所做的大部分工作似乎都有效,我遇到的唯一问题是我可以看到第一项,但我无法进一步向下滚动以查看rv_profile_posts RecyclerView中的其余项目

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

为什么使用recyclerView?

  

您可以使用coordinatorLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/header_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="vertical"
        android:visibility="visible"
        app:layout_scrollFlags="scroll|enterAlways|snap">


        <!-- put your header widget here -->

    </LinearLayout>


    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        app:tabGravity="fill"
        app:tabMode="fixed" />

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/_1dp"
        android:background="@color/line_color" />
</android.support.design.widget.AppBarLayout>


<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v4.view.ViewPager
        android:id="@+id/vp_profile"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

答案 1 :(得分:0)

请在这种情况下在RecyclerView上设置触摸侦听器

yourRecyclerview.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            v.getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    });

答案 2 :(得分:0)

科特琳:

遇到相同的问题,请尝试添加以下内容:-

import React, { useState, useEffect, useRef } from "react"; import { makeStyles } from "@material-ui/core/styles"; import cornerstone from "cornerstone-core"; import cornerstoneMath from "cornerstone-math"; import cornerstoneTools from "cornerstone-tools"; import Hammer from "hammerjs"; cornerstoneTools.external.cornerstone = cornerstone; cornerstoneTools.external.Hammer = Hammer; cornerstoneTools.external.cornerstoneMath = cornerstoneMath; cornerstoneTools.init(); export default function Dummy() { const dicomRef = useRef(); const imageIds = [ "wadouri:http://127.0.0.1:8080/api/challenges/1/series/1/images/1", "wadouri:http://127.0.0.1:8080/api/challenges/1/series/2/images/1", "wadouri:http://127.0.0.1:8080/api/challenges/1/series/3/images/1", ]; // define the stack const stack = { currentImageIdIndex: 0, imageIds, }; // Add our tool, and set it's mode const { LengthTool } = cornerstoneTools; useEffect(() => { // Enable the DOM Element for use with Cornerstone const element = document.getElementById("navbar"); cornerstone.enable(element); cornerstone.loadImage(`${imageIds[0]}`).then((image) => { cornerstone.displayImage(element, image); cornerstoneTools.addTool(LengthTool); cornerstoneTools.addToolForElement(element, LengthTool); cornerstoneTools.setToolActive(LengthTool.name, { mouseButtonMask: 1 }); }); }, []); return <div ref={dicomRef}>aaa</div>; }

这将禁用viewpager的滚动,但会启用recyclerview滚动。