Android,Xamarin:点击后在Recycle Viewer中重新充气布局

时间:2018-01-04 16:13:36

标签: android

我的应用程序中有一个非常长的回收站查看器,带有可点击的标题和许多其他功能。现在,当我单击循环查看器标题内的按钮时,我需要使用其他布局重新填充它。在主要布局中,内容是一个可垂直滚动的视图,在第二个布局中图片后面的图片是3行。我设置了一个bool来检查要膨胀的布局但是我还没找到,怎么样实现这一点。这个(LONG ...)代码是包含其所有组件的recycleviewer:

 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;

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

            headerHolder.txtNewest.Click += delegate
            {
                //MoveSlider(ProfilePhotoSelection.Newest);

                Toast.MakeText(ctx, "Sort below images starting from newest to olders", ToastLength.Long).Show();
            };

            headerHolder.txtMostAp.Click += delegate
            {
                //MoveSlider(ProfilePhotoSelection.MostAp);

                Toast.MakeText(ctx, "Sort below images starting form most received ap (hardest challengs) to least amount (easiest challenge)", ToastLength.Long).Show();
            };

            headerHolder.txtMostUpVotes.Click += delegate
            {
                //MoveSlider(ProfilePhotoSelection.Gallery);

                Toast.MakeText(ctx, "Sort below images start with the one that has the most upvotes, going to one with the least amount", ToastLength.Long).Show();
            };

        }             
        else if (holder.GetType() == typeof(RecyclerViewHolder)) 
        {
             RecyclerViewHolder viewHolder = holder as RecyclerViewHolder;
             viewHolder.txtTitle.Text = (lstData[position-1].description); // - 1 wegen des Headers
             viewHolder.imageView.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 Singles
                LayoutInflater inflater = LayoutInflater.From(parent.Context);
                View itemView = inflater.Inflate(Resource.Layout.RecView_Profile_Rows, parent, false);
                return new RecyclerViewHolder(itemView, ctx);
            }
            else
            {
                // Pics in Rows
                LayoutInflater inflater = LayoutInflater.From(parent.Context);
                View itemView = inflater.Inflate(Resource.Layout.RecView_Profile_Singles, parent, false);
                return new RecyclerViewHolder(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);          
    }
}

// Eigene Klasse für den Header
public class HeaderViewHolder : RecyclerView.ViewHolder
{

    private Typeface font;
    public ImageButton btnAdd, btnAvatar, btnItemSelected, btnBigPicture;
    public TextView txtNewest, txtMostAp, txtMostUpVotes, txtApNumber, txtAp, txtFriendsNumber,
                    txtFriends, txtHofNumber, txtHof, txtUsername, txtRank, txtMotto;
    private LinearLayout linlay, linlayNavigation, linLayDivider;
    private int intHeightOfDisplay, intCurrentPositionOfSlider;

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

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


