随着即将到来的消息聊天布局不会移动上面的消息,而不是消息转移到另一个方向

时间:2017-03-27 12:40:44

标签: android chat

Chat_thread.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:orientation="vertical"
    android:gravity="left"
    android:paddingLeft="10dp"
    android:paddingRight="50dp">

    <!-- For self message -->
    <LinearLayout
        android:background="@drawable/balloon_incoming_normal"
        android:elevation="2dp"
        android:layout_margin="3dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <LinearLayout

            android:padding="8dp"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           >

            <TextView
                android:layout_marginLeft="10dp"
                android:id="@+id/textViewMessage"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello how are you?" />

            <TextView
                android:layout_marginLeft="10dp"
                android:id="@+id/textViewTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Belal Khan, 2016-05-29 16:02:00?"
                android:textSize="10dp"
                />


        </LinearLayout>
    </LinearLayout>

</LinearLayout>
**Chat_other_thread.xml**

<?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="wrap_content"
    android:orientation="vertical"
    android:gravity="right"
    android:paddingRight="10dp"
    android:paddingLeft="50dp">

    <LinearLayout
        android:background="@drawable/balloon_outgoing_normal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:elevation="2dp"
        android:orientation="vertical"
        android:padding="8dp"
        android:layout_marginLeft="3dp"
        android:layout_marginTop="3dp">

        <LinearLayout

            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        <TextView
            android:layout_marginRight="10dp"
            android:id="@+id/textViewMessage"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello how are you?" />

        <TextView
            android:layout_marginRight="10dp"
            android:id="@+id/textViewTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textSize="10dp"
            />
        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

**Demo_chat.xml**

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/list_parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".activity.MainActivity" >

    <LinearLayout
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal"
        android:background="@color/colorPrimary"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:gravity="center">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/back_white"
            android:layout_marginRight="10dp"
            />

        <LinearLayout
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/black_mix"
            android:layout_marginRight="10dp"
            >


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="0.5"
            >

            <TextView
                android:id="@+id/chat_user_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Kamal Jain"
                android:textSize="20dp"
                android:textColor="#fff"
                />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Last seen 2 days ago"
                android:textColor="#b6dbe5"
                />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >

            <LinearLayout
                android:layout_width="7dp"
                android:layout_height="7dp"
                android:background="@drawable/white_no_border"
                android:orientation="vertical"
                >

            </LinearLayout>

            <LinearLayout
                android:layout_width="7dp"
                android:layout_height="7dp"
                android:background="@drawable/white_no_border"
                android:orientation="vertical"
                android:layout_marginTop="7dp"
                android:layout_marginBottom="7dp"
                >

            </LinearLayout>

            <LinearLayout
                android:layout_width="7dp"
                android:layout_height="7dp"
                android:background="@drawable/white_no_border"
                android:orientation="vertical"
                >

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>
    <!--main title end here-->


    <ListView
        android:id="@+id/chat_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.00"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll"
        android:divider="@null"
        android:dividerHeight="5dp"/>

    <include
        android:id="@+id/footer_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/footer" />

    <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/footer_for_emoticons"
        android:layout_width="match_parent"
        android:layout_height="@dimen/keyboard_height"
        android:background="@android:color/transparent"
        android:visibility="gone" />

</LinearLayout>

GroupChatListAdapter.class

package com.trackliy.trackliy.messanger;

/**
 * Created by cbr on 3/24/2017.
 */

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.trackliy.trackliy.R;
import com.trackliy.trackliy.Session;
import com.trackliy.trackliy.model.GroupMessageModel;
import com.trackliy.trackliy.model.Message;

public class GroupChatListAdapter extends BaseAdapter {

    private ArrayList<Spanned> comments;
    private Context mContext;
    private LayoutInflater inflater;
    private Session session;

    //Tag for tracking self message
    private final int Comment_Log_In_User = 0;
    private final int Comment_Other_User = 1;


    //ArrayList of messages object containing all the messages in the thread
    private ArrayList<GroupMessageModel> groupMessagesList;

