在recyclerview的适配器中设置边距

时间:2017-02-11 08:10:09

标签: android android-recyclerview

我有一个链接到RecyclerView的适配器。我试图实现聊天屏幕,我使用套接字来获取消息,如果消息来自用户在屏幕左侧显示的电话聊天气泡,如果消息来自对话者的电话聊天泡泡显示在屏幕的左侧部分。我们来看看:

普通视图:

enter image description here

当我打开键盘输入内容后,出现了问题:

打开键盘:

enter image description here

但是当我上下滚动RecyclerView时,一切都变好了。

这是我的代码:

聊天屏幕布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#F3F7FD">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_marginBottom="47dp"
            android:paddingTop="6dp"
            android:paddingBottom="8dp"
            android:clipToPadding="false"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:id="@+id/element1"
            android:layout_width="match_parent"
            android:layout_height="47dp"
            android:background="#FFFFFF"
            android:orientation="horizontal"
            android:layout_alignParentBottom="true">

            <ImageView
                android:id="@+id/sAttach"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_weight="0"
                android:layout_marginTop="9dp"
                android:layout_marginLeft="11dp"
                android:layout_marginRight="9dp"
                android:src="@drawable/attach"
                android:tint="#A7A7A7"
                android:background="?selectableItemBackgroundBorderless"
                android:clickable="true" />

            <ProgressBar
                android:id="@+id/updateBar"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_weight="0"
                android:layout_marginTop="9dp"
                android:layout_marginLeft="11dp"
                android:layout_marginRight="9dp"
                android:indeterminate="true"
                android:indeterminateTintMode="src_atop"
                android:visibility="gone" />

            <EditText
                android:id="@+id/msg"
                android:layout_width="match_parent"
                android:layout_height="47dp"
                android:layout_weight="1"
                android:paddingLeft="2dp"
                android:textSize="16sp"
                android:inputType="textCapSentences"
                android:maxLines="1"
                android:hint="@string/hint_message"
                android:background="#FFFFFF"/>

            <ImageView
                android:id="@+id/sBt"
                android:layout_width="33dp"
                android:layout_height="33dp"
                android:layout_weight="0"
                android:paddingTop="8dp"
                android:paddingBottom="8dp"
                android:paddingLeft="9dp"
                android:paddingRight="7dp"
                android:layout_marginTop="7dp"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="10dp"
                android:src="@drawable/send"
                android:background="@drawable/circle_send_gray"
                android:clickable="true" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/element2"
            android:layout_width="match_parent"
            android:layout_height="47dp"
            android:background="#FFFFFF"
            android:orientation="horizontal"
            android:layout_alignParentBottom="true"
            android:visibility="gone">

            <TextView
                android:id="@+id/blockMessage"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_marginBottom="47dp"
            android:layout_alignParentBottom="true"
            android:orientation="vertical"
            android:background="#20000000" />

    </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>

这是Chat.class:

private JsonArrayRequest getDataFromServer(String user, String room) {

        //JsonArrayRequest of volley
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(HERE GOES MY APIS URL,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        //Calling method parseData to parse the json response
                        parseData(response);
                        //Hiding the progressbar

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                        //If an error occurs that means end of the list has reached

                    }
                });

        //Returning the request
        return jsonArrayRequest;
    }

    private void parseData(JSONArray array) {
        for (int i = 0; i < array.length(); i++) {
            //Creating the superhero object
            ChatList superHero = new ChatList();
            JSONObject json = null;
            try {
                //Getting json
                json = array.getJSONObject(i);

                superHero.setMessage(json.getString("message"));
                superHero.setFromtype(json.getString("type"));
                superHero.setPhoto(json.getString("photo"));

            } catch (JSONException e) {
                e.printStackTrace();
            }
            //Adding the superhero object to the list
            listSuperHeroes.add(0, superHero);
        }

        recyclerView.scrollToPosition(listSuperHeroes.size() -1);

        //Notifying the adapter that data has been added or changed
        adapter.notifyDataSetChanged();
    }

