当前位置: 首页 > 知识库问答 >
问题:

RecyclerView得内容不可见

陈鸿才
2023-03-14

我正在尝试创建一个ListView,它的每一行都是一个水平滚动列表。早些时候,我使用的是HorizontalScrollView,但我需要一个可以回收视图的布局。所以我使用了RecyclerView,但是现在RecyclerView的内容不可见了。

下面是我的垂直ListView适配器:

public class FeedAdapter extends BaseAdapter {
    private Context context;
    private FeedItem feedItem;
    private static LayoutInflater inflater = null;
    private Picasso picasso;
    Typeface opensans;

    public FeedAdapter(Context context, FeedItem feedItem) {
        this.context = context;
        this.feedItem = feedItem;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        opensans = Typeface.createFromAsset(context.getAssets(), "fonts/OpenSans-Regular.ttf");
        OkHttpClient okHttpClient = new OkHttpClient();
        picasso = new Picasso.Builder(context)
                .downloader(new OkHttpDownloader(okHttpClient))
                .build();
    }


    @Override
    public int getCount() {
        return feedItem.getFeedList().size();
    }

    @Override
    public FeedList getItem(int position) {
        return feedItem.getFeedList().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final FeedRowViewHolder rowViewHolder;

        if (view == null) {
            view = inflater.inflate(R.layout.feed_row, parent, false);
            rowViewHolder = new FeedRowViewHolder(view);
            view.setTag(rowViewHolder);
        } else {
            rowViewHolder = (FeedRowViewHolder) view.getTag();
        }

        final FeedList feedList = feedItem.getFeedList().get(position);

        picasso.with(context)
                .load(feedList.getThumbnailUrl())
                .resize(120, 120)
                .centerCrop()
                .into(rowViewHolder.galleryThumb);
        rowViewHolder.galleryThumb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String api_url = feedList.getUrl();
                String delims = "/";
                String[] tokens = api_url.split(delims);
                String thumbId = tokens[5];
                ((HomeActivity) context).changeIntent(thumbId);
            }
        });

        rowViewHolder.galleryName.setText(feedList.getGalleryName());
        rowViewHolder.galleryName.setTypeface(opensans);
        rowViewHolder.galleryName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String api_url = feedList.getUrl();
                String delims = "/";
                String[] tokens = api_url.split(delims);
                String thumbId = tokens[5];
                ((HomeActivity) context).changeIntent(thumbId);
            }
        });

        rowViewHolder.timestamp.setText(feedList.getCreatedDate());
        rowViewHolder.timestamp.setTypeface(opensans);

        rowViewHolder.followButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                picasso.with(context)
                        .load(R.drawable.cta_button_follow_secondary_state)
                        .into(rowViewHolder.followButton);
            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
        rowViewHolder.recyclerView.setLayoutManager(layoutManager);
        FeedItemAdapter adapter = new FeedItemAdapter(context, feedList.getPhotos());
        rowViewHolder.recyclerView.setAdapter(adapter);

        return view;
    }

    static class FeedRowViewHolder {
        @Bind(R.id.imageView75)
        ImageView galleryThumb;
        @Bind(R.id.textView65)
        TextView galleryName;
        @Bind(R.id.textView66)
        TextView timestamp;
        @Bind(R.id.imageView162)
        ImageView followButton;
        @Bind(R.id.recyclerView)
        RecyclerView recyclerView;

        public FeedRowViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}

下面是我的水平回收视图适配器

public class FeedItemAdapter extends RecyclerView.Adapter<FeedItemAdapter.ViewHolder> {
    private List<Photo> list;
    private Context context;
    private Picasso picasso;
    Typeface opensans;

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.feed_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final Photo photo = getValueAt(position);

