0

我正在試圖製作一個像Vine/Instagram的用戶配置文件。我有配置文件片段,裏面有一個包含兩個不同ViewHolders的RecyclerView。其中之一是標題(用戶的個人資料圖片,用戶名等),另一個是TabLayout和ViewPager。RecyclerView裏面的另一個RecyclerView裏面的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中的剩餘項目。

任何幫助將不勝感激。

回答

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> 
0

請設置觸摸監聽器RecyclerView在這種情況下

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