从回收站视图

时间:2018-01-05 11:34:41

标签: android android-layout xamarin.android

因此,我构建了一个自定义的recyclelerview,因为我需要一个可点击的标题。几个小时后,我开始工作了。党!

但问题是,在RecView的标题内,有一个按钮可以在RecView中重新排列以下照片。

所以我建立了两种不同的布局 - 一种是水平行中总共有3张照片。这些行重复,直到没有剩余的照片 - 和另一个垂直显示照片并在宽度上填充父级的布局。所以基本上是单张照片。

这两个布局上面有两个按钮: “单张照片”|| “连续”。

点击后,我需要重新定义回收站视图的布局。问题是,标头是它自己的类,并且适配器在包装RecView的活动中实例化。

由于我发送一个静态bool来检查它是否应该是单个图片或行,我只需单击我的按钮,通过手机的后退按钮关闭活动并重新启动活动。然后另一个布局膨胀。但这非常不方便。

我完全可以重新加载整个活动,但同样需要在适配器内完成。

为了使这一点更清楚:这是我的活动:

private void initRecView()
        {

            RecyclerView recycler;
            Profile_RecyclerViewAdapter adapter;
            LinearLayoutManager layoutManager;

            startNumberOfTask = 0;

            List<KumulosHelper.Objects.Picture> pictures = 
                KumulosHelper.Pictures.getNewestXPhotosFromUserInRange
                (strUsername, "6", startNumberOfTask.ToString(), "1"); 



            foreach (var picture in pictures)
            {
                startNumberOfTask++;

                var metrics = Resources.DisplayMetrics;

                lstData.Add(new DataForProfile()
                {
                    imageId = startNumberOfTask,
                    img = (Bitmap.CreateScaledBitmap
                    (KumulosGeneral.DecodePhotoFromBase64(picture.photo),
                    metrics.WidthPixels, metrics.WidthPixels, true)),
                    description = picture.taskId.ToString()
                });


            }

            recycler = FindViewById<RecyclerView>(Resource.Id.recview);
            recycler.HasFixedSize = true;
            layoutManager = new LinearLayoutManager(this, LinearLayoutManager.Vertical, false);
            recycler.SetLayoutManager(layoutManager);
            adapter = new Profile_RecyclerViewAdapter(lstData, this);
            recycler.SetAdapter(adapter);

            recycler.AddOnScrollListener(new OnScrollListener(this, layoutManager));
        }

这是带按钮的适配器:

public class HeaderViewHolder:RecyclerView.ViewHolder     {

    public HeaderViewHolder(View itemView, Context ctx) : base(itemView)
    {

        if (ctx.Resources.DisplayMetrics.HeightPixels >= 2560)
        {
            intHeightOfDisplay = 2560;
        }
        else
        {
            intHeightOfDisplay = ctx.Resources.DisplayMetrics.HeightPixels;
        }

        InitTextViews(itemView, ctx);

    }

    private void InitTextViews(View itemView, Context ctx)
    {


        txtNewest.Click += delegate
        {
            MoveSlider(ProfilePhotoSelection.Newest, ctx);
        };

        txtMostAp.Click += delegate
        {
            MoveSlider(ProfilePhotoSelection.MostAp, ctx);
        };

        txtMostUpVotes.Click += delegate
        {
            MoveSlider(ProfilePhotoSelection.Gallery, ctx);               
        };
    }
 }

所以我只是意识到它不是适配器而是视图 - 但仍然是:

如何从ViewHolder类重新加载我的recview?

这是其所有荣耀的载体:

class Profile_RecyclerViewAdapter : RecyclerView.Adapter, IItemClickLIstener
{
    private List<DataForProfile> lstData = new List<DataForProfile>();
    private Context ctx;
    private static int TYPE_HEADER = 0;
    private static int TYPE_ITEM = 2;
    public static bool boolPicsInSingles = false;


    // Konstruktor
    public Profile_RecyclerViewAdapter(List<DataForProfile> lstData, Context ctx)
    {
        this.lstData = lstData;
        this.ctx = ctx;
    }

