Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在recyclerview和x27中设置边距;s适配器_Android_Android Recyclerview - Fatal编程技术网

Android 在recyclerview和x27中设置边距;s适配器

Android 在recyclerview和x27中设置边距;s适配器,android,android-recyclerview,Android,Android Recyclerview,我有一个链接到RecyclerView的适配器。我正在尝试实现聊天屏幕,我使用套接字获取消息,如果消息来自用户的电话聊天气泡显示在屏幕的左侧,如果消息来自对话者的电话聊天气泡显示在屏幕的左侧。让我们看看: 正常视图: 当我打开键盘键入内容时,出现了一些问题: 打开的键盘: 但当我上下滚动RecyclerView时,一切都变好了 这是我的密码: 聊天屏幕布局: <?xml version="1.0" encoding="utf-8"?> <android.support.de

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

正常视图:

当我打开键盘键入内容时,出现了一些问题:

打开的键盘:

但当我上下滚动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>
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);
    }

}

这里是聊天室。课堂:

<?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>
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);
    }

}
私有JsonArrayRequest getDataFromServer(字符串用户,字符串室){
//JsonArrayRequest截击
JsonArrayRequest JsonArrayRequest=新的JsonArrayRequest(这里是我的API URL,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONArray响应){
//调用parseData方法来解析json响应
解析数据(响应);
//隐藏进度条
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
//若发生错误,则表示已到达列表末尾
}
});
//返回请求
返回jsonArrayRequest;
}
私有void parseData(JSONArray数组){
对于(int i=0;i
聊天适配器:

<?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>
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);
    }

}
公共类ChatAdapter扩展了RecyclerView.Adapter{
私人语境;
私人浮动比例;
私有int-dpAsPixels;
私有int-dpAsPixels2;
私有int-dpAsPixels3;
private LinearLayout.LayoutParams参数;
private LinearLayout.LayoutParams参数2;
//存储所有超级英雄的列表
静态列表超级英雄;
公共聊天适配器(列出超级英雄、上下文){
超级();
//获得所有超级英雄
这个。超级英雄=超级英雄;
this.context=上下文;
}
@凌驾
public ViewHolder onCreateViewHolder(视图组父级,int-viewType){
视图v=LayoutInflater.from(parent.getContext()).flate(R.layout.chat_list,parent,false);
ChatAdapter.ViewHolder ViewHolder=新的ViewHolder(v);
scale=context.getResources().getDisplayMetrics().density;
dpAsPixels=(int)(3*scale+0.5f);
dpAsPixels2=(int)(12*刻度+0.5f);
dpAsPixels3=(int)(12*scale+0.5f)+180;
params=新建LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_内容,LinearLayout.LayoutParams.WRAP_内容);
params2=新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_父项,LinearLayout.LayoutParams.WRAP_内容);
返回视图持有者;
}
@凌驾
public void onBindViewHolder(ChatAdapter.ViewHolder,int位置){
//从列表中获取特定项
最终聊天列表超级英雄=超级英雄.get(位置);
//显示视图上的数据
if(superHero.getMessage().equals(“image”)){
毕加索.with(context).load(“http://mywebsite.com/uploads/media/“+superHero.getPhoto()).into(holder.photo);
支架。照片。设置可见性(ImageView.VISIBLE);
holder.message.setVisibility(TextView.GONE);
params2.bottomMargin=dpAsPixels+5;
params2.topMargin=dpAsPixels+5;
if(superHero.getFromtype().equals(“他”)){
参数设置边距(dpAsPixels2、dpAsPixels、dpAsPixels3、dpAsPixels);
holder.photo.setLayoutParams(参数2);
持有者。照片。挫折资源(R.可绘制。聊天室\泡泡\照片);
holder.textCard.setGravity(Gravity.LEFT);
}否则{
参数设置边距(dpAsPixels3、dpAsPixels、dpAsPixels2、dpAsPixels);
holder.photo.setLayoutParams(参数2);
holder.photo.setBackgroundResource(R.drawable.chat\u bubble\u me\u photo);
holder.textCard.setGravity(Gravity.RIGHT);
}
holder.textCard.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
((聊天)上下文).openMedia(superHero.getPhoto());
}
});
}否则{
holder.photo.setVisibility(ImageView.GONE);
holder.message.setVisibility(TextView.VISIBLE);
holder.message.setText(decodeMessage(superHero.getMessage());
}
if(superHero.getFromtype().equals(“他”)){
参数设置边距(dpAsPixels2、dpAsPixels、dpAsPixels3、dpAsPixels);
holder.message.setLayoutParams(params);
holder.message.setBackgroundResource(R.drawable.chat_bubble);
holder.message.setTex