        picasso.with(context)
                .load(photo.getPhotoUrl())
                .resize(1020, 768)
                .centerCrop()
                .into(holder.image);
        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String photo_url = photo.getPhoto();
                String delims = "/";
                String[] tokens = photo_url.split(delims);
                String photoId = tokens[5];
                ((HomeActivity) context).setBackImage(photoId);
            }
        });

        picasso.with(context)
                .load(photo.getProfilePic())
                .resize(120, 120)
                .centerCrop()
                .transform(new CircleTransform())
                .into(holder.profilePic);
        holder.profilePic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String user_url = photo.getOwner();
                String delims = "/";
                String[] tokens = user_url.split(delims);
                String photoId = tokens[5];
                ((HomeActivity) context).showUserProfile(photoId);
            }
        });

        holder.commentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((HomeActivity) context).showComments();
            }
        });

        holder.fiveImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((HomeActivity) context).showLikes();
            }
        });

        holder.commentImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((HomeActivity) context).showComments();
            }
        });

        holder.username.setText(photo.getUserName());
        holder.username.setTypeface(opensans);

        holder.userFives.setText(Integer.toString(photo.getUserFives()) + " Fives");
        holder.userFives.setTypeface(opensans);

        holder.caption.setText(photo.getCaption());
        holder.caption.setTypeface(opensans);

        final int[] fives = {photo.getPhotoFives()};
        holder.numFives.setText(Integer.toString(photo.getPhotoFives()));
        holder.numFives.setTypeface(opensans);

        holder.numComments.setText(Integer.toString(photo.getNumComments()));
        holder.numComments.setTypeface(opensans);

        final boolean[] liked = {false};
        holder.fiveButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    holder.fiveButton.setText("Hi Fiveeed!");
                    holder.fiveButton.setTypeface(opensans);
                    holder.fiveButton.setTextColor(Color.parseColor("#FAC80A"));

                    picasso.with(context)
                            .load(R.drawable.cta_ic_five_pressed_state)
                            .into(holder.fiveImage);
                }
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    holder.fiveButton.setText("Five This!");
                    holder.fiveButton.setTypeface(opensans);
                    holder.fiveButton.setTextColor(Color.parseColor("#707070"));

                    picasso.with(context)
                            .load(R.drawable.user_profile_activity_1_ic_five_count)
                            .into(holder.fiveImage);
                }
                if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
                    holder.fiveButton.setText("Five This!");
                    holder.fiveButton.setTypeface(opensans);
                    holder.fiveButton.setTextColor(Color.parseColor("#707070"));

                    picasso.with(context)
                            .load(R.drawable.user_profile_activity_1_ic_five_count)
                            .into(holder.fiveImage);
                }
                return false;
            }
        });
        holder.fiveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (liked[0] == false) {
                    holder.fiveButton.setText("Hi Fived!");
                    holder.fiveButton.setTypeface(opensans);
                    holder.fiveButton.setTextColor(Color.parseColor("#FAC80A"));

                    holder.numFives.setText(Integer.toString(fives[0] + 1));
                    fives[0] = fives[0] + 1;

                    picasso.with(context)
                            .load(R.drawable.cta_ic_five_pressed_state)
                            .into(holder.fiveImage);

                    liked[0] = true;
                } else {
                    holder.fiveButton.setText("Five This!");
                    holder.fiveButton.setTypeface(opensans);
                    holder.fiveButton.setTextColor(Color.parseColor("#707070"));

                    holder.numFives.setText(Integer.toString(fives[0] - 1));
                    fives[0] = fives[0] - 1;

                    picasso.with(context)
                            .load(R.drawable.user_profile_activity_1_ic_five_count)
                            .into(holder.fiveImage);

                    liked[0] = false;
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public Photo getValueAt(int position) {
        return list.get(position);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.imageView86)
        ImageView image;
        @Bind(R.id.imageView76)
        ImageView profilePic;
        @Bind(R.id.textView67)
        TextView username;
        @Bind(R.id.textView68)
        TextView userFives;
        @Bind(R.id.textView1)
        TextView caption;
        @Bind(R.id.textView69)
        TextView fiveButton;
        @Bind(R.id.textView71)
        TextView commentButton;
        @Bind(R.id.imageView77)
        ImageView fiveImage;
        @Bind(R.id.textView72)
        TextView numFives;
        @Bind(R.id.imageView120)
        ImageView commentImage;
        @Bind(R.id.textView73)
        TextView numComments;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

    }

    public FeedItemAdapter(Context context, List<Photo> list) {
        this.context = context;
        this.list = list;
        opensans = Typeface.createFromAsset(context.getAssets(), "fonts/OpenSans-Regular.ttf");
        OkHttpClient okHttpClient = new OkHttpClient();
        picasso = new Picasso.Builder(context)
                .downloader(new OkHttpDownloader(okHttpClient))
                .build();
    }
}

