我正在尝试制作诸如Vine/instagram之类的用户配置文件。我有个人资料片段,在内部,我有一个包含两个不同视图持有人的循环系统。其中一个是标题(用户的个人资料图片,用户名等),另一个是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中的其余项目。
任何帮助都将不胜感激。
在这种情况下,请在recyclerview上设置触摸听众
yourRecyclerview.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
为什么您使用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>
kotlin:
面对同样的问题,尝试添加以下问题: -
columnRecyclerView.setOnTouchListener { v, event ->
v.parent.requestDisallowInterceptTouchEvent(true)
false
}
这将禁用您的ViewPager滚动,但启用Recyclerview滚动。