[英]RecyclerView inside a ViewPager inside another RecyclerView not scrolling
I'm trying to make a user profile like Vine/Instagram.我正在尝试制作像 Vine/Instagram 这样的用户个人资料。 I have profile fragment and inside that I have a RecyclerView containing two different ViewHolders.我有配置文件片段,里面有一个包含两个不同 ViewHolders 的 RecyclerView。 One of them is the header (user's profile picture, username, etc.) and the other one is a TabLayout and ViewPager.其中之一是标题(用户的个人资料图片、用户名等),另一个是 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 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 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;
}
}
}
I'm not sure if this is a good way to obtain what I'm trying to achieve.我不确定这是否是获得我想要实现的目标的好方法。 I want the user to be able to switch between two options inside the user profile fragment.我希望用户能够在用户配置文件片段内的两个选项之间切换。
fragment_profile.xml 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 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>
Most of what I've done so far seems to work and the only problem I'm encountering is that I can see the first item but I cannot scroll down further to see the remaining items in the rv_profile_posts RecyclerView.到目前为止我所做的大部分工作似乎都有效,我遇到的唯一问题是我可以看到第一个项目,但我无法进一步向下滚动以查看 rv_profile_posts RecyclerView 中的其余项目。
Any help would be much appreciated.任何帮助将非常感激。
please set touch listener on RecyclerView in this case在这种情况下,请在 RecyclerView 上设置触摸侦听器
yourRecyclerview.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
Why your using recyclerView?为什么你使用recyclerView?
You can use the coordinatorLayout您可以使用 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>
Kotlin :科特林:
Faced the same issue, try adding this:-遇到同样的问题,请尝试添加:-
columnRecyclerView.setOnTouchListener { v, event -> v.parent.requestDisallowInterceptTouchEvent(true) false }
this will disable the scrolling of your viewpager but enable recyclerview scroll.这将禁用 viewpager 的滚动,但启用 recyclerview 滚动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.