Android Studio ListView 项目重力
Posted
技术标签:
【中文标题】Android Studio ListView 项目重力【英文标题】:Android Studio ListView items gravity 【发布时间】:2017-01-05 13:25:55 【问题描述】:我想让列表视图中的项目看起来像 Messenger 中的对话。
有简单的接收消息代码:
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)
时,我希望文本位于屏幕/或列表视图的右侧并具有图像背景,就像经典的 Messenger 中一样
【问题讨论】:
【参考方案1】:我会建议您将所有您的消息添加到一个 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】:您可以创建具有 2 个视图的行。一个在左边,另一个在右边。根据您的 if 条件,您可以使其中一个视图消失而另一个可见。任何时候都只能看到其中的一个视图。
另一种方法是在其中使用单一视图并根据您的条件设置(左/右)重力。
您可以参考http://www.uandblog.com/How-to-Create-Chat-Application-using-Firebase-in-android-
。参见 MainActivity.java 和 item.xml
【讨论】:
【参考方案3】:在原始文件中使用两个 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_
android:layout_>
<com.github.library.bubbleview.BubbleTextVew
android:id="@+id/tv_chatRecive"
android:layout_
android:layout_
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_
android:layout_
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);
在聊天响应中添加键,就像我添加左右一样
【讨论】:
【参考方案4】:这是完整的例子。
drawable下的资源:
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_
android:layout_
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_
android:layout_>
</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_
android:layout_>
<FrameLayout
android:id="@+id/container"
android:layout_
android:layout_
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_
android:layout_
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();
上面代码的结果:
【讨论】:
以上是关于Android Studio ListView 项目重力的主要内容,如果未能解决你的问题,请参考以下文章
Android Studio 中带有 imageview 的 listview
在android studio中删除一个带有按钮的listview项目