Android Studio ListView项目引力

时间:2016-08-29 13:28:29

标签: java android listview android-studio

我希望listview中的项目看起来像是信使中的对话。

收到消息的代码很简单:

if (arrayList2.get(y).toString().equals(myEmail)) {
    arrayList.add(arrayList2.get(k).toString());

} else if (arrayList2.get(y).toString().equals(recipientEmail)) {
    arrayList.add(arrayList2.get(k).toString());
}

arrayAdapter.notifyDataSetChanged();

array.get.....equals(myEmail)时,消息来自我,他们应该保持原样。当...equals(recipientEmail)时,我希望文本位于屏幕/列表视图的右侧,并且具有图像背景,例如经常在信使中

Should be bacground of text

Or te best would be looking like this

4 个答案:

答案 0 :(得分:0)

您可以创建一个包含2个视图的行。一个在左边,另一个在右边。根据您的if条件,您可以使其中一个视图消失,另一个视图可见。任何时候只能看到一个视图。

另一种方法是在其中使用单个视图并根据您的条件设置(左/右)重力。

您可以参考http://www.uandblog.com/How-to-Create-Chat-Application-using-Firebase-in-Android-。请参阅MainActivity.java和item.xml

答案 1 :(得分:0)

我建议您在一个Arraylist中添加所有邮件,并创建两个布局,其中一个sender具有左重力,另一个receiver具有正确的重力,然后制作逻辑像 -

if(arraylist.get(position).getUserID==currentUser){
  senderLayout.setvisibility(View.VISIBLE);
  senderTextview.setText(arraylist.get(position).getText) 
} 
 else{
  senderLayout.setvisibility(View.GONE);
  receiverLayout.setvisibility(View.VISIBLE);
  recieverTextview.setText(arraylist.get(position).getText)
}

在适配器的getView方法中执行此操作。

答案 2 :(得分:0)

在原始文件中使用两个TextView

<?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="match_parent">

<com.github.library.bubbleview.BubbleTextVew
    android:id="@+id/tv_chatRecive"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="7dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="35dp"
    android:layout_marginTop="8dp"
    android:padding="10dp"
    android:textColor="@android:color/white"
    android:visibility="gone"
    app:angle="5dp"
    app:arrowHeight="12dp"
    app:arrowLocation="left"
    app:arrowPosition="5dp"
    app:arrowWidth="12dp"
    app:bubbleColor="@color/colorPrimary" />



<com.github.library.bubbleview.BubbleTextVew
    android:id="@+id/tv_chatSend"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="7dp"
    android:layout_marginLeft="35dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="8dp"
    android:padding="10dp"
    android:textColor="@color/text_labelColoe"
    android:visibility="gone"
    app:angle="5dp"
    app:arrowHeight="12dp"
    app:arrowLocation="right"
    app:arrowPosition="5dp"
    app:arrowWidth="12dp"
    app:bubbleColor="@android:color/white" />


   </RelativeLayout>

并在Adapter类中使用这种逻辑来管理textview

 @Override
public void onBindViewHolder(final CustomViewHolder customViewHolder, final int position) {
    if (listChat.get(position).CHAT_SEND_RECIVE.equalsIgnoreCase("right")) {
        customViewHolder.tvSend.setVisibility(View.VISIBLE);
        customViewHolder.tvRecive.setVisibility(View.GONE);
        customViewHolder.tvSend.setText(listChat.get(position).CHAT_MESSAGE);
    } else if (listChat.get(position).CHAT_SEND_RECIVE.equalsIgnoreCase("left")) {
        customViewHolder.tvRecive.setVisibility(View.VISIBLE);
        customViewHolder.tvSend.setVisibility(View.GONE);
        customViewHolder.tvRecive.setText(listChat.get(position).CHAT_MESSAGE);

    }

}

在聊天响应中添加密钥,就像我添加左右

一样

答案 3 :(得分:0)

以下是完整示例。

drawable下的资源:

out in

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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"
    tools:context="com.example.andrii.myapplication.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/chat"
        android:background="@color/colorAccent"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>
</RelativeLayout>

消息布局将如此(message.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="15dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:paddingRight="10dp"
        android:background="@drawable/in_9">
        <TextView
            android:id="@+id/message"
            android:textSize="18sp"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:breakStrategy="balanced"
            android:maxWidth="200dp"
            android:text="test"
            />
    </FrameLayout>
</LinearLayout>

最后在MainActivity.java中:

package com.example.andrii.myapplication;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.LinkedList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<Message> list=  new LinkedList<>();
        list.add(new Message(true, "hi"));
        list.add(new Message(false, "hello"));
        list.add(new Message(true, "how are you"));
        list.add(new Message(false, "I'm fine"));
        list.add(new Message(false, "Test test test test test"));
        RecyclerView view = (RecyclerView) findViewById(R.id.chat);
        view.setLayoutManager(new LinearLayoutManager(this));
        view.setAdapter(new ChatAdapter(this, list));
    }

    private class Message {
        private boolean misMine;
        private String mMessage;
        public Message (boolean mine, String message) {
            misMine = mine;
            mMessage = message;
        }
        public boolean isMine() { return misMine; }
        public String getMessage() { return mMessage; }
    }


    private class MessageViewHolder extends RecyclerView.ViewHolder {
        private View mContainer;
        private TextView mMessage;

        public MessageViewHolder(View itemView) {
            super(itemView);
            mMessage = (TextView) itemView.findViewById(R.id.message);
            mContainer = itemView.findViewById(R.id.container);
        }

        public void setMessage(String message) {
            mMessage.setText(message);
        }

        public void setMine(boolean isMine) {
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mContainer.getLayoutParams();
            params.gravity = isMine ? Gravity.RIGHT : Gravity.LEFT;

            mContainer.setBackgroundResource(isMine ? R.drawable.out_9 : R.drawable.in_9 );
            mContainer.setLayoutParams(params);
        }
    }

    private class ChatAdapter extends RecyclerView.Adapter<MessageViewHolder> {
        private List<Message> mMessages;

        public ChatAdapter(Context context, List<Message> list) {
            mMessages = list;
        }

        @Override
        public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message, parent, false);

            MessageViewHolder holder = new MessageViewHolder(view);

            return holder;
        }

        @Override
        public void onBindViewHolder(MessageViewHolder holder, int position) {
            Message  message =  mMessages.get(position);

            holder.setMessage(message.getMessage());
            holder.setMine(message.isMine());
        }

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

以上代码的结果:

enter image description here