Why do elements move within an Android listview and aamarin?

0

I am developing an android application in xamarin chat type that shows the text in bubble form making use of BubbleView (Nuget), the problem is that in execution if you move the scroll the messages that are left are passed to the right and vice versa.

This is my code:

listReceive.axml

<?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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


  <com.github.library.bubbleview.BubbleTextView
     android:id="@+id/bubbleChat"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentRight="true"
     android:text=""
     android:padding="10dp"
     android:textColor="@android:color/white"
     android:layout_marginBottom="10dp"
     app:arrowWidth="8dp"
     app:angle="8dp"
     app:arrowHeight="10dp"
     app:arrowPosition="14dp"
     app:arrowLocation="right"
     app:bubbleColor="#7EC0EE"
     app:arrowCenter="true"
    />
</RelativeLayout>

listSend

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.github.library.bubbleview.BubbleTextView
        android:id="@+id/bubbleChat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:padding="10dp"
        android:textColor="@android:color/white"
        android:layout_marginBottom="10dp"
        app:arrowWidth="8dp"
        app:angle="8dp"
        app:arrowHeight="10dp"
        app:arrowPosition="14dp"
        app:arrowLocation="left"
        app:bubbleColor="#7EC0EE"
        app:arrowCenter="true" />
</LinearLayout>

Main.axml

<?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="match_parent">
    <ListView
        android:id="@+id/myListView"
        android:nestedScrollingEnabled="true"
        android:padding="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@null"
        android:dividerHeight="0dp" />
</LinearLayout>

ChatModel Class

public class ChatModel
    {
        public string ChatMessage { get; set; }
        public bool IsSend { get; set; }
    }

CustomAdapter Class

public class CustomAdapter : BaseAdapter
    {
        private List<ChatModel> lstChat;
        private Context context;
        private LayoutInflater inflater;

        public CustomAdapter(List<ChatModel> lstChat, Context context)
        {
            this.lstChat = lstChat;
            this.context = context;
            inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
        }

        public override int Count
        {
            get
            {
                return lstChat.Count;
            }
        }

        public override Java.Lang.Object GetItem(int position)
        {
            return position;
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View view = convertView;
            if (convertView == null)
            {
                if (lstChat[position].IsSend == true)
                    view = inflater.Inflate(Resource.Layout.listSend,null);
                else
                    view = inflater.Inflate(Resource.Layout.listReceive, null);
            }

            BubbleTextView bubble = view.FindViewById<BubbleTextView>(Resource.Id.bubbleChat);
            bubble.Text = lstChat[position].ChatMessage;
            return view;

        }
    }

MainActivity

[Activity(Label = "IA", MainLauncher = true, Icon = "@drawable/icon", Theme = "@style/Theme.AppCompat.Light.NoActionBar")]
    public class MainActivity : AppCompatActivity
    {
        List<ChatModel> lstChat = new List<ChatModel>();
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView (Resource.Layout.Main);

            setupMessage();
            CustomAdapter custom = new CustomAdapter(lstChat,this);
            ListView lstView = FindViewById<ListView>(Resource.Id.myListView);
            lstView.Adapter = custom;
        }

        private void setupMessage()
        {
            lstChat.Add(new ChatModel() {ChatMessage ="1", IsSend = true });
            lstChat.Add(new ChatModel() { ChatMessage = "2", IsSend = false });
            lstChat.Add(new ChatModel() { ChatMessage = "3", IsSend = true });
            lstChat.Add(new ChatModel() { ChatMessage = "4", IsSend = false });
            lstChat.Add(new ChatModel() { ChatMessage = "5", IsSend = true });
            lstChat.Add(new ChatModel() { ChatMessage = "6", IsSend = false });
            lstChat.Add(new ChatModel() { ChatMessage = "7", IsSend = true });
            lstChat.Add(new ChatModel() { ChatMessage = "8", IsSend = true });
            lstChat.Add(new ChatModel() { ChatMessage = "9", IsSend = false });
            lstChat.Add(new ChatModel() { ChatMessage = "10", IsSend = true });
            lstChat.Add(new ChatModel() { ChatMessage = "11", IsSend = true });
            lstChat.Add(new ChatModel() { ChatMessage = "12", IsSend = true });
            lstChat.Add(new ChatModel() { ChatMessage = "13", IsSend = true });
            lstChat.Add(new ChatModel() { ChatMessage = "14", IsSend = false });
            lstChat.Add(new ChatModel() { ChatMessage = "15", IsSend = true });
            lstChat.Add(new ChatModel() { ChatMessage = "16", IsSend = false });
            lstChat.Add(new ChatModel() { ChatMessage = "17", IsSend = true });
        }
    }
    
asked by Tiempo Sol 05.05.2017 в 05:19
source

0 answers