再次进入同一活动时未调用Recyclerview notifyDataSetChanged

时间:2019-05-31 12:48:55

标签: android xamarin.android

在我的需求中有向导步骤。所以如果我在第二步中,然后按回去。现在我在第一个向导中。然后按下一步以重新创建活动,这就是为什么将其放入OnCreate中的原因。

当我第一次参加该活动时,我的适配器notifydatasetchange可以正常工作,但是当我再次回到同一活动时 Notifydatasetchange()不起作用意味着未调用onbindviewholder。

即使我尝试再次设置适配器,但仍未调用。这是我的代码。我缺少什么?任何帮助表示赞赏!

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.ProposalInviteOwnersView);
       _lstviewNearByPeople = FindViewById<RecyclerView>(Resource.Id.lstviewNearByPeople);
        _layoutmanager = new LinearLayoutManager(this, LinearLayoutManager.Horizontal, false);
        _lstviewNearByPeople.HasFixedSize = true;
        _lstviewNearByPeople.SetLayoutManager(_layoutmanager);
        BindtoNearByAdapter();
    }

    public void BindtoNearByAdapter()
    {
        try
        {
            if (GlobalConst.LstNearByPeople.Count > 0)
            {
                _lstNearByPeople = new List<Owners>();
                _lstNearByPeople.AddRange(GlobalConst.LstNearByPeople);

                if(_nearByCollaboratorAdapter==null)
                {
                    _lstviewNearByPeople.Visibility = ViewStates.Visible;

                    _nearByCollaboratorAdapter = new NearByCollaboratorAdapter(this, _lstNearByPeople, this, mlastPosition);
                    _lstviewNearByPeople.SetAdapter(_nearByCollaboratorAdapter);

                }
                else
                {
                    _nearByCollaboratorAdapter.updateItems(_lstNearByPeople);

                }

            }
            else
            {
                _noNearByDevicelayout.Visibility = ViewStates.Visible;
                _lstviewNearByPeople.Visibility = ViewStates.Gone;
            }


        }
        catch (System.Exception ex)
        {
            Core.Helpers.GlobalLogic.createCrashReport(ex);
            Toast.MakeText(this, ex.Message.ToString(), ToastLength.Short).Show();
        }
    }

    public class NearByCollaboratorAdapter : RecyclerView.Adapter
{
    public List<Owners> _lstNearByPeople;
    readonly ISelectCollaborator _listner;
    readonly Activity _context;
    protected int mLastPosition;


    public NearByCollaboratorAdapter(Activity context, List<Owners> lstNearByPeople, ISelectCollaborator listner, int _mLastPosition)
    {
        _lstNearByPeople = lstNearByPeople;
        _context = context;
        _listner = listner;
        mLastPosition = _mLastPosition;

    }
    public override int ItemCount => _lstNearByPeople.Count;

    public void updateItems(List<Owners> lstNearByPeople)
    {
        _lstNearByPeople.Clear();
        _lstNearByPeople.AddRange(lstNearByPeople);
        this.NotifyDataSetChanged();
    }






}

3 个答案:

答案 0 :(得分:2)

实现onResume方法...

@Override
public void OnResume (){
  super.onResume();
  BindtoNearByAdapter()
}

答案 1 :(得分:1)

首次启动活动时,

OnCreate仅被调用一次。 如果您移至下一个活动并返回较早的活动,则不会再次调用OnCreate。因此,您需要将BindtoNearByAdapter()更改为OnStartOnResume,如@COYG所发布。我会推荐OnStart

您也可以检查此链接以更好地了解生命周期link

答案 2 :(得分:1)

我创建了一个简单的演示来模拟您的代码,但是它正常工作。 主要代码如下: PhotoAlbumAdapter.cs

 public class PhotoAlbumAdapter: RecyclerView.Adapter
{
    public event EventHandler<int> ItemClick;

    public List<Photo> mPhotoAlbum;

    //readonly ISelectCollaborator _listner;
    readonly Activity _context;
    protected int mLastPosition;

    public PhotoAlbumAdapter(Activity context, List<Photo> _mPhotoAlbum)
    {
        mPhotoAlbum = _mPhotoAlbum;
        _context = context;
        //mLastPosition = _mLastPosition;
    }

    public override RecyclerView.ViewHolder
        OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        View itemView = LayoutInflater.From(parent.Context).
                    Inflate(Resource.Layout.PhotoCardView, parent, false);

        PhotoViewHolder vh = new PhotoViewHolder(itemView, OnClick);
        return vh;
    }

    // Fill in the contents of the photo card (invoked by the layout manager):
    public override void
        OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        PhotoViewHolder vh = holder as PhotoViewHolder;

        vh.Image.SetImageResource(mPhotoAlbum[position].PhotoID);
        vh.Caption.Text = mPhotoAlbum[position].Caption;
    }

    public override int ItemCount
    {
        //get { return mPhotoAlbum.Capacity; }
        get { return mPhotoAlbum.Count; }
    }

    void OnClick(int position)
    {
        if (ItemClick != null)
            ItemClick(this, position);
    }

    public void updateItems(List<Photo> lstNearByPeople)
    {
        mPhotoAlbum.Clear();
        mPhotoAlbum.AddRange(lstNearByPeople);
        this.NotifyDataSetChanged();
    }
}


public class PhotoViewHolder : RecyclerView.ViewHolder
{
    public ImageView Image { get; private set; }
    public TextView Caption { get; private set; }

    public PhotoViewHolder(View itemView, Action<int> listener)
        : base(itemView)
    {
        Image = itemView.FindViewById<ImageView>(Resource.Id.imageView);
        Caption = itemView.FindViewById<TextView>(Resource.Id.textView);

        itemView.Click += (sender, e) => listener(base.LayoutPosition);
    }
}

MainActivity.cs

      private void BindtoNearByAdapter()
    {
        if (GlobalConst.mBuiltInPhotos.Length > 0)
        {
            _lstNearByPeople = new List<Photo>();
            _lstNearByPeople.AddRange(GlobalConst.mBuiltInPhotos);

            if (mAdapter == null)//once press back button, the activity is destroyed ,so the code go here every time
            {
                mAdapter = new PhotoAlbumAdapter(this, _lstNearByPeople);
                mRecyclerView.SetAdapter(mAdapter);
            }
            else  
            {// code never come here
                //mAdapter.updateItems(_lstNearByPeople);
            }

        }
        else
        { //if the data is not empty,  code never come here
            //_noNearByDevicelayout.Visibility = ViewStates.Visible;
            //_lstviewNearByPeople.Visibility = ViewStates.Gone;
        }
    }

效果如下: enter image description here

注意:由于我看不到您的课程NearByCollaboratorAdapterGlobalConst.LstNearByPeople的完整代码,因此您可以检查和调试应用程序的这些代码。