android - RecyclerView 的内容不可见

标签 android android-listview android-adapter android-recyclerview

我正在尝试创建一个ListView,其中每一行都是一个水平滚动列表。早些时候,我使用 Horizo​​ntalScrollView 但我需要一个可以回收 View 的布局。所以我使用了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);
        }
    }
}

这是我的 Horizo​​ntal RecyclerView 适配器:

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>

当我使用 Horizo​​ntalScrollView 时,每行的项目都显示得很好,但 recyclerView 的内容没有显示。我不知道出了什么问题。任何帮助将不胜感激。

最佳答案

我认为你的问题出在RecyclerViewandroid:layout_height="wrap_content"。尝试将此属性设置为特定值。

更新

新发布Android Support Library 23.2 :

The RecyclerView widget provides an advanced and flexible base for creating lists and grids as well as supporting animations. This release brings an exciting new feature to the LayoutManager API: auto-measurement! This allows a RecyclerView to size itself based on the size of its contents. This means that previously unavailable scenarios, such as using WRAP_CONTENT for a dimension of the RecyclerView, are now possible. You’ll find all built in LayoutManagers now support auto-measurement.

关于android - RecyclerView 的内容不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32499487/

相关文章:

android - 如何在我的 fragment 和适配器中添加更多负载

android - Flutter 中项目复用后的问题

android - ListView一直在适配器中填充项目

android - 在 Fragment 中的 ListView 中实现搜索

java - 使用搜索 View 时适配器未更新

Android Google Play 服务回合制多人游戏如何将邀请者的游戏设置传递给被邀请者?

android - 从其伴生对象扩展数据类的函数时不可见

android - JSON Android ListView

android - 无法在自定义适配器中通过 ID 引用布局对象

java - 在 Android 中使用 RecyclerView 不显示列表