在android中使用自定义列表视图时无法设置左右对话
Posted
技术标签:
【中文标题】在android中使用自定义列表视图时无法设置左右对话【英文标题】:Unable to set left and right conversation while using custom listview in android 【发布时间】:2014-01-25 12:57:55 【问题描述】:我正在开发一个聊天应用程序。我已经使用 smack 库完成了它们。一切正常,即使用户也可以互相聊天,但无法将传入消息设置到 ListView
的右侧。我已经尝试将TextView
的重力转移到正确的位置。但我无法实现它。我提到了一些这些问题,但我没有帮助我!
1.Left and Right alignment
2.Listview Row Layout
public class ChatRoom extends Activity
private final static String SERVER_HOST = "talk.google.com";
private final static int SERVER_PORT = 5222;
private final static String SERVICE_NAME = "gmail.com";
private final static String LOGIN = "acd@gmail.com";
private final static String PASSWORD = "password";
private List<String> m_discussionThread;
private XMPPConnection m_connection;
private Handler m_handler;
private String mtemp_user_id;
private ProgressDialog p_dialog;
EditText mMsg;
ListView listview;
MyAdapter myadpter;
String fromName;
String mtempSpecial;
String SendMSg, RecievdMsg;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_screen);
PrepareChat prechat = new PrepareChat(); //AsyncTask
m_handler = new Handler();
p_dialog = new ProgressDialog(ChatRoom.this);
m_discussionThread = new ArrayList<String>();
myadpter = new MyAdapter(m_discussionThread);
Bundle bundle = getIntent().getExtras();
mtemp_user_id = bundle.getString("USER");
prechat.execute();
listview = (ListView) findViewById(R.id.list_view_);
listview.setAdapter(myadpter);
mMsg = (EditText) findViewById(R.id.ed_msg);
findViewById(R.id.b_MSG_To).setOnClickListener(new OnClickListener()
@Override
public void onClick(View arg0)
SendMSg = "SEND";
Message msg = new Message(mtemp_user_id, Message.Type.chat);
msg.setBody(mMsg.getText().toString());
m_connection.sendPacket(msg);
m_discussionThread.add(mMsg.getText().toString());
mMsg.setText("");
);
public class PrepareChat extends AsyncTask<Void, Void, Void>
@Override
protected void onPreExecute()
super.onPreExecute();
p_dialog.setMessage("Preparing chat");
p_dialog.setCancelable(false);
p_dialog.show();
@Override
protected Void doInBackground(Void... params)
System.out.println("In do in back ...");
try
initConnection();
catch (XMPPException e)
e.printStackTrace();
return null;
@Override
protected void onPostExecute(Void result)
super.onPostExecute(result);
p_dialog.dismiss();
private void initConnection() throws XMPPException
ConnectionConfiguration config = new ConnectionConfiguration(
SERVER_HOST, SERVER_PORT, SERVICE_NAME);
m_connection = new XMPPConnection(config);
m_connection.connect();
m_connection.login(LOGIN, PASSWORD);
System.out.println("After LOGInd");
Presence presence = new Presence(Presence.Type.available);
presence.setStatus("available");
m_connection.sendPacket(presence);
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
m_connection.addPacketListener(new PacketListener()
public void processPacket(Packet packet)
Message message = (Message) packet;
if (message.getBody() != null)
String fromName = StringUtils.parseBareAddress(message
.getFrom());
String mtempSpecial = message.getBody().toString();
m_discussionThread.add(mtemp_user_id + mtempSpecial);
RecievdMsg = "RE";
m_handler.post(new Runnable()
public void run()
myadpter.notifyDataSetChanged();
listview.setSelection(myadpter.getCount() - 1);
);
, filter);
public class MyAdapter extends BaseAdapter
LayoutInflater layoutinflate;
List<String> list = new ArrayList<String>();
Context context;
public MyAdapter(List<String> m_discussionThread)
list = m_discussionThread;
@Override
public int getCount()
return list.size();
@Override
public Object getItem(int items)
return items;
@Override
public long getItemId(int posi)
return posi;
@Override
public View getView(int position, View arg1, ViewGroup view_Group)
View view_ = arg1;
LayoutInflater layout_inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (SendMSg.equals("SEND"))
view_ = layout_inflater.inflate(R.layout.chat_b, view_Group,
false);
TextView tv_MyMsg = (TextView) view_
.findViewById(R.id.tv_mymsg);
System.out.println("in send loop");
// tv_MyMsg.setGravity(Gravity.RIGHT);
tv_MyMsg.setText(list.get(position)); //TextView not binding to the right.
SendMSg = "";
else
view_ = layout_inflater.inflate(R.layout.chat_ab, view_Group,
false);
TextView tv_FromMsg = (TextView) view_
.findViewById(R.id.tv_From_Msg);
tv_FromMsg.setText(list.get(position));
/*
* tv_ToMsg.setGravity(Gravity.LEFT);
* System.out.println("in else"); tv_FromMsg.setText("");
* tv_ToMsg.setText(list.get(position));
*/
return view_;
更新
只有 chat_ab.xml 被填充,但包含绿色文本的 chat_b.xml 被忽略。
chat_ab.xml
...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:background="#ffffff"
android:layout_gravity="left"
android:layout_ >
<TextView
android:id="@+id/tv_From_Msg"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="left"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FF00FF" />
chat_b.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:background="#ffffff"
android:layout_gravity="right"
android:layout_ >
<TextView
android:id="@+id/tv_mymsg"
android:layout_
android:layout_
android:textColor="#0000ff"
android:layout_alignParentRight="true"
android:gravity="right"
android:layout_alignParentTop="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
到目前为止我得到的是这个
我期待的是ListView
右侧的 My Message(out going) 和屏幕左侧的传出。
请建议。
【问题讨论】:
【参考方案1】:您可以通过在 LinearLayout 中使用 android:gravity
和 android:layout_gravity
属性来实现此目的。
请参考Android Listview for chat application.
【讨论】:
am 将传入和传出消息添加到已经左右对齐的不同布局中,请查看 chat_b.xml 和 chat_ab.xml【参考方案2】:在列表视图中为 textview 使用不同的布局。每次列表视图填充新条目(消息)时,使用 user.getEmail() 比较当前用户的身份验证和发件人的身份验证。 如果它们匹配,则将上述 textview 的布局重力设置为向右,否则向左。
【讨论】:
以上是关于在android中使用自定义列表视图时无法设置左右对话的主要内容,如果未能解决你的问题,请参考以下文章
在Android中按下提交按钮时使用自定义适配器从列表视图中获取选定项目
Android -- 使用自定义列表视图时如何使用 onlistitemclick() 函数