聊天适配器:

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

    private Context context;

    private float scale;

    private int dpAsPixels;
    private int dpAsPixels2;
    private int dpAsPixels3;

    private LinearLayout.LayoutParams params;
    private LinearLayout.LayoutParams params2;

    //List to store all superheroes
    static List<ChatList> superHeroes;

    public ChatAdapter(List<ChatList> superHeroes, Context context){
        super();
        //Getting all superheroes
        this.superHeroes = superHeroes;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_list, parent, false);
        ChatAdapter.ViewHolder viewHolder = new ViewHolder(v);

        scale = context.getResources().getDisplayMetrics().density;
        dpAsPixels = (int) (3 * scale + 0.5f);
        dpAsPixels2 = (int) (12 * scale + 0.5f);
        dpAsPixels3 = (int) (12 * scale + 0.5f)+180;

        params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ChatAdapter.ViewHolder holder, int position) {

        //Getting the particular item from the list
        final ChatList superHero =  superHeroes.get(position);
        //Showing data on the views

        if(superHero.getMessage().equals("image")) {
            Picasso.with(context).load("http://mywebsite.com/uploads/media/"+superHero.getPhoto()).into(holder.photo);
            holder.photo.setVisibility(ImageView.VISIBLE);
            holder.message.setVisibility(TextView.GONE);
            params2.bottomMargin = dpAsPixels+5;
            params2.topMargin = dpAsPixels+5;
            if(superHero.getFromtype().equals("he")) {
                params.setMargins(dpAsPixels2, dpAsPixels, dpAsPixels3, dpAsPixels);
                holder.photo.setLayoutParams(params2);
                holder.photo.setBackgroundResource(R.drawable.chat_bubble_photo);
                holder.textCard.setGravity(Gravity.LEFT);
            }else{
                params.setMargins(dpAsPixels3, dpAsPixels, dpAsPixels2, dpAsPixels);
                holder.photo.setLayoutParams(params2);
                holder.photo.setBackgroundResource(R.drawable.chat_bubble_me_photo);
                holder.textCard.setGravity(Gravity.RIGHT);
            }

            holder.textCard.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ((Chat)context).openMedia(superHero.getPhoto());
                }
            });
        }else{
            holder.photo.setVisibility(ImageView.GONE);
            holder.message.setVisibility(TextView.VISIBLE);
            holder.message.setText(decodeMessage(superHero.getMessage()));
        }

        if(superHero.getFromtype().equals("he")) {
            params.setMargins(dpAsPixels2, dpAsPixels, dpAsPixels3, dpAsPixels);
            holder.message.setLayoutParams(params);
            holder.message.setBackgroundResource(R.drawable.chat_bubble);
            holder.message.setTextColor(context.getResources().getColor(R.color.darkgray));
            holder.textCard.setGravity(Gravity.LEFT);
        }else{
            params.setMargins(dpAsPixels3, dpAsPixels, dpAsPixels2, dpAsPixels);
            holder.message.setLayoutParams(params);
            holder.message.setBackgroundResource(R.drawable.chat_bubble_me);
            holder.message.setTextColor(context.getResources().getColor(R.color.colorPrimary));
            holder.textCard.setGravity(Gravity.RIGHT);
        }

    }

    public int getItemCount() {
        return superHeroes.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        //Views
        public TextView message;
        public LinearLayout textCard;
        public ImageView photo;

        //Initializing Views
        public ViewHolder(View itemView) {
            super(itemView);
            message = (TextView) itemView.findViewById(R.id.message);
            textCard = (LinearLayout) itemView.findViewById(R.id.textCard);
            photo = (ImageView) itemView.findViewById(R.id.photo);
        }
    }

    private String decodeMessage(String message) {
        String raz = URLDecoder.decode(message);
        return StringEscapeUtils.unescapeJava(raz);
    }

}

2 个答案:

答案 0 :(得分:0)

很抱歉,我无法评论为什么在这里给出答案

当你打开键盘时,你必须在最后一个位置滚动recyclerview。

0xFF

希望它有效。

答案 1 :(得分:0)

我用这种方式解决了这个问题:

我为传入和传出邮件添加了2个不同的聊天气泡,并在ChatAdapter中按setVisibility调整它们以设置应显示的气泡。边距在布局文件中设置,而不是在ChatAdapter中设置。