Hanee Park
Hanee Park

Reputation: 125

RecyclerView has unwanted spaces on it's each of String items on scroll

I am working with a chatting application, and it's ChatActivity that I got a problem. Please check out below images in the links:

  1. when it works fine

enter image description here

  1. when it has unwanted spaces

enter image description here

It's not that the spaces are in between items, but each of it's String item has unwanted space.

Since the complete code is too long, I only append below two.

recyclerview_chat.xml (recycler view item for received chat, it's TYPE_CHAT in Adapter) textView17 within CardView is the textview with the problem.

<?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:layout_width="match_parent"
          android:layout_height="wrap_content"
          >
<ImageView
    android:id="@+id/imageView4"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:background="@color/colorAccent"
    android:scaleType="fitCenter"
    app:srcCompat="@drawable/ic_profile"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_margin="5dp"/>

<TextView
    android:id="@+id/textView15"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="from "
    android:layout_toRightOf="@+id/imageView4"
    android:layout_toEndOf="@+id/imageView4"
    android:layout_alignTop="@id/imageView4"
    android:layout_marginBottom="3dp"/>

    <TextView
    android:id="@+id/textView16"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="time "
    android:layout_toRightOf="@+id/cardView"
    android:layout_toEndOf="@+id/cardView"
    android:layout_alignBottom="@id/cardView"
    android:layout_margin="5dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:textSize="12sp"/>



<android.support.v7.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/imageView4"
    android:layout_toEndOf="@+id/imageView4"
    android:layout_below="@+id/textView15"
    android:id="@+id/cardView"
    android:layout_marginBottom="10dp"
    >

    <TextView
        android:id="@+id/textView17"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text=""
        android:padding="10dp"
        android:maxWidth="230dp"/>



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

</RelativeLayout>

ChatAdapter.java

public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.MyViewHolder> {
    ArrayList<Chat> chatArrayList = new ArrayList<>();
    HashMap<String, Friend> userHashMap = new HashMap<>();
    Context context=null;
    private static final String TAG = "ChatAdapter";
    private String id;

    // type of layouts
    private static final int TYPE_MINE=1;
    private static final int TYPE_CHAT=2;
    private static final int TYPE_DATE=3;


    public ChatAdapter(Context context, String id, ArrayList<Chat> chatArrayList) {
        this.context = context;
        this.chatArrayList = chatArrayList;
        this.id = id;
    }

    public ChatAdapter(Context context,String id){
        this.context = context;
        this.id = id;
    }

    public void setChatArrayList(ArrayList<Chat> chatArrayList) {
        this.chatArrayList = chatArrayList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType){
            case TYPE_CHAT:
                View v = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.recyclerview_chat,parent,false);
                return new MyViewHolder(v,viewType);
            case TYPE_MINE:
                View v1 = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.recyclerview_chat_my,parent,false);
                return new MyViewHolder(v1,viewType);
            case TYPE_DATE:
                View v2 = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.recyclerview_chat_date,parent,false);
                return new MyViewHolder(v2,viewType);
            default:
                View v3 = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.recyclerview_chat,parent,false);
                return new MyViewHolder(v3,viewType);
        }
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Chat chat = chatArrayList.get(position);
        switch (holder.viewType){
            case TYPE_CHAT:
                holder.tv_body.setText(chat.getBody());
                holder.tv_date.setText(chat.getDate(Chat.TYPE_TIME));
                Friend friend;

                if (userHashMap.containsKey(chat.getFrom())){
                    friend = userHashMap.get(chat.getFrom());
                }else {
                    friend = holder.sqLite.getFriend(chat.getFrom());
                    userHashMap.put(chat.getFrom(),friend);
                }

                if (friend!=null){
                    String name = friend.getName(); // 친구이름 찾아서 넣기
                    holder.tv_from.setText(name);

                    // set blob type image on imageView
                    if (friend.isBlobSet()){
                        byte[] byteArray = friend.getImgBlob();
                        Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

                        // get size of imageView
                        holder.imageView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
                        int targetHeight = holder.imageView.getMeasuredHeight();
                        int targetWidth = holder.imageView.getMeasuredWidth();

                        // set image
                        holder.imageView.setImageBitmap(Bitmap.createScaledBitmap(bmp, targetWidth, // FIXME: 2017. 9. 1. OutOfMemoryError
                                targetHeight, false));
                    }

                }
                break;
            case TYPE_MINE:
                holder.my_body.setText(chat.getBody());
                holder.my_date.setText(chat.getDate(Chat.TYPE_TIME));
                break;
            case TYPE_DATE:
                holder.date.setText(chat.getDate(Chat.TYPE_DATE));
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (chatArrayList.get(position).isDateObject()) return TYPE_DATE;
        if (chatArrayList.get(position).getFrom().equals(id)){
            return TYPE_MINE;
        }else {
            return TYPE_CHAT;
        }
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder{
        int viewType;

        // TYPE_CHAT
        TextView tv_from, tv_date, tv_body;
        ImageView imageView;
        MySQLite sqLite;
//        MyPreferences pref;

        // TYPE_MINE
        TextView my_date,my_body;

        // TYPE_DATE
        TextView date;

        public MyViewHolder(View itemView,int viewType) {
            super(itemView);
            this.viewType = viewType;

            switch (viewType){
                case TYPE_CHAT:
                    tv_from = (TextView) itemView.findViewById(R.id.textView15);
                    tv_date = (TextView) itemView.findViewById(R.id.textView16);
                    tv_body = (TextView) itemView.findViewById(R.id.textView17);
                    imageView = (ImageView) itemView.findViewById(R.id.imageView4);
                    sqLite = MySQLite.getInstance(context);
//                    pref = MyPreferences.getInstance(context);
                    break;
                case TYPE_MINE:
                    my_date = (TextView) itemView.findViewById(R.id.textView13);
                    my_body = (TextView) itemView.findViewById(R.id.textView18);
                    break;
                case TYPE_DATE:
                    date = (TextView) itemView.findViewById(R.id.textView14);
                default:
                    break;
            }

        }
    }
}

Upvotes: 0

Views: 425

Answers (1)

VinayagaSundar
VinayagaSundar

Reputation: 1701

Apply trim() function before applying it to TextView

holder.my_body.setText(chat.getBody().trim());

Upvotes: 1

Related Questions