protected void onPostExecute( ArrayList<HashMap<String,String>> myArrayList)// for arraylist(ArrayList<String> result)
{
for (HashMap<String, String> data : myArrayList)
{
String sender_no = data.get(TAG_SENDER_NO);
String msg1=data.get(TAG_SEN_MSG);
String receiver_no=data.get(TAG_RECEIVER_NO);
if(sender_no.equals(senderno))
{
ListAdapter adapter = new SimpleAdapter(SinglechatActivity.this, myArrayList,R.layout.list_row_layout_even,
new String[] { TAG_SEN_MSG },new int[] { R.id.message_me });
// CustomList adapter= new CustomList(SinglechatActivity.this,myArrayList);//sender_no, msg1, receiver_no);
ListView lv = (ListView) findViewById(R.id.listview);
lv.setAdapter( adapter);
}
else
{
ListAdapter adapter = new SimpleAdapter(SinglechatActivity.this, myArrayList,R.layout.list_row_layout_odd,
new String[] { TAG_SEN_MSG },new int[] { R.id.message_frnd });
// CustomList adapter= new CustomList(SinglechatActivity.this, sender_no, msg1, receiver_no);
ListView lv = (ListView) findViewById(R.id.listview);
lv.setAdapter( adapter);
}
在此,我希望根据发送者和接收者在右侧和左侧发送消息。
答案 0 :(得分:3)
使用Custom adapter
为发件人和收件人邮件分别设置布局。它被称为Heterogeneous ListView
。
像这样的东西
public class MyAdapter extends BaseAdapter {
ArrayList<HashMap<String,String>> messages;
int SENDER_MESSAGE = 0;
int RECEIVER_MESSAGE = 1;
Context context;
@Override
public int getCount() {
return messages.size();
}
@Override
public Object getItem(int position) {
return messages.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
//This is dummy logic
//Write your own logic to differentiate between sender and receiver message
if (position % 2 == 0) {
return SENDER_MESSAGE;
}
else {
return RECEIVER_MESSAGE;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (getItemViewType(position) == SENDER_MESSAGE) {
convertView = inflater.inflate(R.layout.sender_message_layout, null);
}
else {
//Received message
convertView = inflater.inflate(R.layout.received_message_layout, null);
}
}
//...set text to message layout here
}
}
有关Custom Adapter
的更多信息,请参阅此
http://www.vogella.com/articles/AndroidListView/article.html#adapterown_example
对于异构ListView(ListView中的不同行布局)教程,您可以参考此
答案 1 :(得分:0)
在recyclerView适配器的onBindViewHolder()方法中,您可以使用&#34; if&#34;以编程方式对齐发送方或接收方视图布局。声明。例如,如果它是发送者左对齐或右对齐,e.t.c。例如,使用下面的代码访问和设置视图布局参数:
RelativeLayout.LayoutParams params =
(RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
button.setLayoutParams(params);