简体   繁体   English

在android中聊天应用程序,以便发送者和接收者消息应该在不同的一面

[英]chat application in android so that sender and receiver message should be on different side

 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);

           }

In this, I would like to have message on right hand side and left hand side based on the sender and receiver. 在这里,我想根据发送者和接收者在右手边和左手边留言。

Use Custom adapter with separate layouts for sender and receiver messages. 对发件人和收件人邮件使用具有单独布局的Custom adapter It is called Heterogeneous ListView . 它被称为Heterogeneous ListView

Something like this 像这样的东西

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


    }



}

For more info on Custom Adapter you can refer this 有关Custom Adapter更多信息,您可以参考

http://www.vogella.com/articles/AndroidListView/article.html#adapterown_example http://www.vogella.com/articles/AndroidListView/article.html#adapterown_example

For heterogeneous ListView (Different row layouts in ListView) tutorial you can refer this 对于异构ListView(ListView中的不同行布局)教程,您可以参考此内容

http://chrislee.kr/wp/tag/getitemviewtype-tutorial/ http://chrislee.kr/wp/tag/getitemviewtype-tutorial/

In your onBindViewHolder() method of your recyclerView adapter you can align the sender or receiver view layout programatically using an "if" statement. 在recyclerView适配器的onBindViewHolder()方法中,您可以使用“if”语句以编程方式对齐发送方或接收方视图布局。 For example if it is sender align left or right, etc Use the code below for example to access and set the view layout parameters: 例如,如果它是发送者左对齐或右对齐等,请使用下面的代码来访问和设置视图布局参数:

 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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的聊天应用程序中未创建发件人和收件人视图 - Sender & Receiver view is not created in my chat Application 发件人/收件人之间的JMS消息流 - JMS message flow between Sender/Receiver 发送方和接收方应用程序的 output 用于 EventHub 功能 - Sender and Receiver application's output for EventHub functions 聊天应用程序。 消息字符的结尾应该是什么 - Chat application. What should be the end of message character android / ios聊天应用服务器端Java实现 - android/ios chat application server side implementation in java 如何为聊天应用程序在android中更新新消息的listview - how to update the listview for the new message in android for the chat application 第一条用户消息后,Android 气泡聊天的不同布局 - Android bubble chat different layout after first user message 发送者和接收者在不同节点上时的Java IP多播问题 - Java IP Multicast issue when sender and receiver on different node 春季引导RabbitMQ中发送方和接收方的最低要求配置是什么? - What are the minimum required configurations at sender side and receiver side in spring boot RabbitMQ? 在Android中使用RecyclerView聊天应用程序 - Chat Application with RecyclerView in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM