Reputation: 3692
I am trying to create a ListView, each row of which is a horizontal scrolling list. Earlier, I was using HorizontalScrollView but I needed a layout which could recycle views. So I used RecyclerView, but now the contents of RecyclerView are not visible.
Here is my adapter for vertical 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);
}
}
}
Here is my adapter for Horizontal 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();
}
}
Here is my layout for row of 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>
The items for each row were getting displayed perfectly fine when I was using HorizontalScrollView but the contents of recyclerView are not getting displayed. I can't figure out what is the problem. Any help would be highly appreciated.
Upvotes: 2
Views: 9216
Reputation: 143
As mr.icetea said you need to set the layout-height
to a specific amount. You could get the shown items height and pass that on to the layout-height param.
https://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html documentation for adding layoutparams
on the fly.
Upvotes: 1
Reputation: 2637
I think your problem is here RecyclerView
's android:layout_height="wrap_content"
. Try to set this attribute to specific value.
Update
New release of 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.
Upvotes: 2
Reputation: 851
check out this method to calculate recyclerview height dynamically
// calculating height of Recycler view
private int calculateHeight(int size,int viewHeight) {
final float scale = mContext.getResources().getDisplayMetrics().density;
int singleViewHeight = (int) (viewHeight * scale + 0.5f);
int totalHeight = singleViewHeight * size;
return totalHeight;
}
and set height to recyclerview as-
//get recyclerview height
int height = calculateHeight(firstParam, secondparam);
ViewGroup.LayoutParams params = recyclerView.getLayoutParams();
params.height = height;
firstparam is sizofYourDataList_or_Array and secondparam is height of your row for recyclerview(so you have to give height for your row layout and that wiil be your second parameter's value)
Upvotes: 0