I have a Recyclerview with items who have some hidden extra description.
I put a button to reveal or hide the extra description.
It works ok, but the problem is that, If I press the first item, when I scroll I see some other items also revealed (for example if I press the first, it also reveals the 7, the 10, the 17 and the 20).
When I scroll back the revealed item is hidden again, that doesn't botter me, but it gives any info.
This is the code of the adapter
class ArticulosAdapter : RecyclerView.Adapter
{
// Event handler for item clicks:
public event EventHandler<int> ItemClick;
// Underlying data set
public List<Articulo> listado;
public ArticulosAdapter(List<Articulo> listadoArticulos)
{
listado = listadoArticulos;
}
// Create a new CardView (invoked by the layout manager):
public override ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
// Inflate the CardView for the photo:
View itemView = LayoutInflater.From(parent.Context).
Inflate(Resource.Layout.filaArticulo, parent, false);
// Create a ViewHolder to find and hold these view references, and
ArticuloViewHolder vh = new ArticuloViewHolder(itemView,OnClick);
vh.Arrow.Click += (o, e) =>
{
if (vh.Info.Visibility == ViewStates.Gone)
vh.Info.Visibility = ViewStates.Visible;
else
vh.Info.Visibility = ViewStates.Gone;
};
return vh;
}
// Fill in the contents
public override void OnBindViewHolder(ViewHolder holder, int position)
{
ArticuloViewHolder vh = holder as ArticuloViewHolder;
if (listado[position].imagen != null)
vh.Image.SetImageBitmap(listado[position].BitImage);
vh.Caption.Text = listado[position].nombre;
vh.Cantidad.Text = "DISPONIBLE :" + listado[position].cantidad;
}
void OnClick(int position)
{
if (ItemClick != null)
ItemClick(this, position);
}
// Return the number of photos available in the photo album:
public override int ItemCount
{
get { return listado.Count; }
}
public class ArticuloViewHolder : ViewHolder
{
public ImageView Image { get; private set; }
public TextView Caption { get; private set; }
public TextView Cantidad { get; private set; }
public LinearLayout Info { get; private set; }
public ImageButton Arrow { get; private set; }
// Get references to the views defined in the CardView layout.
public ArticuloViewHolder(View itemView, Action<int> listener)
: base(itemView)
{
Image = itemView.FindViewById<ImageView>(Resource.Id.foto);
Caption = itemView.FindViewById<TextView>(Resource.Id.nombre);
Cantidad = itemView.FindViewById<TextView>(Resource.Id.cantidad);
Arrow = itemView.FindViewById<ImageButton>(Resource.Id.arrowButt);
Info = itemView.FindViewById<LinearLayout>(Resource.Id.infoLay);
itemView.Click += (sender, e) => listener(base.Position);
}
}
}
And this is the code of the row
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<ImageView
android:layout_width="150px"
android:layout_height="150px"
android:id="#+id/foto"
android:scaleType="centerCrop"
android:layout_gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#333333"
android:text="Caption"
android:id="#+id/nombre"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="4dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/infoLay"
android:visibility="gone"
android:padding="8dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#333333"
android:text="Caption"
android:id="#+id/cantidad"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="4dp" />
</LinearLayout>
<ImageButton
android:id="#+id/arrowButt"
android:layout_width="match_parent"
android:src="#drawable/down"
android:layout_height="50dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
The other revealed items are the ones using the same (recycled) view holder and views.
You need to (re)set the visibility of the Info part in OnBindViewHolder().
In your case, as you don't mind if the item is hidden again when scrolling back, you can simply do something like :
public override void OnBindViewHolder(ViewHolder holder, int position)
{
...
vh.Info.Visibility = ViewStates.Gone
}
Related
I'm trying to put a RecyclerView inside a CardView and create items dynamically.
Obviously, in XML, RecyclerView is set inside CardView tag, but as you can see picture, RecyclerView item is created outside CardView.
(RecyclerView's items are the parts expressed as kg and set in the photo. And CardView is also an item of another RecyclerView.)
What's the problem?
XML
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="12dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
app:cardElevation="10dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/ll1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/workout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="TEST"/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#color/light_blue_400"/>
</LinearLayout>
<LinearLayout
android:id="#+id/ll2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="#+id/ll1">
<com.google.android.material.button.MaterialButton
android:id="#+id/delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginStart="8dp"
android:layout_marginEnd="4dp"
style="#style/RoutineButtonStyle"
android:text="DELETE"
app:icon="#drawable/ic_delete"/>
<com.google.android.material.button.MaterialButton
android:id="#+id/add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginStart="4dp"
android:layout_marginEnd="8dp"
style="#style/RoutineButtonStyle"
android:text="ADD"
app:icon="#drawable/ic_add"/>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/ll2"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Let me know if you need any other code.
Here is the Activity of your add/delete CardView:
//Item List if you want to add one or more Items
private List<MyItem> myItems = new ArrayList<>();
private Adapter smallCardAdapter;
private int size;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.rv);
//Creates a new Object in the Adapter.class
smallCardAdapter = new Adapter();
recyclerView.setAdapter(smallCardAdapter);
CardView cardView = findViewById(R.id.cardView);
MaterialButton delete = findViewById(R.id.delete);
MaterialButton add = findViewById(R.id.add);
//Add onClickListener
add.setOnClickListener(v -> {
try {
size = myItems.size();
//if the value is 0, a new item is created
//if you want to delete the card after 0 than you have to change the value to 0
if(size == 0){
//Creates a new Object in the MyItem.class
myItems.add(new MyItem("Set","1kg"));
}else{
MyItem myItem = myItems.get(0);
//Replace kg with "" to make a Integer
String secondString = myItem.getSecondString().replace("kg","");
Integer value = Integer.valueOf(secondString);
value++;
myItem.setSecondString(value+"kg");
myItems.set(0,myItem);
}
updateAdapter();
}catch (Exception e){
Log.w("MainActivity","Add Button OnClickListener");
e.printStackTrace();
}
});
//Delete onClickListener
delete.setOnClickListener(v->{
try {
size = myItems.size();
//If the value is 0 then canceled to avoid errors
if(size == 0){
return;
}
MyItem myItem = myItems.get(0);
//Replace kg with "" to make a Integer
String secondString = myItem.getSecondString().replace("kg","");
Integer value = Integer.valueOf(secondString);
//if the value is one or lower
if(value <= 1){
//The card will deleted after updateAdapter();
myItems.clear();
}else{
value--;
myItem.setSecondString(value+"kg");
myItems.set(0,myItem);
}
updateAdapter();
}catch (Exception e){
Log.w("MainActivity","Delete Button OnClickListener");
e.printStackTrace();
}
});
}
private void updateAdapter(){
try {
smallCardAdapter.setItems(myItems);
}catch (Exception e){
Log.w("MainActivity","updateAdapter");
e.printStackTrace();
}
}
The custom Item called MyItem:
public class MyItem {
String firstString;
String secondString;
public MyItem(String firstString,String secondString){
this.firstString = firstString;
this.secondString = secondString;
}
public String getFirstString() {
return firstString;
}
public void setFirstString(String firstString) {
this.firstString = firstString;
}
public String getSecondString() {
return secondString;
}
public void setSecondString(String secondString) {
this.secondString = secondString;
}
}
The Adapter.java creates a smallCard:
public class Adapter extends RecyclerView.Adapter<Adapter.SmallCardHolder> {
private List<MyItem> items = new ArrayList<>();
#NonNull
#Override
public SmallCardHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
// I created a new smallcard for the CardView
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.smallcard, parent, false);
SmallCardHolder smallCardHolder = new SmallCardHolder(itemView);
return smallCardHolder;
}
#Override
public void onBindViewHolder(#NonNull final SmallCardHolder holder, int position) {
//get the current item of the position
MyItem myItem = items.get(position);
String firstString = myItem.getFirstString();
String secondString = myItem.getSecondString();
holder.textViewSet.setText(firstString);
holder.textViewkg.setText(secondString);
}
#Override
public int getItemCount () {
return items.size();
}
public void setItems(List <MyItem> items) {
this.items = items;
notifyDataSetChanged();
}
class SmallCardHolder extends RecyclerView.ViewHolder {
//Here are the TextView's of the smallcard (smallcard.xml)
private TextView textViewSet;
private TextView textViewkg;
public SmallCardHolder(#NonNull View itemView) {
super(itemView);
textViewSet = itemView.findViewById(R.id.set);
textViewkg = itemView.findViewById(R.id.kg);
}
}
}
Create a new layout with name called smallcard.xml for the Adapter.java:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set"
android:layout_centerVertical="true"
android:textSize="18sp"
android:layout_marginStart="5dp"
android:textColor="?attr/colorPrimary"
android:textStyle="bold"/>
<TextView
android:id="#+id/kg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="kg"
android:layout_toRightOf="#id/set"
android:layout_marginLeft="55dp"
android:layout_centerVertical="true"
android:textColor="?attr/colorPrimary"
android:textSize="16sp"
android:layout_marginRight="10dp"
android:maxLines="1"/>
</RelativeLayout>
</RelativeLayout>
In your XML you have to give the CardView a name. I called it cardView. Here is the code android:id="#+id/cardView"
Result:
You will find more information as comments in the code.
I have a recycler inside fragment in view pager.
The problem that it put likes on user accounts in UI randomly but in DB everything is fine.
In logs, I see that random is not influence on BD. So the bug is only in UI part. After the refresh of the list, it can appear again and after that disappear.
I would like to share code but I already don't have an I idea where the problem could be. It might be in adapter/refresh list listener / XML or any other places. Please let me know what part of the code you need and I will provide it. Maybe it is a bug of recycler as itself and I can't fix it.
Adapter class code:
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.UserViewHolder>{
private List<FsUser> fsUserList = new ArrayList<>();
private OnItemClickListener.OnItemClickCallback onItemClickCallback;
private OnItemClickListener.OnItemClickCallback onChatClickCallback;
private OnItemClickListener.OnItemClickCallback onLikeClickCallback;
private Context context;
public SearchAdapter(OnItemClickListener.OnItemClickCallback onItemClickCallback,
OnItemClickListener.OnItemClickCallback onChatClickCallback,
OnItemClickListener.OnItemClickCallback onLikeClickCallback) {
this.onItemClickCallback = onItemClickCallback;
this.onChatClickCallback = onChatClickCallback;
this.onLikeClickCallback = onLikeClickCallback;
}
public void addUsers(List<FsUser> userList) {
fsUserList.addAll(userList);
notifyItemRangeInserted(fsUserList.size() - userList.size(), fsUserList.size());
}
public void clearData(){
fsUserList.clear();
notifyDataSetChanged();
}
#NonNull
#Override
public UserViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user, parent, false);
return new UserViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull UserViewHolder holder, int position) {
FsUser fsUser = fsUserList.get(position);
holder.bind(fsUser, position);
}
#Override
public int getItemCount() {
return fsUserList.size();
}
public String getLastItemId(){
return fsUserList.get(fsUserList.size() - 1).getUid();
}
class UserViewHolder extends RecyclerView.ViewHolder {
RelativeLayout container;
ImageView imageView, like, chat;
TextView name, country;
private LottieAnimationView animationView;
UserViewHolder(View itemView) {
super(itemView);
context = itemView.getContext();
container = itemView.findViewById(R.id.item_user_container);
imageView = itemView.findViewById(R.id.user_img);
like = itemView.findViewById(R.id.search_btn_like);
chat = itemView.findViewById(R.id.search_btn_chat);
name = itemView.findViewById(R.id.user_name);
country = itemView.findViewById(R.id.user_country);
animationView = itemView.findViewById(R.id.lottieAnimationView);
}
void bind(FsUser fsUser, int position){
ViewCompat.setTransitionName(imageView, fsUser.getName());
if (FirebaseUtils.isUserExist() && fsUser.getUid() != null) {
new FriendRepository().isLiked(fsUser.getUid(), flag -> {
if (flag) {
like.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_favorite));
animationView.setVisibility(View.VISIBLE);
}
});
}
if(fsUser.getUid() != null) {
chat.setOnClickListener(new OnItemClickListener(position, onChatClickCallback));
like.setOnClickListener(new OnItemClickListener(position, onLikeClickCallback));
}
imageView.setOnClickListener(new OnItemClickListener(position, onItemClickCallback));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
if(fsUser.getImage().equals("default")){
Glide.with(context).load(context.getResources().getDrawable(R.drawable.default_avatar)).into(imageView);
} else {
Glide.with(context).load(fsUser.getImage()).thumbnail(0.5f).into(imageView);
}
name.setText(fsUser.getName());
country.setText(fsUser.getCountry());
ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f).setDuration(500);
animator.addUpdateListener(valueAnimator ->
animationView.setProgress((Float) valueAnimator.getAnimatedValue()));
if (animationView.getProgress() == 0f) {
animator.start();
} else {
animationView.setProgress(0f);
}
}
}
}
And xml file of RecyclerView item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/item_user_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="#dimen/user_cv_width"
android:layout_height="#dimen/user_cv_height"
android:layout_margin="#dimen/dp4"
android:elevation="#dimen/dp4">
<RelativeLayout
android:id="#+id/item_user_main_relative_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/item_user_top_relative_container"
android:layout_width="#dimen/user_rl_width"
android:layout_height="#dimen/user_rl_height">
<ImageView
android:id="#+id/user_img"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:src="#drawable/default_avatar" />
<RelativeLayout
android:id="#+id/item_user_top_relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/user_item_bg"
android:orientation="vertical">
<TextView
android:id="#+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/dp4"
android:textColor="#android:color/white"
android:textSize="#dimen/medium_text_size" />
<TextView
android:id="#+id/user_country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:layout_marginStart="#dimen/dp4"
android:textSize="#dimen/medium_text_size" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/item_user_bottom_relative_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/dp12">
<ImageView
android:id="#+id/search_btn_like"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/heart_outline"
android:contentDescription="#string/search_btn_like_desc"/>
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/lottieAnimationView"
android:visibility="gone"
android:layout_width="#dimen/lottie_animation_view_size"
android:layout_height="#dimen/lottie_animation_view_size"
app:lottie_loop="true"
app:lottie_autoPlay="true"
app:lottie_fileName="like.json"/>
</RelativeLayout>
<ImageView
android:id="#+id/search_btn_chat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/dp12"
android:layout_weight="1"
android:src="#drawable/message_outline" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
Override this two methods inside your adapter
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
I check everything in my code,I ask a question and do some of the modification,and also reading all the question about "Item not showing in RecyclerView",but the layout still not appear in my recyclerView.
This is my previous question,I attach all my code here,have 3 answer tell me,I should use LinearLayout instead of RelativeLayout as the parent of RecyclerView,so I modified the code as below
Fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/commentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/titlebar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/feed_item_margin"
android:layout_marginRight="#dimen/feed_item_margin"
android:layout_marginTop="#dimen/feed_item_margin"
android:layout_weight="1"
android:text="Be the first to like this" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/comment_recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:animateLayoutChanges="false"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
<LinearLayout
android:id="#+id/commentInsert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="horizontal">
<EditText
android:id="#+id/commentField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#null"
android:ems="10"
android:hint="Add a comment" />
<Button
android:id="#+id/sendButton"
android:layout_width="77dp"
android:layout_height="wrap_content"
android:text="Send" />
</LinearLayout>
</LinearLayout>
Here is the item which should be appear in the RecyclerView.comment_item.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="wrap_content"
android:layout_weight="1">
<ImageView
android:id="#+id/commentProfilePic"
android:layout_weight="0.05"
android:layout_width="#dimen/comment_item_profile_pic"
android:layout_height="#dimen/comment_item_profile_pic"
android:layout_marginLeft="#dimen/feed_item_margin"
android:layout_marginRight="#dimen/feed_item_margin"
android:layout_marginTop="#dimen/feed_item_margin"
android:scaleType="fitCenter" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/feed_item_margin"
android:layout_marginRight="#dimen/feed_item_margin"
android:layout_marginTop="#dimen/feed_item_margin"
android:orientation="vertical"
android:layout_weight="1"
android:paddingLeft="#dimen/comment_item_profile_info_padd">
<TextView
android:id="#+id/commentUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/comment_item_status_pad_left_right"
android:paddingRight="#dimen/comment_item_status_pad_left_right"
android:textStyle="bold" />
<TextView
android:id="#+id/commentBody"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/comment_item_status_pad_left_right"
android:paddingRight="#dimen/comment_item_status_pad_left_right" />
<TextView
android:id="#+id/commentTimestamp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/comment_item_status_pad_left_right"
android:paddingRight="#dimen/comment_item_status_pad_left_right"
android:paddingTop="#dimen/comment_item_timestamp_pad_top" />
</LinearLayout>
I read this issue tell that need to add this line of code adapter.notifyDataSetChanged();,I done that,this is how I notifyDataSetChanged() when I parse the JSON.
private void parseJsonFeed(JSONObject response) {
try {
JSONArray commentArray = response.getJSONArray("comment");
//get all the item in Json
for (int i = 0; i < commentArray.length(); i++) {
JSONObject commentObj = (JSONObject) commentArray.get(i);
commentId = commentObj.getInt("comment_id");
commenterUsername= commentObj.getString("commenter_username");
commentBody = commentObj.getString("comment_body");
commenterProfileImage = commentObj.getString("commenter_profile_image");
commentCreatedAt = commentObj.getString("comment_created_at");
//set all item to the Array list
setItemToCommentArrayList(commentId,commenterUsername,commenterProfileImage,commentBody,commentCreatedAt);
}
// notify data changes to list adapter
commentAdapter.notifyDataSetChanged();
}catch (JSONException e){
System.out.println("end of content");
}
}
I checked,whether is something wrong when I set my adapter to the RecycleView.So i read this answer,and here is how I set my adapter
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View dialogView = inflater.inflate(R.layout.fragment_comment, container,false);
commentRecyclerView =(RecyclerView)dialogView.findViewById(R.id.comment_recycler_view);
commentRecyclerView.setNestedScrollingEnabled(false);
//bind the recycler view with the adapter
commentAdapter = new CommentAdapter(getActivity(),commentItems);
final LinearLayoutManager myLayoutManager = new LinearLayoutManager(getActivity());
commentRecyclerView.setLayoutManager(myLayoutManager);
commentRecyclerView.setAdapter(commentAdapter);
Here is my CommentAdpater , I still didnt see any different with my another recycleview adapter.
public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.MyViewHolder>{
private Context cContext;
private List<CommentItem> commentItems;
class MyViewHolder extends RecyclerView.ViewHolder{
TextView commentBody,commentUsername,commentTimeStamp;
ImageView commentProfilePic;
//find all the view here
MyViewHolder(final View view) {
super(view);
commentProfilePic = (ImageView)view.findViewById(R.id.commentProfilePic);
commentUsername = (TextView)view.findViewById(R.id.commentUsername);
commentBody = (TextView)view.findViewById(R.id.commentBody);
commentTimeStamp = (TextView)view.findViewById(R.id.commentTimestamp);
}
}
public CommentAdapter(Context cContext, List<CommentItem> commentItems) {
this.cContext = cContext;
this.commentItems = commentItems;
}
#Override
public long getItemId(int position) {
return position;
}
//this one for make the adview inside this
#Override
public int getItemViewType(int position) {
return position;
}
//bind the comment item here
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View commentItemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.comment_item, parent, false);
return new MyViewHolder(commentItemView);
}
//do all the action here
#Override
public void onBindViewHolder(CommentAdapter.MyViewHolder holder, int position) {
final CommentItem commentItem = commentItems.get(position);
//commenter username
holder.commentUsername.setText(commentItem.getUsername());
//commenter profile image
Glide
.with(cContext)
.load(commentItem.getCommentProfilePic())
.fitCenter()
.into(holder.commentProfilePic);
//comment body
holder.commentBody.setText(commentItem.getCommentBody());
//comment timestamp
holder.commentTimeStamp.setText(commentItem.getCommentTimeStamp());
}
#Override
public int getItemCount() {
return commentItems.size();
}
}
I make sure my JSON is received in my volley Request.I log it out,is received in Volley onResponse .My JSON is look like this
"comment":[{"comment_created_at":"2017-03-21 13:03:40","comment_id":8,"comment_body":"abc","commenter_username":"abc","profile_image_path":"http:\/\/abc\/def\/v1\/ef\/defaultmale.jpg"
And this question I ask just tell me no need to worry about cause I using Glide to load my image,Glide will handle that
This is all the solution that tried,and still havent get it done,so what I still missing out here,in order the comment_xmlappear to the recyclerView inside Fragment.xml? Any guide that lead me to the right direction is well-appreciated.Thanks
UPDATE
Here is my setItemToCommentArrayList()
private void setItemToCommentArrayList(int commentId, String commenterUsername, String commenterProfileImage, String commentBody, String commentCreatedAt) {
CommentItem item = new CommentItem();
item.setCommentId(commentId);
item.setUsername(commenterUsername);
item.setCommentProfilePic(commenterProfileImage);
item.setCommentBody(commentBody);
item.setCommentTimeStamp(commentCreatedAt);
//save it to the comment array list
commentItems.add(item);
}
Here is my data model of Comment Item
public class CommentItem {
private String username,commentBody,commentTimeStamp,commentProfilePic;
private int commentId;
public CommentItem(){
}
public CommentItem(int commentId, String username,String commentBody,String commentTimeStamp,String commentProfilePic){
super();
this.commentId = commentId;
this.username = username;
this.commentBody = commentBody;
this.commentProfilePic= commentTimeStamp;
this.commentTimeStamp= commentProfilePic;
}
public int getCommentId() {
return commentId;
}
public void setCommentId(int commentId) {
this.commentId = commentId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getCommentBody() {
return commentBody;
}
public void setCommentBody(String commentBody) {
this.commentBody = commentBody;
}
public String getCommentTimeStamp() {
return commentTimeStamp;
}
public void setCommentTimeStamp(String commentTimeStamp) {
this.commentTimeStamp = commentTimeStamp;
}
public String getCommentProfilePic() {
return commentProfilePic;
}
public void setCommentProfilePic(String commentProfilePic) {
this.commentProfilePic = commentProfilePic;
}
}
First of all try to print out your dataSet size into getItemCount method of recyclerview -
#Override
public int getItemCount() {
Log.e("Size of data:", "" +commentItems.size())
return commentItems.size();
}
If it returns greater than zero - there may be a problem with your row_item of recyclerview. Double check your comment_item.xml file. Possibly try to remove weights.
Are items not showing in the cards or is the recycler view itself not showing?
If the recycler view itself is not showing, maybe the problem is inside the getItemCount the size being sent is 0.
do a check there and return 1 if size is zero.
#Override
public int getItemCount() {
if(commentItems.size() == 0)
return 1;
else
return commentItems.size();
}
I am trying a heterogeneous Recycler view, following this tutorial. Heterogeneous Layouts All is working fine expect for the part where i scroll the Recycler view, the layouts aren't displayed properly. I have 2 layouts, one has text and other has images, on scrolling I am left with alot of blank space in the text section,giving a feel to the viewer that there was some image previously here.
I have checked links online but nothing solved the issue.Your help is needed and appreciated.
Here is my code
ComplexRecyclerViewAdapter
public class ComplexRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Object> items;
private final int STATUS = 0, IMAGE_STATUS = 1;
public ComplexRecyclerViewAdapter(List<Object> items) {
this.items = items;
}
#Override
public int getItemCount() {
return this.items.size();
}
#Override
public int getItemViewType(int position) {
if (items.get(position) instanceof ArrayFeedItem) {
return STATUS;
} else if (items.get(position) instanceof ArrayFeedItemWithImage) {
return IMAGE_STATUS;
}
return -1;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
RecyclerView.ViewHolder viewHolder;
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
if (viewType == STATUS){
View v1 = inflater.inflate(R.layout.layout_viewholder1, viewGroup, false);
viewHolder = new ViewHolder1(v1);
}
else if(viewType ==IMAGE_STATUS){
View v2 = inflater.inflate(R.layout.layout_viewholder2, viewGroup, false);
viewHolder = new ViewHolder2(v2);
}
else
viewHolder=null;
return viewHolder;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
if (viewHolder.getItemViewType() == STATUS )
{
ViewHolder1 vh1 = (ViewHolder1) viewHolder;
configureViewHolder1(vh1, position);
vh1.setIsRecyclable(false);
}
else
{
ViewHolder2 vh2 = (ViewHolder2) viewHolder;
configureViewHolder2(vh2, position);
vh2.setIsRecyclable(false);
}
}
private void configureViewHolder1(ViewHolder1 vh1, int position) {
ArrayFeedItem item = (ArrayFeedItem) items.get(position);
if (item != null) {
vh1.getHolderImage().setImageResource(item.img);
vh1.getHolderText().setText(item.txtname);
vh1.getStatusMsg().setText(item.StatusMsg);
vh1.getTimestamp().setText(item.Timestamp);
vh1.getUrl().setText(item.URL);
}
}
private void configureViewHolder2(ViewHolder2 vh2, int position) {
ArrayFeedItemWithImage item = (ArrayFeedItemWithImage) items.get(position);
if (item != null) {
vh2.getHolderImage().setImageResource(item.img);
vh2.getHolderText().setText(item.txtname);
vh2.getStatusMsg().setText(item.StatusMsg);
vh2.getTimestamp().setText(item.Timestamp);
vh2.getUrl().setText(item.URL);
vh2.getFeedImage().setImageResource(item.feedImage1);
}
}
}
This is how I am binding the adapter to the recycle view.
Home Activity
list=(RecyclerView) findViewById(R.id.list);
adapter =new ComplexRecyclerViewAdapter(items);
list.setNestedScrollingEnabled(false);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
list.setLayoutManager(mLayoutManager);
list.setItemAnimator(new DefaultItemAnimator());
list.setAdapter(adapter);
ViewHolder1
public class ViewHolder1 extends RecyclerView.ViewHolder {
private ImageView Image;
private TextView Text,Timestamp,StatusMsg,Url;
public ViewHolder1(View itemView) {
super(itemView);
Image=(ImageView) itemView.findViewById(R.id.img);
Text=(TextView)itemView.findViewById(R.id.txt);
Timestamp=(TextView)itemView.findViewById(R.id.timestamp);
StatusMsg=(TextView)itemView.findViewById(R.id.txtStatusMsg);
Url=(TextView)itemView.findViewById(R.id.txtUrl);
}
public ImageView getHolderImage() {
return Image;
}
public void setHolderImage(ImageView image) {
this.Image = image;
}
public TextView getHolderText() {
return Text;
}
public void setHolderText(TextView text) {
this.Text = text;
}
public TextView getTimestamp() {
return Timestamp;
}
public void setTimestamp(TextView timestamp) {
this.Timestamp = timestamp;
}
public TextView getStatusMsg() {
return StatusMsg;
}
public void setStatusMsg(TextView statusmsg) {
this.StatusMsg = statusmsg;
}
public TextView getUrl() {
return Url;
}
public void setUrl(TextView url) {
this.Url = url;
}
}
layout_viewholder1
<?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"
android:background="#color/feed_bg"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="#dimen/feed_item_margin"
android:layout_marginRight="#dimen/feed_item_margin"
android:layout_marginTop="#dimen/feed_item_margin"
android:background="#drawable/bg_parent_rounded_corner"
android:orientation="vertical"
android:paddingBottom="#dimen/feed_item_padding_top_bottom"
android:paddingTop="#dimen/feed_item_padding_top_bottom"
android:id="#+id/layout1"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="#dimen/feed_item_padding_left_right"
android:paddingRight="#dimen/feed_item_padding_left_right" >
<ImageView
android:id="#+id/img"
android:layout_width="#dimen/feed_item_profile_pic"
android:layout_height="#dimen/feed_item_profile_pic"
android:scaleType="fitCenter" >
</ImageView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="#dimen/feed_item_profile_info_padd" >
<TextView
android:id="#+id/txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/feed_item_profile_name"
android:textStyle="bold"
android:textColor="#color/black"/>
<TextView
android:id="#+id/timestamp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/timestamp"
android:textSize="#dimen/feed_item_timestamp"
/>
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/txtStatusMsg"
android:textColor="#color/black"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingLeft="#dimen/feed_item_status_pad_left_right"
android:paddingRight="#dimen/feed_item_status_pad_left_right"
android:paddingTop="#dimen/feed_item_status_pad_top" />
<TextView
android:id="#+id/txtUrl"
android:textColor="#color/black"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:linksClickable="true"
android:paddingBottom="10dp"
android:paddingLeft="#dimen/feed_item_status_pad_left_right"
android:paddingRight="#dimen/feed_item_status_pad_left_right"
android:textColorLink="#color/link" />
</LinearLayout>
</LinearLayout>
Thanks in advance.
I have faced this issue and realised that in my case I was toggling view visibility inside
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {}
So what was happening is due to bad code on my part sometimes some of the views were getting completely hidden and hence were not getting removed or recylcled. This caused the randomness in the view. So if you have some code that changes the view visibility then remove that and test if the issue is still there.
pay attention to this point please!
if your app need remove item for any reason and you want to see removed item process in recyclerview so setISRecyclable to false is not good ,check it! so for every body wants to add this to adapter except(those that dont want to recycle some item for any reason) i have message:
please consider this point that in your adapter in onBindViewHolder for cases that need kind of toggle stuff Every call to onBindViewHolder must include either a load() call or a clear() call.
for example if you check the Boolean or 0-1 for setting some part of your RecyclerView item layout to gone or visible you should consider both of them in your If-Statement Block.
for example in this question i cant see else block inside configureViewHolder2 and 1 so messed up items happen maybe.
tnx to read patient reader
I setup a recyclerview with a viewholder with a click event on the recyclerview items, following the guide at https://developer.xamarin.com/guides/android/user_interface/recyclerview/
and everything worked just fine.
After that, i wanted to use this component for the swipe feature
https://components.xamarin.com/view/androidswipelayout
the swipe if working fine, but the click on the recyclerview item doens't trigger the event anymore. Any suggestions?
The itemlayout is
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<com.daimajia.swipe.SwipeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/swipe_layout"
android:clickable="false"
android:focusable="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF5534"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Elimina"
android:layout_marginLeft="30dp"
android:textColor="#fff"
android:textSize="17sp" />
<View
android:layout_height="wrap_content"
android:layout_width="0px"
android:layout_weight="1" />
<ImageView
android:id="#+id/trash"
android:layout_width="36dp"
android:layout_height="40dp"
android:layout_marginRight="50dp"
android:src="#drawable/trash" />
</LinearLayout>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="4dp"
app:cardUseCompatPadding="true"
app:cardCornerRadius="3dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="100dp"
android:layout_height="150dp"
android:layout_margin="3dp"
android:id="#+id/imageView" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/title"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:lines="1"
android:maxLines="1"
android:ellipsize="end" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/author"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="5dp"
android:layout_marginBottom="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/fairNav"
android:id="#+id/progress"
android:layout_alignBottom="#+id/imageView"
android:layout_alignRight="#+id/imageView"
android:layout_gravity="bottom"
android:layout_margin="7dp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</com.daimajia.swipe.SwipeLayout>
</FrameLayout>
the adapter:
public class BookCardAdapter : RecyclerView.Adapter
{
// Event handler for item clicks:
public event EventHandler ItemClick;
List<LibraryEdition> items;
Context mContext {get; set;}
public BookCardAdapter(List<LibraryEdition> myDataset, Context context) {
items = myDataset;
mContext = context;
}
public override RecyclerView.ViewHolder OnCreateViewHolder (ViewGroup parent, int viewType)
{
// Inflate the CardView for the photo:
View itemView = LayoutInflater.From (parent.Context).
Inflate (Resource.Layout.CardBookListItem, parent, false);
BookCardViewHolder vh = new BookCardViewHolder (itemView, OnClick);
vh.Icon.Click += (object senerObj, EventArgs eve) => {
AndroidUtils.ReadList.Delete (items [vh.AdapterPosition].EditionID);
FairbooksAPI.DeleteFromReadingList (items [vh.AdapterPosition].VersionID);
items.RemoveAt (vh.AdapterPosition);
NotifyItemRemoved (vh.AdapterPosition);
NotifyItemRangeChanged (vh.AdapterPosition, items.Count);
vh.SwipeLayout.Close (true);
AndroidUtils.toUpdateHomeReadings = true;
};
vh.SwipeLayout.SetShowMode (SwipeLayout.ShowMode.LayDown);
vh.SwipeLayout.Opened += (sender, e) => {
YoYo.With (Techniques.Tada)
.Duration (700)
.PlayOn (vh.Icon);
};
return vh;
}
// Fill in the contents of the book (invoked by the layout manager):
public override void OnBindViewHolder (RecyclerView.ViewHolder holder, int position)
{
BookCardViewHolder vh = holder as BookCardViewHolder;
vh.Image.SetImageBitmap(null);
string coverUrl = items[position].CoverUrl;
AndroidUtils.SetImage (mContext, coverUrl, vh.Image);
vh.Title.Text = items[position].Title;
vh.Author.Text = "by " + items [position].Author.UserName;
int value = (int)items [position].Percentage;
vh.Progress.Text = value > 100 ? "100%" : value + "%";
}
// Return the number of books:
public override int ItemCount
{
get { return items.Count; }
}
// Raise an event when the item-click takes place:
void OnClick (int position)
{
if (ItemClick != null)
ItemClick (this, position);
}
}
the viewholder:
public class BookCardViewHolder : RecyclerView.ViewHolder
{
public SwipeLayout SwipeLayout;
public ImageView Icon;
public ImageView Image { get; private set; }
public TextView Title { get; private set; }
public TextView Author { get; private set; }
public TextView Progress { get; private set; }
public BookCardViewHolder (View itemView, Action<int> listener)
: base (itemView)
{
SwipeLayout = itemView.FindViewById<SwipeLayout> (Resource.Id.swipe_layout);
Icon = itemView.FindViewById<ImageView> (Resource.Id.trash);
Image = itemView.FindViewById<ImageView> (Resource.Id.imageView);
Title = itemView.FindViewById<TextView> (Resource.Id.title);
Author = itemView.FindViewById<TextView> (Resource.Id.author);
Progress = itemView.FindViewById<TextView> (Resource.Id.progress);
itemView.Click += (sender, e) => listener (base.AdapterPosition);
}
}
and in the parent fragment i set the adapter like this:
mAdapter = new BookCardAdapter (Books, this.Activity);
// Register the item click handler (below) with the adapter:
mAdapter.ItemClick += OnItemClick;
mRecyclerView.SetAdapter (mAdapter);
and:
void OnItemClick (object sender, int position)
{
// ProgressDialog progress = new ProgressDialog (this.Activity);
// progress.SetMessage ("Opening Book...");
// progress.Show ();
AndroidUtils.Read (Books [position], this.Activity);
}