    // OnBindViewHolder beinhaltet die Items die recycelt (also immer mit neuen Daten bespielt) werden.
    public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {


        //Check ob Header oder Data
        if (holder.GetType() == typeof(HeaderViewHolder))
        {
            HeaderViewHolder headerHolder = holder as HeaderViewHolder;

        }
        else if (holder.GetType() == typeof(RecyclerViewHolderSingles) || holder.GetType() == typeof(RecyclerViewHolderRows))
        {
            if (boolPicsInSingles)
            {
                RecyclerViewHolderSingles viewHolder = holder as RecyclerViewHolderSingles;
                viewHolder.txtTitle.Text = (lstData[position - 1].description); 
                viewHolder.imageView.SetImageBitmap((lstData[position - 1].img));
                viewHolder.SetItemClickListener(this);
            }
            else
            {
                RecyclerViewHolderRows viewHolder = holder as RecyclerViewHolderRows;


                viewHolder.rowpic1.SetImageBitmap((lstData[position-1].img));


                viewHolder.rowpic2.SetImageBitmap((lstData[position - 1].img));


                viewHolder.rowpic3.SetImageBitmap((lstData[position - 1].img));

                viewHolder.SetItemClickListener(this);

            }

        }

    }

    // Welches Layout Inflatet wird!
    public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {

        if (viewType == TYPE_ITEM)
        {
            // Normale Items
            if (!boolPicsInSingles)
            {
                //Pics in Rows
                LayoutInflater inflater = LayoutInflater.From(parent.Context);
                View itemView = inflater.Inflate(Resource.Layout.RecView_Profile_Rows, parent, false);
                return new RecyclerViewHolderRows(itemView, ctx);
            }
            else
            {
                //Pics in Singles
                LayoutInflater inflater = LayoutInflater.From(parent.Context);
                View itemView = inflater.Inflate(Resource.Layout.RecView_Profile_Singles, parent, false);
                return new RecyclerViewHolderSingles(itemView, ctx);
            }

        }
        else if (viewType == TYPE_HEADER)
        {
            //Inflating header view
            LayoutInflater inflater = LayoutInflater.From(parent.Context);
            View itemView = inflater.Inflate(Resource.Layout.RecView_Profile_Header, parent, false);
            return new HeaderViewHolder(itemView, ctx);
        }
        else return null;
    }

    // Get Number Of Items
    public override int ItemCount
    {
        get
        {
            return lstData.Count + 1; // +1 wegen des headers!
        }
    }

    // Custom Override um zu bestimmen, ob Header oder Item
    public override int GetItemViewType(int position)
    {
        if (position == 0)
        {
            return TYPE_HEADER;
        }
        else if (position == lstData.Count() + 1) //+1 wegen des Headers
        {
            return TYPE_ITEM;
        }
        return TYPE_ITEM;
    }

    // Click Events On Picture
    public void OnClick(View itemView, int position, bool isLongClick)
    {
        var activity2 = new Intent(ctx, typeof(Activity_Userpicture_Fullscreen));
        activity2.PutExtra("username", Activity_Profile.strUsername);
        activity2.PutExtra("taskid", lstData[position - 1].description);
        ctx.StartActivity(activity2);
    }
}

3 个答案:

答案 0 :(得分:1)

方法:
 public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)

更改此

   return new HeaderViewHolder(itemView, ctx);

对此:

  return new HeaderViewHolder(itemView, ctx, this);
HeadeViewHolder Contstructor中的

更改为:

public HeaderViewHolder(View itemView, Context ctx, Profile_RecyclerViewAdapter adapter) : base(itemView)

然后在点击监听器中调用它。

adapter.notifyDataSetChanged();

答案 1 :(得分:1)

有趣的是:一个非常简单的方法是:

((Activity_Profile)ctx).Recreate();

答案 2 :(得分:0)

当您使用recyclerview执行时,rais会复制view.or unaspected situation

然后,在适配器上添加这两个@override方法

@Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }