0

i have used CardView for displaying pictures and i want to add a ListView inside the CardView for displaying comments for those picture. For CardView, i have used RecyclerView adapter and the CardView and RecyclerView are used in fragment. But the ListView is not being displayed inside the CardView.

CardAdapter.java

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {

List<NatureItem> mItems;

public CardAdapter() {
    super();
    mItems = new ArrayList<NatureItem>();
    NatureItem nature = new NatureItem();
    nature.setName("The Great Barrier Reef");
    nature.setDes("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt" +
                  "ut labore et dolore magna aliqua. Ut enim ad minim veniam.");
    nature.setThumbnail(R.drawable.great_barrier_reef);
    mItems.add(nature);

    nature = new NatureItem();
    nature.setName("Grand Canyon");
    nature.setDes("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt" +
            "ut labore et dolore magna aliqua.");
    nature.setThumbnail(R.drawable.grand_canyon);
    mItems.add(nature);

    nature = new NatureItem();
    nature.setName("Baltoro Glacier");
    nature.setDes("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt" +
            "ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis.");
    nature.setThumbnail(R.drawable.baltoro_glacier);
    mItems.add(nature);

    nature = new NatureItem();
    nature.setName("Aurora Borealis");
    nature.setDes("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt" +
                  "ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud.");
    nature.setThumbnail(R.drawable.aurora_borealis);
    mItems.add(nature);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.cardview, viewGroup, false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
    NatureItem nature = mItems.get(i);
    viewHolder.tvNature.setText(nature.getName());
    viewHolder.tvDesNature.setText(nature.getDes());
    viewHolder.imgThumbnail.setImageResource(nature.getThumbnail());
}

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

class ViewHolder extends RecyclerView.ViewHolder{

    public ImageView imgThumbnail;
    public TextView tvNature;
    public TextView tvDesNature;
    public ListView listView;

    public ViewHolder(View itemView) {
        super(itemView);
        imgThumbnail = (ImageView)itemView.findViewById(R.id.img_thumbnail);
        tvNature = (TextView)itemView.findViewById(R.id.tv_nature);
        tvDesNature = (TextView)itemView.findViewById(R.id.desc);
        listView = (ListView)itemView.findViewById(R.id.list);
    }
}

}

and the cardview.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"
    xmlns:card_view="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.CardView
    android:layout_width="fill_parent"
    android:layout_height="300dp"
    card_view:cardCornerRadius="3dp"
    android:layout_marginTop="9dp"
    android:layout_marginLeft="9dp"
    android:layout_marginRight="9dp"
    card_view:cardElevation="0.01dp"
    android:layout_marginBottom="0dp">
    <RelativeLayout
        android:id="@+id/top_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/img_thumbnail"
            android:layout_width="fill_parent"
            android:layout_height="200dp"
            android:scaleType="fitXY" />
        <TextView
            android:id="@+id/tv_nature"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:layout_gravity="bottom"
            android:gravity="center_vertical"
            android:background="#5c1b1b1b"
            android:alpha="0.8"
            android:textColor="#fff"
            android:textSize="19sp"
            android:text="Test"
            android:layout_alignBottom="@+id/img_thumbnail"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />


        <TextView
            android:id="@+id/desc"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="5dp"
            android:paddingBottom="2dp"
            android:alpha="0.8"
            android:textColor="#ff272727"
            android:textSize="12dp"
            android:text="Test Test Test Test Test Test Test Test Test Test Test Test Test"
            android:layout_below="@+id/tv_nature"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_above="@+id/btn_like" />

<Button android:layout_width="110dp"
    android:layout_height="30dp"
    android:text="LIKE"
    android:id="@+id/btn_like"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:layout_below="@+id/img_thumbnail"
    android:textColor="#ffffff"
    android:background="#1976D2" />

        <Button android:layout_width="150dp"
            android:layout_height="30dp"
            android:textColor="#ffffff"
            android:background="#1976D2"
            android:layout_below="@+id/img_thumbnail"
            android:layout_toRightOf="@+id/btn_like"
            android:layout_toEndOf="@+id/btn_like"
            android:layout_marginLeft="7dp"
            android:text="COMMENT"
            android:id="@+id/btn_comment"
            />

        <Button
            android:layout_width="110dp"
            android:layout_height="30dp"
            android:text="VIEWS"
            android:textColor="#ffffff"
            android:background="#1976D2"
            android:id="@+id/btn_view"
            android:layout_below="@+id/img_thumbnail"
            android:layout_toRightOf="@+id/btn_comment"
            android:layout_toEndOf="@+id/btn_comment"
            android:layout_marginLeft="7dp"
            android:paddingRight="@dimen/activity_horizontal_margin"/>

    </RelativeLayout>

    <ListView
        android:id = "@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/top_layout"

        />

</android.support.v7.widget.CardView>

If anyone could provide a solution.. Thanks

silika thapa
  • 11
  • 1
  • 5
  • 1
    Did you tried to wrap the listView in a relativ layout? nevertheless, if I got you right, then your cards are rows of a recycler view? It will be a pain to have a listView inside of another listview (or recyclerview). Maybe reconsider the design. – JacksOnF1re Oct 28 '15 at 16:12
  • Well you need to provide some code because it may be that which is causing it not to display – Unknownweirdo Oct 28 '15 at 16:33
  • It's not displaying because you can't add one scrollable view inside another. Your solution will not work and you should think of a different approach. – Budius Oct 28 '15 at 17:05
  • you can use recyclerview inside cardview instead listview. check this answer http://stackoverflow.com/questions/28166624/android-how-can-i-insert-a-recyclerview-inside-cardview – H Raval Oct 29 '15 at 05:53

0 Answers0