android v7包没有viewpage,android – Viewpager里面没有显示RecyclerView行

本文介绍如何使用RecyclerView在Android应用中创建一个显示照片及其相关评论和点赞的界面,通过SlidingTabLayout实现两个选项卡的滚动切换。作者探讨了现有代码的问题,并寻求更优实现方案,避免使用headerView。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我想制作一个“照片详细信息”活动或片段,我在其顶部和下方显示照片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"

/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值