我试图在recyler视图的帮助下水平显示图像,但是即使列表大小为3,recyler视图适配器也只能迭代一项。 我不明白为什么会这样。请帮帮我。
我尝试过将recylerview的layout_height以及行项目的布局保持为wrap_content,但没有用。
图像回收器视图的行项目:item_glance_image_comments.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ffffff"
android:orientation="vertical"
android:layout_margin="@dimen/_5sdp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/img_glanceImagecontent"
android:scaleType="fitXY"
android:src="@drawable/logo"/>
</RelativeLayout>
其中存在recyler视图的item_glance
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#e2e2e2"
android:layout_marginBottom="@dimen/_7sdp"
android:orientation="vertical"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical">
<View
android:id="@+id/line_view"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#eb650d"/>
<LinearLayout
android:id="@+id/layout_glance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/_10sdp"
android:visibility="visible"
android:layout_marginRight="@dimen/_5sdp"
android:layout_marginLeft="@dimen/_5sdp"
tools:ignore="MissingConstraints">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:id="@+id/recycler_glancecontent"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
用于显示图像的适配器:
class GlanceMainImageContentAdapter extends RecyclerView.Adapter<GlanceMainImageContentAdapter.VideoInfoHolder> implements Filterable {
Context ctx;
List<PostContentGlance> data = new ArrayList<>();
List<PostContentGlance> datalistfilter;
public GlanceMainImageContentAdapter(Context ctx, List<PostContentGlance> glanceImageContents) {
this.ctx = ctx;
this.data = glanceImageContents;
this.datalistfilter = data;
}
@NonNull
@Override
public VideoInfoHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_glance_image_comments, parent, false);
return new GlanceMainImageContentAdapter.VideoInfoHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull GlanceMainImageContentAdapter.VideoInfoHolder holder, int position) {
PostContentGlance postContentGlance = datalistfilter.get(position);
int listSize = data.size();
int pos = position;
holder.txt_count_images.setText(pos+1+" / "+listSize);
Glide.with(ctx)
.load(postContentGlance.getPiImage())
.apply(new RequestOptions()
.placeholder(R.drawable.user)
.fitCenter())
.into(holder.img_glanceImagecontent);
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getItemCount() {
return datalistfilter.size();
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
datalistfilter = data;
} else {
List<PostContentGlance> filteredList = new ArrayList<>();
for (PostContentGlance row : data) {
filteredList.add(row);
}
datalistfilter = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = datalistfilter;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
datalistfilter = (ArrayList<PostContentGlance>) filterResults.values;
notifyDataSetChanged();
}
};
}
public class VideoInfoHolder extends RecyclerView.ViewHolder {
ImageView img_glanceImagecontent;
TextView txt_count_images;
public VideoInfoHolder(@NonNull View itemView) {
super(itemView);
img_glanceImagecontent = itemView.findViewById(R.id.img_glanceImagecontent);
txt_count_images = itemView.findViewById(R.id.txt_count_images);
}
}
}
Api响应:
{
"success": true,
"data": [
{
"p_id": 105,
"p_user_id": 2,
"p_access_id": 1,
"p_description": "pdf upload demo",
"p_status": 1,
"p_created_at": "2019-07-22 18:31:39",
"p_updated_at": "2019-07-22 18:31:39",
"p_post_id": null,
"post_user": {
"id": 2,
"first_name": "Anand",
"last_name": "Deshmukh",
"profile_image": "http://ag.homeplanetary.com/uploads/users/1564564830.png"
},
"post_content": [
{
"pi_id": 65,
"pi_post_id": 105,
"pi_image": "http://ag.homeplanetary.com/uploads/glance/1563800499.pdf",
"pi_file_type": "file"
}
],
"post_likes": {
"count": 1,
"isLike": 0
},
"post_shares_count": 0,
"post_comments": {
"count": 4,
"comments": [
{
"c_id": 53,
"c_comment": "hey test",
"c_created_at": "2019-07-29T09:02:11.000000Z",
"user_id": 2,
"user_name": "Anand Deshmukh",
"user_image": "http://ag.homeplanetary.com/uploads/users/1564564830.png"
},
{
"c_id": 52,
"c_comment": "hi",
"c_created_at": "2019-07-29T08:30:06.000000Z",
"user_id": 2,
"user_name": "Anand Deshmukh",
"user_image": "http://ag.homeplanetary.com/uploads/users/1564564830.png"
}
]
},
"user_follow_type": "Unfollow"
},
{
"p_id": 100,
"p_user_id": 2,
"p_access_id": 1,
"p_description": "Base 64 pdf",
"p_status": 1,
"p_created_at": "2019-07-22 10:10:04",
"p_updated_at": "2019-07-22 10:10:04",
"p_post_id": null,
"post_user": {
"id": 2,
"first_name": "Anand",
"last_name": "Deshmukh",
"profile_image": "http://ag.homeplanetary.com/uploads/users/1564564830.png"
},
"post_content": [
{
"pi_id": 61,
"pi_post_id": 100,
"pi_image": "http://ag.homeplanetary.com/uploads/glance/1563770405.pdf",
"pi_file_type": "file"
}
],
"post_likes": {
"count": 1,
"isLike": 0
},
"post_shares_count": 0,
"post_comments": {
"count": 0,
"comments": []
},
"user_follow_type": "Unfollow"
},
{
"p_id": 94,
"p_user_id": 2,
"p_access_id": 1,
"p_description": "Base 64",
"p_status": 1,
"p_created_at": "2019-07-20 18:09:39",
"p_updated_at": "2019-07-20 18:09:39",
"p_post_id": null,
"post_user": {
"id": 2,
"first_name": "Anand",
"last_name": "Deshmukh",
"profile_image": "http://ag.homeplanetary.com/uploads/users/1564564830.png"
},
"post_content": [
{
"pi_id": 56,
"pi_post_id": 94,
"pi_image": "http://ag.homeplanetary.com/uploads/glance/1563626379.png",
"pi_file_type": "image"
}
],
"post_likes": {
"count": 1,
"isLike": 0
},
"post_shares_count": 0,
"post_comments": {
"count": 0,
"comments": []
},
"user_follow_type": "Unfollow"
},
{
"p_id": 55,
"p_user_id": 2,
"p_access_id": 1,
"p_description": null,
"p_status": 1,
"p_created_at": "2019-07-19 11:02:58",
"p_updated_at": "2019-07-19 11:02:58",
"p_post_id": null,
"post_user": {
"id": 2,
"first_name": "Anand",
"last_name": "Deshmukh",
"profile_image": "http://ag.homeplanetary.com/uploads/users/1564564830.png"
},
"post_content": [
{
"pi_id": 45,
"pi_post_id": 55,
"pi_image": "http://ag.homeplanetary.com/uploads/glance/1563514378.1.jpg",
"pi_file_type": "image"
},
{
"pi_id": 46,
"pi_post_id": 55,
"pi_image": "http://ag.homeplanetary.com/uploads/glance/1563514378.2.jpg",
"pi_file_type": "image"
},
{
"pi_id": 47,
"pi_post_id": 55,
"pi_image": "http://ag.homeplanetary.com/uploads/glance/1563514378.3.jpg",
"pi_file_type": "image"
}
],
"post_likes": {
"count": 0,
"isLike": 0
},
"post_shares_count": 0,
"post_comments": {
"count": 0,
"comments": []
},
"user_follow_type": "Unfollow"
}
}
recyler视图适配器集中的glance适配器
public class GlanceAdapter extends RecyclerView.Adapter {
Context ctx;
//Glance Content
public GlanceMainImageContentAdapter glanceMainImageContentAdapter;
List<PostContentGlance> glanceImageContents = new ArrayList<>();
List<PostContentGlance> glanceImageContentssharepopup = new ArrayList<>();
public GlanceYoutubeInnerContentAdapter glanceYoutubeInnerContentAdapter;
public GlanceShareYoutubeInnerContentAdapter glanceShareYoutubeInnerContentAdapter;
List<YoutubeData> glanceYoutubeInnerContents = new ArrayList<>();
SharedPreferences sharedPreferences;
String pdfPath = "";
public GlanceAdapter(FragmentActivity activity, List<DataMultiviewTypeGlance> data) {
this.ctx = activity;
this.data = data;
this.datalistfilter =data;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case 2:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_glance, parent, false);
return new GlanceAdapter.VideoInfoHolder2(view);
}
return null;
}
@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holdermain, final int position) {
sharedPreferences = ctx.getSharedPreferences("agricos", MODE_PRIVATE);
final DataMultiviewTypeGlance listmain = datalistfilter.get(position);
final int duration = 50;
final int pixelsToMove = 100;
final Handler mHandler = new Handler(Looper.getMainLooper());
switch (holdermain.getItemViewType()) {
case 1:
case 2:
final GlanceAdapter.VideoInfoHolder2 holder = (GlanceAdapter.VideoInfoHolder2)holdermain;
mHandler.removeCallbacksAndMessages(null);
final glanceData list = listmain.glanceDataObj;
final glanceData glanceDataShare = listmain.glanceDataObj;
final Boolean[] isClicked = {false};
if((!(list.getPPostId() == null))){}else {
/**************** Start Main Post **********************************************************************/
glanceImageContents.clear();
glanceImageContents = list.getPostContentGlance();
if (glanceImageContents.size() > 0) {
if (glanceImageContents.get(0).getPiFileType().equals("image")) {
holder.recycler_glancecontent.setVisibility(View.VISIBLE);
holder.layout_videoview.setVisibility(View.GONE);
holder.layout_pdfview.setVisibility(View.GONE);
holder.layout_youtubeview.setVisibility(View.GONE);
holder.recycler_glancecontent.setHasFixedSize(true);
//to use RecycleView, you need a layout manager. default is LinearLayoutManage
CustomLinearLayoutManager linearLayoutManager = new CustomLinearLayoutManager(ctx);
linearLayoutManager.setOrientation(CustomLinearLayoutManager.HORIZONTAL);
holder.recycler_glancecontent.setLayoutManager(linearLayoutManager);
glanceMainImageContentAdapter = new GlanceMainImageContentAdapter(ctx, glanceImageContents);
holder.recycler_glancecontent.setAdapter(glanceMainImageContentAdapter);
holder.recycler_glancecontent.scheduleLayoutAnimation();
} else if (list.getPostContentGlance().get(0).getPiFileType().equals("video")) {
holder.recycler_glancecontent.setVisibility(View.GONE);
holder.layout_pdfview.setVisibility(View.GONE);
holder.layout_videoview.setVisibility(View.VISIBLE);
holder.txt_video_description.setText(list.getPDescription());
Uri uri = Uri.parse(list.getPostContentGlance().get(0).getPiImage()); //Declare your url here.
holder.videoview.setMediaController(new MediaController(ctx));
holder.videoview.setVideoURI(uri);
holder.videoview.requestFocus();
// holder.videoview.start();
} else if (list.getPostContentGlance().get(0).getPiFileType().equals("file")) {
holder.recycler_glancecontent.setVisibility(View.GONE);
holder.layout_videoview.setVisibility(View.GONE);
holder.layout_pdfview.setVisibility(View.VISIBLE);
holder.txt_pdf_description.setText(list.getPDescription());
// http://ag.homeplanetary.com/uploads/glance/1563800499.pdf
String[] pdfnameArray = list.getPostContentGlance().get(0).getPiImage().split("/");
int length = pdfnameArray.length;
String pdfname = pdfnameArray[length - 1];
holder.txt_pdf_view.setText(pdfname);
pdfPath = "";
pdfPath = list.getPostContentGlance().get(0).getPiImage();
}
}
}
break;
}
}
@Override
public int getItemViewType(int position) {
switch (datalistfilter.get(position).getType()) {
case 1:
return datalistfilter.get(position).getType();
case 2:
return datalistfilter.get(position).getType();
default:
return -1;
}
}
@Override
public int getItemCount() {
return datalistfilter.size();
}
public class VideoInfoHolder2 extends RecyclerView.ViewHolder {
TextView txt_glanceusername,txt_glancedate,txt_glancelike,txt_glancecomment,
txt_glanceshare;
ImageView user_glanceprofileimg,img_glancelike,img_glancecomment
,img_glanceshare,img_loggedInUser;
LinearLayout layout_comment,layout_glance,layout_sharedpost,layout_comment_shared;
EditText etx_glancecomment,etx_glancecomment_shared;
VideoView videoview;
LinearLayout layout_videoview,layout_youtubeview;
TextView txt_video_description,txt_video_youtube;
RecyclerView recycler_glanceyoutubecontent;
ImageView user_glanceprofileimg_shared,user_glanceprofileimg_shared_in
,img_glancelike_shared_in,img_glancelike_shared,img_glancecomment_shared,img_glanceshare_shared
,img_loggedInUser_shared;
TextView txt_glanceusername_shared,txt_glancedate_shared,txt_glanceusername_shared_in
,txt_glancedate_shared_in,txt_glancelike_shared_in,txt_glancelike_shared,txt_glancecomment_shared,txt_glanceshare_shared;
RecyclerView recycler_glancecomments,recycler_glancecontent,recycler_glancecomments_shared
,recycler_glancecomments_more,recycler_glancecomments_more_share;
RecyclerView recycler_glancecontent_shared_in,recycler_glanceyoutubecontent_shared_in;
LinearLayout layout_videoview_shared_in,layout_youtubeview_shared_in,layout_pdfview,layout_pdfview_shared;
TextView txt_video_description_shared_in,txt_video_youtube_shared_in;
VideoView videoview_shared_in;
Button btn_loadmorecomments,btn_loadmorecomments_share;
TextView txt_pdf_description,txt_pdf_view,txt_pdf_description_shared,txt_pdf_view_shared;
TextView txt_time_glance_shared_in,txt_time_glance_shared,txt_time_glance
,img_glancemenu_shared,img_glancemenu,img_glancecommentsend,img_glancecommentsend_shared;
public VideoInfoHolder2(View itemView) {
super(itemView);
txt_time_glance_shared_in = itemView.findViewById(R.id.txt_time_glance_shared_in);
txt_time_glance_shared_in.setTypeface(FontAwesomeManager.getTypeface(ctx, FontAwesomeManager.FONTAWESOME));
txt_time_glance_shared = itemView.findViewById(R.id.txt_time_glance_shared);
txt_time_glance_shared.setTypeface(FontAwesomeManager.getTypeface(ctx, FontAwesomeManager.FONTAWESOME));
txt_time_glance = itemView.findViewById(R.id.txt_time_glance);
txt_time_glance.setTypeface(FontAwesomeManager.getTypeface(ctx, FontAwesomeManager.FONTAWESOME));
layout_glance = itemView.findViewById(R.id.layout_glance);
//Shared
user_glanceprofileimg_shared = itemView.findViewById(R.id.user_glanceprofileimg_shared);
img_glancemenu_shared = itemView.findViewById(R.id.img_glancemenu_shared);
img_glancemenu_shared.setTypeface(FontAwesomeManager.getTypeface(ctx, FontAwesomeManager.FONTAWESOME));
user_glanceprofileimg_shared_in = itemView.findViewById(R.id.user_glanceprofileimg_shared_in);
img_glancelike_shared_in = itemView.findViewById(R.id.img_glancelike_shared_in);
img_glancelike_shared = itemView.findViewById(R.id.img_glancelike_shared);
img_glancecomment_shared = itemView.findViewById(R.id.img_glancecomment_shared);
img_glanceshare_shared = itemView.findViewById(R.id.img_glanceshare_shared);
txt_glanceusername_shared = itemView.findViewById(R.id.txt_glanceusername_shared);
txt_glancedate_shared = itemView.findViewById(R.id.txt_glancedate_shared);
txt_glanceusername_shared_in = itemView.findViewById(R.id.txt_glanceusername_shared_in);
txt_glancedate_shared_in = itemView.findViewById(R.id.txt_glancedate_shared_in);
txt_glancelike_shared_in = itemView.findViewById(R.id.txt_glancelike_shared_in);
txt_glancelike_shared = itemView.findViewById(R.id.txt_glancelike_shared);
txt_glancecomment_shared = itemView.findViewById(R.id.txt_glancecomment_shared);
txt_glanceshare_shared = itemView.findViewById(R.id.txt_glanceshare_shared);
etx_glancecomment_shared = itemView.findViewById(R.id.etx_glancecomment_shared);
layout_sharedpost = itemView.findViewById(R.id.layout_sharedpost);
layout_comment_shared = itemView.findViewById(R.id.layout_comment_shared);
img_loggedInUser_shared = itemView.findViewById(R.id.img_loggedInUser_shared);
img_glancecommentsend_shared = itemView.findViewById(R.id.img_glancecommentsend_shared);
img_glancecommentsend_shared.setTypeface(FontAwesomeManager.getTypeface(ctx, FontAwesomeManager.FONTAWESOME));
recycler_glancecomments_shared = itemView.findViewById(R.id.recycler_glancecomments_shared);
//main
txt_glanceusername = itemView.findViewById(R.id.txt_glanceusername);
user_glanceprofileimg = itemView.findViewById(R.id.user_glanceprofileimg);
txt_glancedate = itemView.findViewById(R.id.txt_glancedate);
img_glancemenu = itemView.findViewById(R.id.img_glancemenu);
img_glancemenu.setTypeface(FontAwesomeManager.getTypeface(ctx, FontAwesomeManager.FONTAWESOME));
img_glancelike = itemView.findViewById(R.id.img_glancelike);
txt_glancelike = itemView.findViewById(R.id.txt_glancelike);
img_glancecomment = itemView.findViewById(R.id.img_glancecomment);
txt_glancecomment = itemView.findViewById(R.id.txt_glancecomment);
img_glanceshare = itemView.findViewById(R.id.img_glanceshare);
txt_glanceshare = itemView.findViewById(R.id.txt_glanceshare);
layout_comment = itemView.findViewById(R.id.layout_comment);
img_loggedInUser = itemView.findViewById(R.id.img_loggedInUser);
etx_glancecomment = itemView.findViewById(R.id.etx_glancecomment);
img_glancecommentsend = itemView.findViewById(R.id.img_glancecommentsend);
img_glancecommentsend.setTypeface(FontAwesomeManager.getTypeface(ctx, FontAwesomeManager.FONTAWESOME));
videoview = itemView.findViewById(R.id.videoview);
layout_videoview = itemView.findViewById(R.id.layout_videoview);
txt_video_description = itemView.findViewById(R.id.txt_video_description);
recycler_glancecomments = itemView.findViewById(R.id.recycler_glancecomments);
recycler_glancecontent = itemView.findViewById(R.id.recycler_glancecontent);
layout_youtubeview = itemView.findViewById(R.id.layout_youtubeview);
txt_video_youtube = itemView.findViewById(R.id.txt_video_youtube);
recycler_glanceyoutubecontent = itemView.findViewById(R.id.recycler_glanceyoutubecontent);
recycler_glancecontent_shared_in = itemView.findViewById(R.id.recycler_glancecontent_shared_in);
recycler_glanceyoutubecontent_shared_in = itemView.findViewById(R.id.recycler_glanceyoutubecontent_shared_in);
layout_videoview_shared_in = itemView.findViewById(R.id.layout_videoview_shared_in);
layout_youtubeview_shared_in = itemView.findViewById(R.id.layout_youtubeview_shared_in);
txt_video_description_shared_in = itemView.findViewById(R.id.txt_video_description_shared_in);
txt_video_youtube_shared_in = itemView.findViewById(R.id.txt_video_youtube_shared_in);
videoview_shared_in = itemView.findViewById(R.id.videoview_shared_in);
recycler_glancecomments_more = itemView.findViewById(R.id.recycler_glancecomments_more);
recycler_glancecomments_more_share = itemView.findViewById(R.id.recycler_glancecomments_more_share);
btn_loadmorecomments = itemView.findViewById(R.id.btn_loadmorecomments);
btn_loadmorecomments_share = itemView.findViewById(R.id.btn_loadmorecomments_share);
layout_pdfview = itemView.findViewById(R.id.layout_pdfview);
txt_pdf_description = itemView.findViewById(R.id.txt_pdf_description);
txt_pdf_view = itemView.findViewById(R.id.txt_pdf_view);
layout_pdfview_shared = itemView.findViewById(R.id.layout_pdfview_shared);
txt_pdf_description_shared = itemView.findViewById(R.id.txt_pdf_description_shared);
txt_pdf_view_shared = itemView.findViewById(R.id.txt_pdf_view_shared);
}
}
}
答案 0 :(得分:0)
我尝试在Recyler视图的帮助下水平显示图像, 但是Recyler View适配器仅迭代一项,即使列表大小 有3.我不明白为什么会这样。请帮我
我尝试过保持 layout_height 的recylerview以及行 项目布局为 wrap_content ,但不起作用。
如果要水平工作,请将RecyclerView的 layout_width 设置为 wrap_content ,而不是 layout_height 。
还需要将RecyclerView设置为水平
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView myList = (RecyclerView) findViewById(R.id.my_recycler_view);
myList.setLayoutManager(layoutManager);
答案 1 :(得分:0)
请设置布局管理器的水平方向,而不是垂直方向。
LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext,RecyclerView.HORIZONTAL,false);