gpt4 book ai didi

android - Viewpager 未显示在 RecyclerView 行内

转载 作者:IT老高 更新时间:2023-10-28 23:08:36 25 4
gpt4 key购买 nike

我想制作一个“照片详细信息” Activity 或 fragment ,在其中我将照片显示在其顶部和下方的 aViewpPager 中,显示相关照片的评论和喜欢(2 个选项卡)。为了使屏幕“可滚动”,以便我可以在评论和喜欢上向上/向下滚动并向左/向右滑动,我决定使用具有 2 行的 RecyclerView:

第 1 行:照片 (ImageView)。

第 2 行:SlidingTabLayout + ViewPager + FragmentPagerAdapter。

代码编译运行,显示图片和slidingTabLayout,但不显示ViewPager。

所以我的两个主要问题是:

1-我的实现有什么问题。

2-对于我想要实现的目标是否有替代或更好的解决方案?

注意:我不想使用带有 header 的 listView。我想使用 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<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>

最佳答案

我遇到了这个问题,并通过为每个 ViewPager 设置 ID 解决了这个问题 :) ViewPager 不允许在同一 fragment 中共享 id,即使它是 recyclerview 上下文的一部分。

pagerHolder.pager.setId(position);

关于android - Viewpager 未显示在 RecyclerView 行内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29152674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com