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.
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.
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.
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)
Related
I have implemented a RecyclerView inside a ConstraintLayout. I am having one child image element inside the layout. But when I click the child image, it always returns the ConstraintLayout, not the clicked image.
Could you please tell me why this is happening, what is the solution for this ?
I separately did bind listener to image, it is working but not able to get the RecyclerItem object. I need RecyclerItem object for the position to proceed.
I implemented it by binding elements via onBindViewholder method in Adapter. Below are the codes
customAdapter = new GridViewAdapter(recyclerItems, 1, this.getContext().getPackageName(),
new GridViewAdapter.OnItemClickListener(){
#Override
public void onItemClick(RecyclerItem item) {
CommonUtil.addFragment("REP", Constants.CONTAINER_HOME,
new ModifyFragment(), getActivity(), null);
}
}, R.layout.rec_view_item_stock, Constants.V_SPAN_LIST_8);
RecyclerView recyclerView = view.findViewById(R.id.stockListRecView);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setLayoutManager(new GridLayoutManager(this.getContext(), 1));
recyclerView.setAdapter(customAdapter);
//Adapter
public class GridViewAdapter extends RecyclerView.Adapter<GridViewAdapter.ViewHolder>{
private List<RecyclerItem> dataItems;
private int hSpan = 1;
private String packageName;
private final OnItemClickListener listener;
private int inflator;
private int vSpan;
public interface OnItemClickListener {
void onItemClick(RecyclerItem item);
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private final ConstraintLayout constraintLayout;
private final TextView textView;
private final ImageView imageView;
private Object obj;
public ViewHolder(View view) {
super(view);
constraintLayout = (ConstraintLayout) view.findViewById(R.id.rec_content_layout);
textView = constraintLayout.findViewById(R.id.recTextView);
imageView = constraintLayout.findViewById(R.id.recImage);
}
public void bind(final RecyclerItem item, final OnItemClickListener listener) {
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onItemClick(item);
}
});
}
public TextView getTextView() {
return textView;
}
public ImageView getImageView() {
return imageView;
}
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
}
public GridViewAdapter(List<RecyclerItem> items, int spanCount, String packageName, OnItemClickListener listener,
int inflator, int vSpan) {
dataItems = items;
this.hSpan = spanCount;
this.packageName = packageName;
this.listener = listener;
this.inflator = inflator;
this.vSpan = vSpan;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int gridType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(inflator, viewGroup, false);
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = (viewGroup.getResources().getDisplayMetrics().widthPixels / hSpan) - 24;
if(Constants.V_SPAN_GRID == vSpan) {
layoutParams.height = layoutParams.width;
} else {
layoutParams.height = (viewGroup.getResources().getDisplayMetrics().widthPixels / vSpan);
}
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
viewHolder.getTextView().setText(dataItems.get(position).getText());
int imgId = viewHolder.getImageView().getResources().getIdentifier(
dataItems.get(position).getImageName(), "drawable", packageName);
viewHolder.getImageView().setImageResource(imgId);
viewHolder.setObj(dataItems.get(position));
viewHolder.bind(dataItems.get(position), listener);
}
#Override
public int getItemCount() {
return dataItems.size();
}
//item layout
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/rec_content_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_marginTop="8dp"
android:layout_marginLeft="4dp"
android:padding="8dp"
android:background="#drawable/border_top_bottom" >
<androidx.constraintlayout.widget.Guideline
android:id="#+id/viewstock_gline_1_v"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.6"/>
<ImageView
android:id="#+id/recImage"
android:background="#color/colorPrimaryDark"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<TextView
android:id="#+id/recTextView"
app:layout_constraintLeft_toRightOf="#+id/recImage"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<ImageView
android:id="#+id/editstock_image"
android:src="#drawable/ic_edit_stock"
android:background="#color/colorPrimaryDark"
app:layout_constraintRight_toLeftOf="#+id/deletestock_image"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<ImageView
android:id="#+id/deletestock_image"
android:src="#drawable/ic_delete_stock"
android:background="#color/colorPrimaryDark"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
</androidx.constraintlayout.widget.ConstraintLayout>```
Try this!
public void bind(final RecyclerItem item, final OnItemClickListener listener, TextView itemTextView) {
itemTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onItemClick(item);
}
});
}
Pass every item text view instance from the view holder or fro onBindViewHolder itself implement onclick listener
I do the processing of click in the Activity.
#Override
public void onClick(View view) {
switch (view.getId()) {
final RecyclerView avaRecycler = (RecyclerView) view2.findViewById(R.id.ava_recycler);
avaRecycler.setLayoutManager(new GridLayoutManager(PersonEditActivity.this, 3));
avaRecycler.setHasFixedSize(true);
final AvaChooseRecyclerAdapter avaChooseRecyclerAdapter = new AvaChooseRecyclerAdapter(PersonEditActivity.this, new AvaChooseRecyclerAdapter.AvaViewHolder.MyClickListener() {
#Override
public void onAvaClickListener(int position) {
Toast.makeText(view2.getContext(), "Выбрана ава " + position, Toast.LENGTH_SHORT).show();
chosenId = (int) avaRecycler.getAdapter().getItemId(position);
avaRecycler.getAdapter().notifyItemChanged(position);
}
});
avaRecycler.setAdapter(avaChooseRecyclerAdapter);
Layout of the item
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/ava_item"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/ava_item_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
app:srcCompat="#drawable/avatars_man" />
<ImageView
android:id="#+id/ava_badge_yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:visibility="invisible"
app:layout_constraintRight_toRightOf="#+id/ava_item_imageview"
app:layout_constraintTop_toTopOf="#+id/ava_item_imageview"
app:srcCompat="#drawable/avatars_yes" />
I can not figure out how to change the visibility of ava_badge_yes by clicking on ava_item_imageview.
Edit.
My Adapter for recyclerview
public class AvaChooseRecyclerAdapter extends RecyclerView.Adapter<AvaChooseRecyclerAdapter.AvaViewHolder> {
private static final String TAG = "AvaChooseAdapter";
Context context;
private AvaChooseRecyclerAdapter.AvaViewHolder.MyClickListener myClickListener;
List<Integer> resourceIds = Arrays.asList(
R.drawable.avatars_01, R.drawable.avatars_02, R.drawable.avatars_03,
R.drawable.avatars_04, R.drawable.avatars_05, R.drawable.avatars_06,
R.drawable.avatars_07, R.drawable.avatars_08, R.drawable.avatars_09,
R.drawable.avatars_10, R.drawable.avatars_11, R.drawable.avatars_12,
R.drawable.avatars_13, R.drawable.avatars_14, R.drawable.avatars_15,
R.drawable.avatars_16, R.drawable.avatars_17, R.drawable.avatars_18,
R.drawable.avatars_19, R.drawable.avatars_20, R.drawable.avatars_21,
R.drawable.avatars_22, R.drawable.avatars_23, R.drawable.avatars_24,
R.drawable.avatars_25, R.drawable.avatars_26, R.drawable.avatars_27,
R.drawable.avatars_28, R.drawable.avatars_29, R.drawable.avatars_30,
R.drawable.avatars_31, R.drawable.avatars_32, R.drawable.avatars_33,
R.drawable.avatars_34, R.drawable.avatars_35, R.drawable.avatars_36,
R.drawable.avatars_37, R.drawable.avatars_38, R.drawable.avatars_39,
R.drawable.avatars_40, R.drawable.avatars_41, R.drawable.avatars_42,
R.drawable.avatars_43, R.drawable.avatars_44, R.drawable.avatars_45,
R.drawable.avatars_46, R.drawable.avatars_47, R.drawable.avatars_48,
R.drawable.avatars_49, R.drawable.avatars_50, R.drawable.avatars_51,
R.drawable.avatars_52, R.drawable.avatars_53, R.drawable.avatars_54,
R.drawable.avatars_55, R.drawable.avatars_56, R.drawable.avatars_57,
R.drawable.avatars_58, R.drawable.avatars_59, R.drawable.avatars_60,
R.drawable.avatars_61, R.drawable.avatars_62, R.drawable.avatars_63,
R.drawable.avatars_64, R.drawable.avatars_65, R.drawable.avatars_66);
public AvaChooseRecyclerAdapter(Context context, AvaChooseRecyclerAdapter.AvaViewHolder.MyClickListener m) {
this.context = context;
myClickListener = m;
}
public static class AvaViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected MyClickListener myClickListener;
protected ImageView iv;
public AvaViewHolder(View itemView, MyClickListener myClickListener) {
super(itemView);
this.myClickListener = myClickListener;
iv = (ImageView) itemView.findViewById(R.id.ava_item_imageview);
iv.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if (myClickListener != null) myClickListener.onAvaClickListener(getLayoutPosition());
}
public interface MyClickListener {
void onAvaClickListener(int position);
}
}
#Override
public long getItemId(int position) {
return resourceIds.get(position);
}
#Override
public AvaChooseRecyclerAdapter.AvaViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
context = parent.getContext();
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.ava_item, parent, false);
v.setPadding(14, 14, 14, 14);
return new AvaViewHolder(v, myClickListener);
}
#Override
public void onBindViewHolder(AvaViewHolder holder, int position) {
holder.iv.setImageResource(resourceIds.get(position));
Log.d(TAG, "onBindViewHolder position: " + position + " | " + holder.toString());
}
#Override
public int getItemCount() {
return resourceIds.size();
}
public getItem(int position){
}
}
And i want to understand.
On the forums write 3 options for processing clicks in the recyclerview
1. in ViewHolder
2. in OnBindViewHolder
3. With the use of the interface and the transfer of click processing in the activity. (I used this option)
But I don't understand the principle of choosing the place of treatment of pressing. In which cases to choose this or that method?
I would be very grateful if you explain. Highly.
Modify the onclick in Adapter & see if , click item change the color
#Override
public void onClick(View view) {
if (myClickListener != null) myClickListener.onAvaClickListener(getLayoutPosition());
AvaViewHolder.iv.setBackgroundColor(Color.rgb(100, 100, 50));
}
}
i already success to make one Recyclerview and i want to add new Recyclerview Horizontal on top. i will explain in my code :
<android.support.v7.widget.RecyclerView
android:id="#+id/arrayListUser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:divider="#color/white">
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:id="#+id/arrayList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:divider="#color/white">
</android.support.v7.widget.RecyclerView>
id:arrayList is my first Recyclerview have name xml feeds_listview
id:arrayListUser is my new Recyclerview, i want make this Recyclerview Horizontal
xml for new Recylerview is feeds_listviewUser
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profil"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:src="#drawable/cthprofil" />
<TextView
android:id="#+id/fullName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="5dp"
android:layout_marginBottom="20"
android:text="Megi Fernanda"
android:textSize="17sp"
android:textColor="#color/colordefault"
android:textStyle="bold" />
</LinearLayout>
and this is my class adapter
public class FeedsCustomAdapter extends RecyclerView.Adapter<FeedsCustomAdapter.ViewHolder> {
private Context context;
private List<FeedsAdapter> feeds_list;
private ArrayList<Feeds> mFeedsList = new ArrayList<Feeds>();
private OnItemClickListener mListener;
private OnItemClickListener mListener2;
public FeedsCustomAdapter(Context context, ArrayList<Feeds> mFeedsList) {
this.context = context;
this.mFeedsList = mFeedsList;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.feeds_listview, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Feeds feed = getFeeds().get(position);
int textColor = context.getResources().getColor(R.color.btn_next);
int textColor2 = context.getResources().getColor(R.color.text_color_black);
holder.fullName.setText(feed.user.fullName);
holder.location.setText(feed.user.location);
holder.topic.setText(Html.fromHtml( "Menyelesaikan Tantangan " + " <font color = '" + String.valueOf(textColor2) + "'>" + feed.topic + "</font>" ) );
Picasso.with(context)
.load(feed.user.avatar)
.into(holder.profile);
PrettyTime prettyTime = new PrettyTime();
String times = prettyTime.format(DateUtil.timeMilisTodate(feed.timestamp * 1000));
holder.times.setText(times);
}
#Override
public int getItemCount() {
return mFeedsList.size();
}
public ArrayList<Feeds> getFeeds() {
return mFeedsList;
}
public void setComplete(int position) {
mFeedsList.get(position).isComplete = 1;
}
public boolean last() {
boolean result = false;
int total = mFeedsList.size();
for (int i = 0; i < mFeedsList.size(); i++) {
if (mFeedsList.get(i).isComplete == 1) {
total--;
}
}
if (total == 1) {
result = true;
}
return result;
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView fullName;
public TextView location;
public TextView topic;
public ImageView profile;
public TextView times;
public ViewHolder(View itemView) {
super(itemView);
fullName = (TextView) itemView.findViewById(R.id.fullName);
location = (TextView) itemView.findViewById(R.id.location);
topic = (TextView) itemView.findViewById(R.id.topic);
profile = (ImageView) itemView.findViewById(R.id.profil);
times = (TextView) itemView.findViewById(R.id.times);
profile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mListener2 != null){
mListener2.onItemClick2(v ,getPosition());
}
}
});
topic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mListener != null){
mListener.onItemClick(v ,getPosition());
}
}
});
}
}
public void setClickListener(OnItemClickListener clickListener) {
this.mListener = clickListener;
}
public void setClickListenerProfile(OnItemClickListener clickListener2){
this.mListener2 = clickListener2;
}
public interface OnItemClickListener {
public abstract void onItemClick(View view, int position);
public abstract void onItemClick2(View view, int position);
}
so, in my code i success to display first recylerview with my first xml and i want add new recylerview horizontal with new xml feeds_listviewUser
You can use LinearLayout to wrap both recyclerView.
<android.support.v7.widget.RecyclerView
android:id="#+id/arrayListUser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#color/white">
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:id="#+id/arrayList"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:divider="#color/white">
</android.support.v7.widget.RecyclerView>
And assign horizontal layout manager to one recyclerview and vertical layout manager to other
LinearLayoutManager userManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
arrayListUser.setLayoutManager(userManager);
LinearLayoutManager listManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
arrayList.setLayoutManager(listManager);
Put your recyclerviews in Relative layout.
First add horizontal recyclerView to alignParentTop true and fix height according to visibility of feeds_listviewUser next add vertical recyclerView with layout_below horizontal recyclerview id.
I am using linkify to make links clickable in the textview. But whenever text length is big, the text flickers on scrolling or sometimes just disappears. The text view is part of header of recyclerview. Posting relevant code from activity class, its xml and adapter.
The Activity:
public class NewDiscussionDetailActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_discussion_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
aQuery = new AQuery(this);
setSupportActionBar(toolbar);
final ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.setDisplayHomeAsUpEnabled(true);
ab.setTitle("Discussion");
}
Intent activityIntent = getIntent();
discussion_id = Integer.parseInt(activityIntent.getStringExtra("discussion_id"));
discussion_title = activityIntent.getStringExtra("discussion_title");
discussion_text = activityIntent.getStringExtra("discussion_text");
discussion_image_url = activityIntent.getStringExtra("discussion_image_url");
comment_count = Integer.parseInt(activityIntent.getStringExtra("discussion_comment_no"));
community_id = activityIntent.getStringExtra("community_id");
community_name = activityIntent.getStringExtra("community_name");
is_from_notification = activityIntent.getBooleanExtra("is_notification", false);
// initializing header and footer for List view
footer = getLayoutInflater().inflate(R.layout.main_list_footer, null);
header = getLayoutInflater().inflate(R.layout.discussion_detail_header, null);
prepareHeader(header);
recyclerView = (RecyclerView) findViewById(R.id.comment_list);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(recyclerView.getContext());
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mLayoutManager.canScrollVertically();
recyclerView.setLayoutManager(mLayoutManager);
adapter = new CommentAdapter(commentList, this);
/* download page 1*/
adapter.addHeader(header);
recyclerView.setAdapter(adapter);
getCommentList();
/* Scroll Listener which takes care of all triggers to download to load data to adapter based position of the view*/
// code for downloading more pages of comments
}
private void prepareHeader(View header) {
/* preparing the header*/
discussion_image = (ImageView) header.findViewById(R.id.discussion_image_1);
title = (TextView) header.findViewById(R.id.discussion_title);
commentno = (TextView) header.findViewById(R.id.comment_count);
description = (TextView) header.findViewById(R.id.discussion_text_content);
title.setText(discussion_title);
title.setTypeface(AppConstants.getTypeFaceBold(this));
description.setText(discussion_text);
discriptionText = discussion_text + "";
Linkify.addLinks(description, Linkify.WEB_URLS);
description.setTypeface(AppConstants.getTypeFaceNormal(this));
if (comment_count > 2) {
commentno.setText(String.valueOf(comment_count - 1) + " comments");
} else if (comment_count == 2){
commentno.setText(String.valueOf(comment_count - 1) + " comment");
}else {
commentno.setText("Be the first to comment");
}
if (discussion_image_url == null || discussion_image_url.equals("")) {
discussion_image.setVisibility(View.GONE);
//progressbar.setVisibility(View.GONE);
} else {
discussion_image.setVisibility(View.VISIBLE);
final String image_url = discussion_image_url;
if(UtilMethod.isStringNullOrBlank(image_url))
{
//progressbar.setVisibility(View.GONE);
}
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
Picasso.with(this)
.load(image_url)
.placeholder(R.drawable.default_img_big).error(R.drawable.default_img_big)
.transform(new ImageTransformation(width))
.into(discussion_image, new Callback() {
#Override
public void onSuccess() {
// TODO Auto-generated method stub
//progressbar.setVisibility(View.GONE);
}
#Override
public void onError() {
// TODO Auto-generated method stub
//progressbar.setVisibility(View.GONE);
}
});
discussion_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// code
}
});
}
}
The Adapter:
public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CustomViewHolder> {
ArrayList<CommentListBean> mArrayList;
Context mContext;
String event = "";
public static final int TYPE_HEADER = 111;
public static final int TYPE_FOOTER = 222;
public static final int TYPE_ITEM = 333;
//headers
List<View> headers = new ArrayList<>();
//footers
List<View> footers = new ArrayList<>();
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class CustomViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
private View view;
final TextView timestamp_tv;
final TextView chat_username;
final TextView description_tv;
final ImageView author_image_view;
final ImageView comment_image;
public CustomViewHolder(View v) {
super(v);
view = v;
chat_username = (TextView) v.findViewById(R.id.comment_author_chatname);
timestamp_tv = (TextView) v.findViewById(R.id.comment_timestamp);
description_tv = (TextView) v.findViewById(R.id.comment_text);
comment_image = (ImageView) v.findViewById(R.id.comment_image);
author_image_view = (ImageView) v.findViewById(R.id.comment_author_image);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public CommentAdapter(ArrayList<CommentListBean> arrayList, Context context) {
this.mContext = context;
this.mArrayList = arrayList;
}
// Create new views (invoked by the layout manager)
#Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
if (viewType == TYPE_ITEM) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.comment_list_item, null);
CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;
} else {
//create a new framelayout, or inflate from a resource
FrameLayout frameLayout = new FrameLayout(parent.getContext());
//make sure it fills the space
frameLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
return new HeaderFooterViewHolder(frameLayout);
}
}
// Replace the contents of a view (invoked by the layout manager)
public void onBindViewHolder(final CustomViewHolder holder, final int position) {
//check what type of view our position is
if(position < headers.size()){
View v = headers.get(position);
//add our view to a header view and display it
prepareHeaderFooter((HeaderFooterViewHolder) holder, v);
}else if(position >= headers.size() + mArrayList.size()){
View v = footers.get(position-mArrayList.size()-headers.size());
//add oru view to a footer view and display it
prepareHeaderFooter((HeaderFooterViewHolder) holder, v);
}else {
// - code for normal comment view
}
}
#Override
public int getItemViewType(int position) {
//check what type our position is, based on the assumption that the order is headers > items > footers
if(position < headers.size()){
return TYPE_HEADER;
}else if(position >= headers.size() + mArrayList.size()){
return TYPE_FOOTER;
}
return TYPE_ITEM;
}
private void prepareHeaderFooter(HeaderFooterViewHolder vh, View view){
//empty out our FrameLayout and replace with our header/footer
vh.base.removeAllViews();
vh.base.addView(view);
}
//add a header to the adapter
public void addHeader(View header){
if(!headers.contains(header)){
headers.add(header);
//animate
notifyItemInserted(headers.size()-1);
}
}
//remove a header from the adapter
public void removeHeader(View header){
if(headers.contains(header)){
//animate
notifyItemRemoved(headers.indexOf(header));
headers.remove(header);
}
}
//add a footer to the adapter
public void addFooter(View footer){
if (!footers.contains(footer)){
footers.add(footer);
//animate
notifyItemInserted(headers.size()+mArrayList.size()+footers.size()-1);
}
}
//remove a footer from the adapter
public void removeFooter(View footer){
if(footers.contains(footer)) {
//animate
notifyItemRemoved(headers.size()+mArrayList.size()+footers.indexOf(footer));
footers.remove(footer);
}
}
//our header/footer RecyclerView.ViewHolder is just a FrameLayout
public static class HeaderFooterViewHolder extends CustomViewHolder{
FrameLayout base;
public HeaderFooterViewHolder(View itemView) {
super(itemView);
this.base = (FrameLayout) itemView;
}
}
}
XML for the header view
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/margin_10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/message_list_image_layout"
android:orientation="horizontal"
android:paddingTop="20dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="Representative image for the discussion"
android:background="#color/background_color"
android:id="#+id/discussion_image_1"
android:layout_marginBottom="#dimen/margin_5"
android:layout_marginTop="#dimen/margin_5"
android:src="#drawable/bg"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:maxHeight="280dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/discussion_title"
android:text="This is heading of the content"
android:layout_marginLeft="#dimen/margin_20"
android:layout_marginRight="#dimen/margin_20"
style="#style/Base.TextAppearance.AppCompat.Display1"
android:alpha="075" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/discussion_text_content"
android:layout_marginTop="#dimen/margin_10"
android:layout_marginLeft="#dimen/margin_20"
android:layout_marginRight="#dimen/margin_20"
android:text="this is the text of content. this is the text of content. this is the text of content. this is the text of content. this is the text of content.this is the text of content. this is the text of content. this is the text of content. this is the text of content. this is the text of content."
style="#style/Base.TextAppearance.AppCompat.Body1"
android:alpha="0.75" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="#dimen/margin_10"
android:layout_marginLeft="#dimen/margin_20"
android:layout_marginRight="#dimen/margin_20"
android:background="#color/dark_background_color"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="#dimen/margin_10"
android:layout_marginBottom="#dimen/margin_10"
android:layout_marginRight="#dimen/margin_20"
android:layout_marginLeft="#dimen/margin_20"
android:gravity="center_vertical|right|end"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/comment_count"
android:text="4 comments" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Screenshots
Normal view
when I scroll a bit
I have a feeling that it has something to do with LinkMovementMethod. Please help.
I have a ListView in my code which contains TextViews as its child, when my OnItemClickListener is fired I will receive a view. This view is the one that I clicked from the ListView. From this part, I will cast this view to make it as TextView. Next, I will set a text of this TextView. The problem is, when I scroll my ListView upward/downward some of the ListView's child text are also change. the ListView redraw its child containing the text of other child text. How can I fix this?
UPDATE: The view I recieved on the OnItemClickListener is somehow the same with other child view. I found this out when I tried to compared the previous view I clicked and the newly clicked view by using equals() method. scrolling the listview redraws its child but sometimes there are child view that are the same with other child view those are not visible in the list.
This is my OnItemClickListener
messagesContainer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(adapter.getItemViewType(i)==ChatAdapter.VOICE_MESSAGE){
RelativeLayout rootView = (RelativeLayout) view;
LinearLayout content = (LinearLayout) rootView.getChildAt(0);
LinearLayout contentWithBackground = (LinearLayout) content.getChildAt(1);
LinearLayout voiceContent = (LinearLayout) contentWithBackground.getChildAt(1);
TextView voiceMessage = (TextView) voiceContent.getChildAt(0);
voiceMessage.setText("Sample Text");
}
}
});
This my Adapter
public class ChatAdapter extends BaseAdapter {
private List<QBChatMessage> chatMessages;
private Context context;
private static final int TXT_MESSAGE = 0;
private static final int IMAGE_MESSAGE = 1;
public static final int VOICE_MESSAGE = 2;
private DisplayImageOptions displayImageOptions;
public ChatAdapter(Context context, List<QBChatMessage> chatMessages){
this.chatMessages = chatMessages;
this.context = context;
initImageLoaderOptions();
}
public void initImageLoaderOptions() {
displayImageOptions = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty).showImageOnFail(R.drawable.ic_error).cacheInMemory(true)
.cacheOnDisc(true).considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565).build();
}
#Override
public int getViewTypeCount() {
return 3;
}
#Override
public int getItemViewType(int position){
if(getItem(position).getProperty("fileUID") == null){
return TXT_MESSAGE;
} else {
if (getItem(position).getProperty("type").equals(QBAttachment.PHOTO_TYPE)) {
return IMAGE_MESSAGE;
} else {
return VOICE_MESSAGE;
}
}
}
#Override
public int getCount() {
if(chatMessages!=null){
return chatMessages.size();
} else{
return 0;
}
}
#Override
public QBChatMessage getItem(int position) {
if(chatMessages!=null){
return chatMessages.get(position);
} else{
return null;
}
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
QBChatMessage chatMessage = getItem(position);
final LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int type = getItemViewType(position);
if (convertView == null) {
if(type == TXT_MESSAGE){
convertView = vi.inflate(R.layout.list_item_message, null);
} else if(type == IMAGE_MESSAGE){
convertView = vi.inflate(R.layout.list_item_message_image, null);
} else{
convertView = vi.inflate(R.layout.list_item_message_voice, null);
}
holder = createViewHolder(convertView, type, position);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
QBUser currentUser = ChatService.getInstance().getCurrentUser();
boolean isOutgoing = chatMessage.getSenderId() != null && chatMessage.getSenderId().equals(currentUser.getId());
setAlignment(holder, isOutgoing, type);
if (chatMessage.getProperty("fullName") != null) {
holder.txtInfo.setText(chatMessage.getProperty("fullName") + ": " + getTimeText(chatMessage));
} else {
holder.txtInfo.setText(getTimeText(chatMessage));
}
if(type == TXT_MESSAGE){
holder.txtMessage.setText(chatMessage.getBody());
} else if (type == IMAGE_MESSAGE){
Log.i("Loading", "Loading");
ImageLoader.getInstance().displayImage(AttachmentConstants.URL_S3+chatMessage.getProperty("fileUID"), holder.imageMessage, displayImageOptions);
}
ImageLoader.getInstance().displayImage(AttachmentConstants.URL_FACEBOOK_OPEN+chatMessage.getProperty("social_picture")+ AttachmentConstants.URL_FACEBOOK_CLOSING, holder.socialPhoto, displayImageOptions);
return convertView;
}
private ViewHolder createViewHolder(View v, int type, final int position) {
final ViewHolder holder = new ViewHolder();
holder.content = (LinearLayout) v.findViewById(R.id.content);
holder.contentWithBG = (LinearLayout) v.findViewById(R.id.contentWithBackground);
holder.txtInfo = (TextView) v.findViewById(R.id.txtInfo);
holder.socialPhoto = (ImageView) v.findViewById(R.id.social_photo);
if(type == TXT_MESSAGE){
holder.txtMessage = (TextView) v.findViewById(R.id.txtMessage);
} else if(type == IMAGE_MESSAGE) {
holder.imageMessage = (ImageView) v.findViewById(R.id.imageMessage);
} else{
holder.voiceMessage = (TextView) v.findViewById(R.id.voiceMessage);
holder.voiceSeekBar = (SeekBar) v.findViewById(R.id.voiceSeekBar);
holder.voiceSeekBar.setEnabled(false);
}
return holder;
}
private String getTimeText(QBChatMessage message) {
return TimeUtils.millisToLongDHMS(message.getDateSent() * 1000);
}
private static class ViewHolder {
public TextView txtMessage;
public TextView txtInfo;
public LinearLayout content;
public LinearLayout contentWithBG;
public ImageView socialPhoto;
public ImageView imageMessage;
public TextView voiceMessage;
public SeekBar voiceSeekBar;
}
}
This is list_item_message_voice.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="#+id/txtInfo"
android:layout_width="wrap_content"
android:layout_height="20sp"
android:layout_gravity="right"
android:textSize="10sp"
android:textColor="#android:color/secondary_text_dark" />
<LinearLayout
android:id="#+id/contentWithBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#drawable/incoming_message_bg"
android:paddingLeft="10dp"
android:paddingBottom="10dp"
android:orientation="horizontal">
<ImageView
android:layout_margin="10dp"
android:id="#+id/social_photo"
android:layout_width="33.33dp"
android:layout_height="33.33dp" />
<LinearLayout
android:id="#+id/voiceContent"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="Play"
android:id="#+id/voiceMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"/>
<SeekBar
android:id="#+id/voiceSeekBar"
android:layout_width="100dp"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
The text is getting changed because you changing the text in the TextView but not in the chatMessages list of the adapter.
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(adapter.getItemViewType(i)==ChatAdapter.VOICE_MESSAGE){
//Your code.....
voiceMessage.setText("Sample Text");
//Update the chat messages in the list.
}
}
You can do this:
messagesContainer.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
{
TextView voiceMessage = (TextView)view.findViewById(R.id.txtMessage);
if(!voiceMessage.getText().toString().equals(""))
voiceMessage.setText("Sample Text");
}
});
Try replacing this in your Adapter
else
{
if(!holder.txtInfo.getText().toString().equals(""))
holder.txtInfo.setText(getTimeText(chatMessage));
}
if(convertView == null){
convertView = vi.inflate(R.layout.list_item_message, null);
}
// You can set the data here...
return covertView;