0

I need to implement something like this. I just want to show max. 3 or 4 items at a time and rest of the items should be scrollable.

If i have a total of 10 items in the dropdown, then only the 4 items should be visible at a time in the dropdown, and the other 6 items should be visible on scrolling down

Java code for Custom Spinner

Can any one help me i have stuck i have converted but still not able to limit height of spinner dropdown item and scroll its content

public class CompoundCodeView: Spinner,IJavaObject
{


    Context mContext;

    public CompoundCodeView(Context context) :
        base(context)
        {
        init(context);
    }
    public CompoundCodeView(Context context, IAttributeSet attrs) :
        base(context, attrs)
        {
        init(context);
    }

    public CompoundCodeView(Context context, IAttributeSet attrs, int defStyle) :
        base(context, attrs, defStyle)
        {
        init(context);
    }

    private void init(Context ctx)
    {
        mContext = ctx;
    }

        public override bool PerformClick()
    {
            bool bClicked = base.PerformClick();
        try
        {

         //   Class klass = Class.FromType(typeof(Spinner));
           var klass = Java.Lang.Class.FromType(typeof(Spinner));
            var mPopupField = klass.GetDeclaredField("mPopup");
            mPopupField.Accessible=true;
            ListPopupWindow pop = (ListPopupWindow)mPopupField.Get(this);

            ListView listview = pop.ListView;

            var vklass = Java.Lang.Class.FromType(typeof(View));
           var mScrollCacheField = vklass.GetDeclaredField("mScrollCache");
          mScrollCacheField.Accessible=true;
           Java.Lang.Object mScrollCache = mScrollCacheField.Get(listview);
            var temp = mScrollCache.Class;
            Field scrollBarField=      temp.GetDeclaredField("scrollBar");
            scrollBarField.Accessible = true;
            Java.Lang.Object scrollBar = scrollBarField.Get(mScrollCache);
            Method method = scrollBar.Class.GetDeclaredMethod("setVerticalThumbDrawable",Class.FromType(typeof(Drawable)) );
            method.Accessible = true;
            method.Invoke(scrollBar,Resources.GetDrawable(Resource.Drawable.scrollbar_style));
            if (VERSION.SdkInt >= VERSION_CODES.Honeycomb)
            {
                var vlass = Java.Lang.Class.FromType(typeof(View));
        var       mVerticalScrollbarPositionField = vlass.GetDeclaredField("mVerticalScrollbarPosition");
                mVerticalScrollbarPositionField.Accessible=true;
                mVerticalScrollbarPositionField.Set(listview,Left);
        }






         }
         catch (Java.Lang.Exception e)
        {
           // e.StackTrace;
        }

        return base.PerformClick();
    }

}

}

xam
  • 3
  • 6

1 Answers1

0

I have converted the link that you have of java code to C#,

Kindly, Take a look

public class CompoundCodeView : Spinner
{
    Context mContext;

    public CompoundCodeView(Context context) : base(context)
    {
        init(context, null);
    }

    public CompoundCodeView(Context context, Android.Util.IAttributeSet attrs) : base(context, attrs)
    {
        init(context, attrs);
    }

    public CompoundCodeView(Context context, Android.Util.IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
    {
        init(context, attrs);
    }

    private void init(Context ctx, Android.Util.IAttributeSet attrs)
    {
        mContext = ctx;
    }

    public override bool PerformClick()
    {
        bool bClicked = base.PerformClick();

        try
        {
            var klass = Java.Lang.Class.FromType(typeof(Spinner));
            Field mPopupField = klass.GetDeclaredField("mPopup");

            mPopupField.Accessible = (true);
            ListPopupWindow pop = (ListPopupWindow)mPopupField.Get(this);
            ListView listview = pop.ListView;

            var veiw = Java.Lang.Class.FromType(typeof(View));
            Field mScrollCacheField = veiw.GetDeclaredField("mScrollCache");
            mScrollCacheField.Accessible = true;

            Java.Lang.Object mScrollCache = mScrollCacheField.Get(listview);
            Field scrollBarField = mScrollCache.Class.GetDeclaredField("scrollBar");
            scrollBarField.Accessible = true;
            Java.Lang.Object scrollBar = scrollBarField.Get(mScrollCache);
            Method method = scrollBar.Class.GetDeclaredMethod("setVerticalThumbDrawable", Java.Lang.Class.FromType(typeof(Drawable)));
            method.Accessible = (true);
            method.Invoke(scrollBar, Resources.GetDrawable(Android.Resource.Drawable.scrollbar_style));


            if (Build.VERSION.SdkInt >= BuildVersionCodes.Honeycomb)
            {
                Field mVerticalScrollbarPositionField = Java.Lang.Class.FromType(typeof(View)).
                    GetDeclaredField("mVerticalScrollbarPosition");
                mVerticalScrollbarPositionField.Accessible = (true);
                mVerticalScrollbarPositionField.Set(listview, Left);
            }


            return bClicked;
        }
        catch
        {
            return bClicked;
        }
    }
}

Let me know if it doesn't work. Goodluck happy coding.

FreakyAli
  • 13,349
  • 3
  • 23
  • 63