        InitButtons(itemView, ctx);
        InitTextViews(itemView, ctx);
        InitLinLays(itemView, ctx);
        InitAvatar(ctx);
        InitBigPicture(ctx);
    }

    private void InitButtons(View itemView, Context ctx)
    {
        btnAdd = itemView.FindViewById<ImageButton>(Resource.Id.btn_addfriendProfile);
        btnAvatar = itemView.FindViewById<ImageButton>(Resource.Id.btn_avatarProfile);
        btnItemSelected = itemView.FindViewById<ImageButton>(Resource.Id.btn_selecteditem_profile);
        btnBigPicture = itemView.FindViewById<ImageButton>(Resource.Id.btn_dummyBackgroundPicture_Profile);

        btnAdd.Click += delegate
        {
            AlertAddAUser(ctx);
        };

        btnAvatar.Click += delegate
        {
            AlertAvatarAndGallery(Constants.PROFILEPICTURETASK.ToString(), ctx);
        };
    }

    private void InitTextViews(View itemView, Context ctx)
    {

        txtApNumber = itemView.FindViewById<TextView>(Resource.Id.txt_ap_number_profile);
        txtAp = itemView.FindViewById<TextView>(Resource.Id.txt_ap_text_profile);
        txtFriendsNumber = itemView.FindViewById<TextView>(Resource.Id.txt_friends_number_profile);
        txtFriends = itemView.FindViewById<TextView>(Resource.Id.txt_friends_text_profile);
        txtHofNumber = itemView.FindViewById<TextView>(Resource.Id.txt_hof_number_profile);
        txtHof = itemView.FindViewById<TextView>(Resource.Id.txt_hof_text_profile);
        txtUsername = itemView.FindViewById<TextView>(Resource.Id.txt_username_profile);
        txtRank = itemView.FindViewById<TextView>(Resource.Id.txt_level_profile);
        txtMotto = itemView.FindViewById<TextView>(Resource.Id.txt_motto_profile);

        txtNewest = itemView.FindViewById<TextView>(Resource.Id.txt_newest_profile);
        txtMostAp = itemView.FindViewById<TextView>(Resource.Id.txt_mostap_profile);
        txtMostUpVotes = itemView.FindViewById<TextView>(Resource.Id.txt_mostupvotes_profile);

        font = Typeface.CreateFromAsset(ctx.Assets, "CourierNew.ttf");

        txtApNumber.SetTypeface(font, TypefaceStyle.Normal);
        txtAp.SetTypeface(font, TypefaceStyle.Normal);
        txtFriendsNumber.SetTypeface(font, TypefaceStyle.Normal);
        txtFriends.SetTypeface(font, TypefaceStyle.Normal);
        txtHofNumber.SetTypeface(font, TypefaceStyle.Normal);
        txtHof.SetTypeface(font, TypefaceStyle.Normal);
        txtUsername.SetTypeface(font, TypefaceStyle.Bold);
        txtRank.SetTypeface(font, TypefaceStyle.Normal);
        txtMotto.SetTypeface(font, TypefaceStyle.Normal);
        txtNewest.SetTypeface(font, TypefaceStyle.Normal);
        txtMostAp.SetTypeface(font, TypefaceStyle.Normal);
        txtMostUpVotes.SetTypeface(font, TypefaceStyle.Normal);

        txtUsername.Text = Activity_Profile.strUsername.ToUpper();
        txtUsername.TextSize = 18;
        txtApNumber.Text = formatXP();

        int padding = 50;

        txtUsername.SetPadding(ctx.Resources.DisplayMetrics.WidthPixels / padding, 0, 0, 0);
        txtRank.SetPadding(ctx.Resources.DisplayMetrics.WidthPixels / padding, 0, 0, 0);
        txtHof.SetPadding(0, 0, ctx.Resources.DisplayMetrics.WidthPixels / padding, 0);
        txtHofNumber.SetPadding(0, 0, ctx.Resources.DisplayMetrics.WidthPixels / padding, 0);
        txtMotto.SetPadding(ctx.Resources.DisplayMetrics.WidthPixels / padding, intHeightOfDisplay / 150, 0, 0);

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

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

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

    private void InitLinLays(View itemView, Context ctx)
    {
        linlay = itemView.FindViewById<LinearLayout>(Resource.Id.linlay_profile);
        linLayDivider = itemView.FindViewById<LinearLayout>(Resource.Id.linlay_divider_profile);
        linlayNavigation = itemView.FindViewById<LinearLayout>(Resource.Id.linlay_navigation_profile);

        linlayNavigation.LayoutParameters = new LinearLayout.LayoutParams
            (LinearLayout.LayoutParams.MatchParent, intHeightOfDisplay / 20);

        linLayDivider.LayoutParameters = new LinearLayout.LayoutParams
            (LinearLayout.LayoutParams.FillParent, intHeightOfDisplay / 25);

        double tempValue = intHeightOfDisplay / 2.85;

        FrameLayout.LayoutParams _params = new FrameLayout.LayoutParams
            (ViewGroup.LayoutParams.FillParent, (int)tempValue);

        linlay.LayoutParameters = _params;

        FrameLayout.LayoutParams xy = new FrameLayout.LayoutParams
            (FrameLayout.LayoutParams.WrapContent, FrameLayout.LayoutParams.WrapContent);
        xy.SetMargins(ctx.Resources.DisplayMetrics.WidthPixels / 100, 0, 0, 0);
        xy.Gravity = GravityFlags.Bottom | GravityFlags.Left;
        btnAvatar.LayoutParameters = xy;
    }

    private void InitAvatar(Context ctx)
    {
        List<KumulosHelper.Objects.Picture> lstPicture;

        lstPicture = Pictures.getPhotoBasedOnUserAndTask(Constants.PROFILEPICTURETASK.ToString(), 
            Activity_Profile.strUsername, "1");

        if (lstPicture.Count > 0)
        {
            Bitmap bitmap = KumulosGeneral.DecodePhotoFromBase64(lstPicture[0].photo);
            bitmap = ImageServiceDroid.ResizeImage(bitmap, ctx.Resources.DisplayMetrics.WidthPixels / 4f,
                              ctx.Resources.DisplayMetrics.WidthPixels / 4f, false);

            btnAvatar.SetImageBitmap(BitmapHelpers.DrawBorderWithOutLine(bitmap, Constants.SCALEOFCIRCLE));
        }

    }

    private void InitBigPicture(Context ctx)
    {
        List<TitlePicture> pictures = KumulosHelper.TitePictures.getPicture(Activity_Profile.strUsername, "0"); //1 für thumbnail 0 für vollbild
        if (pictures.Count == 1)
        {
            Bitmap bitmap = KumulosGeneral.DecodePhotoFromBase64(pictures[0].photo);

            Drawable intHeitOfDrawable = ctx.Resources.GetDrawableForDensity
                (Resource.Drawable.thebook_dummyBackgroundBigPicture, (int)ctx.Resources.DisplayMetrics.DensityDpi);

            bitmap = Bitmap.CreateScaledBitmap(bitmap, ctx.Resources.DisplayMetrics.WidthPixels,
                intHeitOfDrawable.IntrinsicHeight, false);
            btnBigPicture.SetImageBitmap(bitmap);
        }
        else
        {

        }

        btnBigPicture.Click += delegate 
        {
            AlertAvatarAndGallery(Constants.LANDSCAPEPICTURETASK.ToString(), ctx);
        };
    }

    private void AlertAvatarAndGallery(string id, Context ctx)
    {
        if (!Activity_Profile.boolSomeoneElsesProfile)
        {

            AlertDialog.Builder alert = new AlertDialog.Builder(ctx);

            alert.SetPositiveButton("Change Picture", (senderAlert, args) =>
            {
                Toast.MakeText(ctx, "Not yet possible", ToastLength.Short).Show();
            });

            alert.SetNeutralButton("Show Picture", (senderAlert, args) =>
            {
                var activity2 = new Intent(ctx, typeof(Activity_Userpicture_Fullscreen));
                activity2.PutExtra("username", Activity_Profile.strUsername);
                activity2.PutExtra("taskid", id);
                ctx.StartActivity(activity2);
            });

            Dialog dialog = alert.Create();
            dialog.Show();
        }
        else
        {
            var activity2 = new Intent(ctx, typeof(Activity_Userpicture_Fullscreen));

            activity2.PutExtra("username", Activity_Profile.strUsername);
            activity2.PutExtra("taskid", Constants.PROFILEPICTURETASK.ToString());
            ctx.StartActivity(activity2);
        }
    }

    public void MoveSlider(ProfilePhotoSelection selection, Context ctx)
    {
        int[] locationOnScreenTxt = new int[2];
        int lengthOfTxt = 0;
        int animateTo = 0;
        TranslateAnimation animation = null;


        switch (selection)
        {


            case ProfilePhotoSelection.MostAp:
                txtMostAp.GetLocationOnScreen(locationOnScreenTxt);
                lengthOfTxt = txtMostAp.MeasuredWidth;
                animateTo = (locationOnScreenTxt[0] - lengthOfTxt / 3);
                animation = new TranslateAnimation(intCurrentPositionOfSlider, animateTo, 0, 0);


                break;

            case ProfilePhotoSelection.Newest:
                txtNewest.GetLocationOnScreen(locationOnScreenTxt);
                lengthOfTxt = txtNewest.MeasuredWidth;
                animateTo = (locationOnScreenTxt[0] - lengthOfTxt / 3);
                animation = new TranslateAnimation(intCurrentPositionOfSlider, animateTo, 0, 0);


                Profile_RecyclerViewAdapter.boolPicsInSingles = false;
                break;

            case ProfilePhotoSelection.Gallery:
                txtMostUpVotes.GetLocationOnScreen(locationOnScreenTxt);
                lengthOfTxt = txtMostUpVotes.MeasuredWidth;
                animateTo = (locationOnScreenTxt[0] - lengthOfTxt / 4);
                animation = new TranslateAnimation(intCurrentPositionOfSlider, animateTo, 0, 0);

                Profile_RecyclerViewAdapter.boolPicsInSingles = true;
                break;

        }

        animation.FillAfter = true;
        animation.SetInterpolator(ctx, Android.Resource.Animation.AccelerateDecelerateInterpolator);
        animation.Duration = 400;
        btnItemSelected.StartAnimation(animation);
        intCurrentPositionOfSlider = animateTo;
    }

    private string formatXP()
    {
        string result = "Null";
        string temp = "Null";

        if (Account.Instance.Experience > 999)
        {
            temp = (Account.Instance.Experience.ToString("N"));
            temp = temp.Remove(temp.Length - 3, 3);
            result = temp;
        }
        else
        {
            result = Account.Instance.Experience.ToString();
        }

        return result;
    }

    private void AlertAddAUser(Context ctx)
    {
        AlertDialog.Builder alert = new AlertDialog.Builder(ctx);
        alert.SetPositiveButton("Befriend Me!", (senderAlert, args) =>
        {
            var xy = new Activity_AddAFriend();
            xy.AddFriend(Activity_Profile.strUsername, ctx);

        });

        alert.SetNeutralButton("Add Me To Group!", (senderAlert, args) =>
        {
            Toast.MakeText(ctx, "Not yet possible", ToastLength.Short).Show();
        });

        Dialog dialog = alert.Create();
        dialog.Show();
    }

}

