I want to make a 'Photo details' activity or fragment where i display the photo on top and below it aViewpPager that display both comments and likes of the related photo(2 tabs). In order to make the screen 'Scrollable' so i can scroll up/down on both comments and likes and slide left/right i decided to use a RecyclerView with 2 rows :
ROW 1 : The photo (ImageView).
ROW 2 : SlidingTabLayout + ViewPager + FragmentPagerAdapter.
The code compile and run, display the image and the slidingTabLayout but not the ViewPager.
So my two main questions are :
1-What's wrong with my implementation.
2-Is there an alternative or better solution for what i want to achieve ?
Note : I don't want to use a listView with header.I want to use RecyclerView because it's easier to add elements on top/bottom from network.
PhotoDetailsActivity.java
public class MainActivity extends ActionBarActivity { RecyclerView recyclerViewPhotoDetails; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.recyclerViewPhotoDetails = (RecyclerView) this.findViewById(R.id.recycler_view_photo_details); this.recyclerViewPhotoDetails.setLayoutManager(new LinearLayoutManager(this)); this.recyclerViewPhotoDetails.setAdapter(new PhotoDetailsRecyclerAdapter(this.getSupportFragmentManager())); } }
PhotosDetailsRecyclerAdapter.java
public class PhotoDetailsRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private static final int ROW_IMAGE = 0; private static final int ROW_LIKES_AND_COMMENTS = 1; private static final int TOTAL_ROWS = 2; private FragmentManager fragmentManager; public PhotoDetailsRecyclerAdapter(FragmentManager fragmentManager) { this.fragmentManager = fragmentManager; } @Override public int getItemViewType(int position) { if (position == 0) { return ROW_IMAGE; } else { return ROW_LIKES_AND_COMMENTS; } } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if(viewType == ROW_IMAGE) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_image, parent, false); return new ImageViewHolder(view); } else { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_comments_and_likes, parent, false); return new CommentsAndLikesViewHolder(view); } } @Override public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) { } @Override public int getItemCount() { return TOTAL_ROWS; } public class ImageViewHolder extends RecyclerView.ViewHolder { public ImageViewHolder(View itemView) { super(itemView); } } public class CommentsAndLikesViewHolder extends RecyclerView.ViewHolder { private SlidingTabLayout slidingTabLayout; private ViewPager viewPager; public CommentsAndLikesViewHolder(View view) { super(view); slidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tab_layout_comments_and_likes); viewPager = (ViewPager) view.findViewById(R.id.view_pager_comments_and_likes); viewPager.setAdapter(new CommentsAndLikesPagerAdapter(fragmentManager)); slidingTabLayout.setDistributeEvenly(true); slidingTabLayout.setViewPager(viewPager); } } }
activity_main.xml
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/recycler_view_photo_details" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent" />
layout_image.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:adjustViewBounds="true" android:scaleType="centerCrop" android:src="@drawable/img" /> </FrameLayout>
layout_comments_and_likes.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <org.bitbucket.androidapp.SlidingTabLayout android:id="@+id/sliding_tab_layout_comments_and_likes" android:layout_width="match_parent" android:layout_height="48dp" android:background="@android:color/darker_gray" /> <android.support.v4.view.ViewPager android:id="@+id/view_pager_comments_and_likes" android:layout_height="match_parent" android:layout_width="match_parent" android:background="@android:color/holo_blue_dark" /> </LinearLayout>
CommentsAndLikesPagerAdapter.java
public class CommentsAndLikesPagerAdapter extends FragmentPagerAdapter { private static final int TOTAL_TABS = 2; private String[] tabs = { "comments", "likes" }; public CommentsAndLikesPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { if(position == 0) { return new CommentsFragment(); } else { return new LikesFragment(); } } @Override public CharSequence getPageTitle(int position) { return tabs[position]; } @Override public int getCount() { return TOTAL_TABS; } }
CommentsFragment.java
public class CommentsFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_comments, container, false); RecyclerView recyclerViewComments = (RecyclerView) view.findViewById(R.id.recycler_view_comments); recyclerViewComments.setLayoutManager(new LinearLayoutManager(this.getActivity())); recyclerViewComments.setAdapter(new CommentsRecyclerAdapter()); return view; } }
fragment_comments.xml
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/recycler_view_comments" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent" />
LikesFragment.java
public class LikesFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_likes, container, false); RecyclerView recyclerViewLikes = (RecyclerView) view.findViewById(R.id.recycler_view_likes); recyclerViewLikes.setLayoutManager(new LinearLayoutManager(this.getActivity())); recyclerViewLikes.setAdapter(new LikesRecyclerAdapter()); return view; } }
fragment_likes.xml
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/recycler_view_likes" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent" />
layout_comment.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Comment" /> </RelativeLayout>
layout_like.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Like" /> </RelativeLayout>
I've faced this problem and resolved this by set ID for each ViewPager :) ViewPager does not allow sharing of the id in the same fragment, even if it is part of a recyclerview context.
pagerHolder.pager.setId(position);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With