    //  public ChatListAdapter(Context context, ArrayList<Spanned> comments,String users) {
    //Constructor
    public GroupChatListAdapter(Context context, ArrayList<GroupMessageModel> groupMessages) {
        this.mContext = context;
        this.groupMessagesList = groupMessages;
        session = new Session(context);
        this.comments = comments;
        this.inflater = LayoutInflater.from(context);
    }


    public void addGroupMessage(GroupMessageModel m) {

        groupMessagesList.add(m);
        notifyDataSetChanged();
    }

    public void addGroupMessageList(ArrayList<GroupMessageModel> groupMessagesList) {

        this.groupMessagesList.addAll(groupMessagesList);

        Log.d("Addmessage",groupMessagesList.toString());

        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {

        //getting message object of current position
        GroupMessageModel message = groupMessagesList.get(position);
        // Here you have to write logic for which position have which type of row and return the same
        if (message.getUserid().equals( session.get_id())) {
            return Comment_Log_In_User;
        } else if (!message.getUserid().equals(session.get_id()) ) {
            //Returning other user
            return Comment_Other_User;

        }

        //else returning position
        return position;
    }


    public View getView(final int position, View view, ViewGroup parent) {
        ViewHolder holder = null;

        //  View v = convertView;
        if (getItemViewType(position) == Comment_Log_In_User) {

            if (view == null) {
                view = inflater.inflate(R.layout.chat_thread, parent, false);//set layout for displaying items
                holder = new ViewHolder();

                holder.textViewMessage = (TextView) view.findViewById(R.id.textViewMessage);
                holder.textViewTime = (TextView) view.findViewById(R.id.textViewTime);
                // holder.reciever_UserName=(TextView)view.findViewById(R.id.chat_user_name);
                view.setTag(holder);

            } else {

                holder = (ViewHolder) view.getTag();
            }
            GroupMessageModel message = groupMessagesList.get(position);

            SpannableString spannedTxt = new SpannableString(message.getMessage());

            holder.textViewMessage.setText(spannedTxt);

            //holder.textViewTime.setText(message.getTime_stamp());

            String formatedDate = convertTimeStampIntoFormat(String.valueOf(message.getTime_stamp()));

            holder.textViewTime.setText(formatedDate);

        } else if (getItemViewType(position) == Comment_Other_User) {
            if (view == null) {
                view = inflater.inflate(R.layout.chat_thread_other,parent, false);//set layout for displaying items
                holder = new ViewHolder();

                holder.textViewMessage = (TextView) view.findViewById(R.id.textViewMessage);
                holder.textViewTime = (TextView) view.findViewById(R.id.textViewTime);
                // holder.reciever_UserName=(TextView)view.findViewById(R.id.chat_user_name);
                view.setTag(holder);

            } else {

                holder = (ViewHolder) view.getTag();
            }
            GroupMessageModel message = groupMessagesList.get(position);

            holder.textViewMessage.setText(message.getMessage());

            holder.textViewTime.setText(message.getName());

            String formatedDate = convertTimeStampIntoFormat(String.valueOf(message.getTime_stamp()));

            holder.textViewTime.setText(formatedDate);

        }

        // final Spanned item = comments.get(position);

        /*TextView fans_image = (TextView) v.findViewById(R.id.item_text);
        fans_image.setText(item);*/

        return view;


    }

    @Override
    public int getCount() {
        return groupMessagesList.size();
    }

    @Override
    public Object getItem(int position) {
        return groupMessagesList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private String convertTimeStampIntoFormat(String time_stamp) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        String finalDate = "";
        if (time_stamp.contains(".")) {
            time_stamp = time_stamp.substring(0, time_stamp.indexOf('.'));
        }
        try {

            finalDate = sdf1.format(sdf2.parse(time_stamp));

        } catch (Exception e) {
            e.printStackTrace();
        }
        return finalDate;
    }

    public class ViewHolder {

        public TextView textViewMessage;
        public TextView textViewTime;
        public TextView reciever_UserName;
    }
}

每当我发送或获取新消息时,消息的布局都不会随消息移动而不是消息从其他方向移动。 即从左到右但它应朝同一方向移动。

请告诉我我错过了什么。

0 个答案:

没有答案