// Init aus XML, Optik veränderungen hier!
class RecyclerViewHolder : RecyclerView.ViewHolder, View.IOnClickListener
{
    public ImageView imageView { get; set; }
    public TextView txtTitle { get; set; }
    public TextView txtDescription { get; set; }
    public TextView txtDate { get; set; }
    public TextView txtLikeCountBlue { get; set; }
    public TextView txtLikeCountGreen { get; set; }
    public TextView txtComments { get; set; }
    private Typeface font;
    private Context ctx;
    private IItemClickLIstener itemClickListener;


    public RecyclerViewHolder(View itemView, Context ctx) : base(itemView)
    {
        this.ctx = ctx;
        txtTitle = itemView.FindViewById<TextView>(Resource.Id.RecView_Profile_TaskName);
        imageView = itemView.FindViewById<ImageView>(Resource.Id.imageView1);
        txtDescription = itemView.FindViewById<TextView>(Resource.Id.RecView_Profile_Description);
        txtDate = itemView.FindViewById<TextView>(Resource.Id.RecView_Profile_Date);
        txtLikeCountBlue = itemView.FindViewById<TextView>(Resource.Id.txt_recview_profile_blue);
        txtLikeCountGreen = itemView.FindViewById<TextView>(Resource.Id.txt_recview_profile_green);
        txtComments = itemView.FindViewById<TextView>(Resource.Id.txt_recview_profile_comments);

        font = Typeface.CreateFromAsset(ctx.Assets, "CourierNew.ttf");

        txtTitle.SetTypeface(font, TypefaceStyle.Bold);
        txtDescription.SetTypeface(font, TypefaceStyle.Normal);
        txtDate.SetTypeface(font, TypefaceStyle.Normal);
        txtLikeCountBlue.SetTypeface(font, TypefaceStyle.Normal);
        txtLikeCountGreen.SetTypeface(font, TypefaceStyle.Normal);
        txtComments.SetTypeface(font, TypefaceStyle.Normal);

        itemView.SetOnClickListener(this);

    }

    public void SetItemClickListener(IItemClickLIstener itemClickListener)
    {
        this.itemClickListener = itemClickListener;
    }

    public void OnClick(View v)
    {
        itemClickListener.OnClick(v, AdapterPosition, false);
    }
}

// Getter und Setter für einzelne Optiken. Je nach Elemente die in der Scrollview gebraucht werden, anpassen.
public class DataForProfile
{
    public int imageId { get; set; }
    public Bitmap img { get; set; }
    public string description { get; set; }

}

public interface IItemClickLIstener
{
    void OnClick(View itemView, int position, bool isLongClick);
}

}

0 个答案:

没有答案