我想制作一个“照片详细信息”活动或片段,我在其顶部和下方显示照片aViewpPager显示相关照片的评论和喜欢(2个选项卡).
为了使屏幕“滚动”,所以我可以向上/向下滚动两个评论和喜欢,滑动左/右我决定使用一个RecyclerView与2行:
ROW 1:照片(ImageView).
ROW 2:SlidingTabLayout ViewPager FragmentPagerAdapter.
代码编译并运行,显示图像和slidingTabLayout,但不显示ViewPager.
所以我的两个主要问题是:
1 – 我的实现有什么问题
2 – 有没有一个替代或更好的解决方案,我想实现什么?
注意:我不想使用headerView with header.I想使用RecyclerView,因为它更容易在网络上/下添加元素.
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 {
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中
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
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/img"
/>
layout_comments_and_likes.xml
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
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:id="@+id/view_pager_comments_and_likes"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@android:color/holo_blue_dark"
/>
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
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
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
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Comment"
/>
layout_like.xml
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Like"
/>