Xamarin,Android:重新加载recyclerview而不重新创建整个活动

时间:2018-06-14 08:51:58

标签: android

在我们的应用中,有一个聊天界面。消息将从本地数据库加载,内容将被放入我们的recyclerview中。在我们的方法OnPushReceived中,我们正在使用新消息:

    protected override void OnPushReceived(Context context, PushMessage pushMessage)
    {
              try
              {
                  if (pushMessage.Data.HasValues)
                  {
                      saveNotifactionToDB(pushMessage);
                  }
              }
              catch
              {

              }


        base.OnPushReceived(context, pushMessage);
        SendNotification(context);
    }

    private bool saveNotifactionToDB(PushMessage pushMessage)
    {
        DatabaseService db = new DatabaseService();

        db.createTableMessages();

        SQL.Message msg = new SQL.Message();

        JsonMessagePushNotification jmpn = new JsonMessagePushNotification();
        JToken fromUser = "";
        JToken content = "";
        JToken toUser = "";
        JToken time = "";
        if (pushMessage.Data.TryGetValue("fromUser", out fromUser) && pushMessage.Data.TryGetValue("content", out content) 
            && pushMessage.Data.TryGetValue("toUser", out toUser))// && pushMessage.Data.TryGetValue("time", out time)) // der letze hat false returned
        {
            msg.Fromuser = fromUser.ToString();
            msg.Content = content.ToString();
            msg.Touser = toUser.ToString();
            msg.Time = DateTime.Now.ToLongTimeString() + " " + DateTime.Now.ToShortDateString();

            return db.insertUpdateData(msg);         
        }
        return false;
    }    

并将它们保存到我们的数据库中。但问题出在这里:

只要我们的recyclerview被加载,它就会将所有来自数据库的消息加载到我们的适配器中但是当活动打开时,新消息将不会显示。我们需要重新创建整个recyclerview活动,以便完成这项工作:

 private async void InitRecViewAsync()
        {

            RecyclerView mRecyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);

            await LoadChatsAsync(Constants.LOADNEXTCOMMENTS);

            GridLayoutManager mLayoutManager = new GridLayoutManager(this, 1, GridLayoutManager.Vertical, false);
            mLayoutManager.ReverseLayout = true;
            mRecyclerView.SetLayoutManager(mLayoutManager);

            chatAdapter = new ChatAdapter(loadChat, this, toUsername);

            mRecyclerView.SetAdapter(chatAdapter);
            mLayoutManager.SetSpanSizeLookup(new GridViewSpansizeLookup(chatAdapter, mLayoutManager));

            chatAdapter.ItemClick += OnItemClick;

            var onScrollListener = new XamarinRecyclerViewOnScrollListener(mLayoutManager);

            onScrollListener.LoadMoreEvent += async (object sender, EventArgs e) =>
            {
                if (loadChat.CanLoadMoreItems && !loadChat.IsBusy)
                {
                    int oldCount = loadChat.chats.Count;

                    await LoadChatsAsync(Constants.LOADNEXTCOMMENTS);
                    if (!mRecyclerView.IsComputingLayout)
                    {
                        chatAdapter.NotifyItemRangeChanged(oldCount - 1, loadChat.chats.Count);
                    }

                }

            };

            mRecyclerView.SetItemViewCacheSize(Constants.ITEMCACHEFORCOMMENTS);
            mRecyclerView.DrawingCacheEnabled = true;
            mRecyclerView.DrawingCacheQuality = DrawingCacheQuality.High;
            mRecyclerView.AddOnScrollListener(onScrollListener);

        }

有一个名为“NotifyDataSetChanged”的函数,但这对我们来说根本不起作用。也许我们错了?

有关如何在不重新创建整个活动的情况下向Recyclerview添加一条消息的任何提示吗?那太棒了:))

由于

0 个答案:

没有答案