下面是ListView行的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroller_placeholder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#231f20"
    android:orientation="vertical">


    <LinearLayout
        android:id="@+id/linearLayout7"
        android:background="#343031"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/imageView75"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:paddingRight="5dp"
                android:src="@drawable/user_profile_activity_1_img_gallery_1_icon"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="10dp" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@+id/imageView75"
                android:orientation="vertical"
                android:paddingLeft="5dp">

                <TextView
                    android:id="@+id/textView65"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:alpha="0.8"
                    android:paddingBottom="2dp"
                    android:text="Wilderness"
                    android:textColor="#FFFFFF" />

                <TextView
                    android:id="@+id/textView66"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingTop="2dp"
                    android:text="2 hrs."
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="#707070"
                    android:textSize="12sp" />
            </LinearLayout>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView162"
                android:src="@drawable/cta_button_follow_default_state"
                android:layout_alignParentEnd="true"
                android:layout_marginRight="20dp"
                android:layout_centerVertical="true" />
        </RelativeLayout>
    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

当我使用HorizontalScrollView时,每一行的项都显示得非常好,但recyclerView的内容没有显示出来。我想不出问题出在哪里。如有任何帮助,我们将不胜感激。

共有2个答案

郭洋
2023-03-14

正如Icetea先生所说,您需要将布局高度设置为特定的数量。您可以获取显示项的高度,并将其传递给layout-height参数。

https://developer.android.com/reference/android/widget/linearlayout.layoutparams.html动态添加layoutparams的文档。

常俊侠
2023-03-14

我认为您的问题在这里recyclerViewandroid:layout_height=“wrap_content”。尝试将此属性设置为特定值。

更新

Android支持库23.2的新版本:

RecyclerView小部件为创建列表和网格以及支持动画提供了一个高级而灵活的基础。这个版本为LayoutManager API带来了一个令人兴奋的新特性:自动测量!这允许RecyclerView根据其内容的大小调整自身的大小。这意味着以前不可用的场景,例如为RecyclerView的一个维度使用WRAP_CONTENT现在是可能的。您会发现所有内置的LayoutManager现在都支持自动测量。

 类似资料:
  • 本文向大家介绍node.js不得不说的12点内容,包括了node.js不得不说的12点内容的使用技巧和注意事项,需要的朋友参考一下 1.node.js,服务器端的javascript,它允许在后端(脱离浏览器环境)运行javascript代码。 2.事件驱动、异步式I/O的编程模式(单线程)是其核心。 3.node.js的javascript引擎是v8,来自google chrome项目。V8号称

  • 我有一个带有的列表项,我不知道会有多少项。当我在中输入一些文本,然后向下滚动时,我遇到了一个问题,当我再次向上滚动后,我的第一个 中没有文本。 我想知道我应该写什么,在哪里写代码,这样当用户在中键入或完成键入(我想用一个>来完成)时,文本会保存到一个文件中(我将它保存在外部存储的.txt文件中) 我应该在活动的方法中,还是在适配器类或其他地方这样做? 下面是一些代码 如果没有这个“addtextC

  • 我试图解决以下问题: 使用RecyclerView 我尝试使用下面的代码,问题是它不起作用,我没有发现任何工作示例没有提到在方向改变时重新绘制。

  • 数据表格的表头菜单字段里,勾选“字段内容不允许重复”后,将会对本图层该属性字段值进行去重比较,如果新填写的内容有和之前填写内容重复的,将不能保存

  • 我想将“项目单击侦听器”添加到我的中,这样当我单击某个项目时,它将启动一个新的并从web服务器检索有关该项目的数据。我试过很多建议,但没有一个有用。 我的RecylerView类

  • 本文向大家介绍使用jQuery获得内容以及内容的属性,包括了使用jQuery获得内容以及内容的属性的使用技巧和注意事项,需要的朋友参考一下 Tips: 1、点击事件节点的选择 #botton 2、alert 的代码规则  ("Value: " + $("#id").html) 今天的分享就先到这里了,后续我们还将持续更新一些新的jQuery实例。