GridView和动态添加项

时间:2017-01-06 18:55:03

标签: c# gridview xamarin.android

我已经搜索了很多,但真的没有找到任何有用的东西。所以我在这里问。

我在Xamarin Android中这样做,我有一个ItemsGrid类,扩展到BaseAdapter。一切都工作正常,因为我能够填充项目并显示我需要显示的信息,但有一件事情不太正确,它描述如下:

在我的GridView中,当我点击按钮时动态加载更多项目,但在创建活动时添加了第一组项目。一切正常,直到我添加更多项目。因为每次我添加更多项目时,我都会将Adapter设置为ItemsGrid新实例(我每次都使用新实例)。这样就可以加载项目,但滚动查看器每次都会上升到zero。所以我添加了一个自动滚动功能,以自动滚动查看新创建的一组项目的第一项。

以上是我目前的方法,但我正在寻找更好的和UI友好的东西,因为新的项目应该在视图中已经可用的项目之后添加,因为它发生在" XAML中的UWP应用程序"

以下是" ItemsGrid.cs"的代码:

public class ItemsGrid : BaseAdapter
{

    private Context mContext;
    private int iCount = 0;
    private int mCount = 0;
    private string type = "";

    private int[] mThumbIds = new int[24];

    public override int Count
    {
        get
        {
            mThumbIds = getIds();
            return mThumbIds.Count();
        }
    }

    public ItemsGrid(Context c, int initialCount, int maxCount, string itemType)
    {
        mContext = c;
        iCount = initialCount;
        mCount = maxCount;
        type = itemType;

    }

    private int[] getIds()
    {
        string[] ids = new string[10000];

        for (int b = iCount; b < mCount; b++)
        {
            ids[b] = b + "";
        }
        int[] x = new int[mCount];
        for (int a = 0; a < mCount; a++)
        {
            if (ids[a] != null)
            {
                x[a] = Convert.ToInt32(ids[a]);
            }
        }
        return x;
    }


    public override Java.Lang.Object GetItem(int position)
    {
        return null;
    }

    public override long GetItemId(int position)
    {
        return 0;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {

        RelativeLayout layout = new RelativeLayout(mContext);
        ImageView image = new ImageView(mContext);
        TextView text = new TextView(mContext);
        TextView text2 = new TextView(mContext);
        TextView text3 = new TextView(mContext);
        if (type == "Movies")
        {
            Picasso.With(mContext).Load(MoviesActivity.MoviesImageLinks[position]).Into(image);
            text.Text = MoviesActivity.MoviesTitles[position];
            text2.Text = "Movie";
        }
        else if (type == "TV Shows")
        {
            Picasso.With(mContext).Load(TVShowsActivity.TVShowsImageLinks[position]).Into(image);
            text.Text = TVShowsActivity.TVShowsTitles[position];
            text2.Text = "TV Show";
        }
        else if (type == "Search")
        {
            Picasso.With(mContext).Load(SearchActivity.SearchImageLinks[position]).Into(image);
            text.Text = SearchActivity.SearchTitles[position];
            if (SearchActivity.SearchLinks[position].Contains("movie"))
            {
                text2.Text = "Movie";
            }
            else
            {
                text2.Text = "TV Show";
            }
        }
        image.Id = 2;
        //setting text

        text.Id = 1;
        text2.Id = 3;
        text3.Id = 4;
        text3.Text = (position + 1) + "";

        text.SetSingleLine(true);
        text.Ellipsize = Android.Text.TextUtils.TruncateAt.End;
        // setting layout
        int borderID = HelperClass.GetBorder(mContext);
        layout.SetBackgroundResource(borderID);

        RelativeLayout.LayoutParams textLayout = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent,
                ViewGroup.LayoutParams.WrapContent);
        textLayout.AddRule(LayoutRules.CenterHorizontal);
        textLayout.AddRule(LayoutRules.AlignParentTop);
        RelativeLayout.LayoutParams imageLayout = new RelativeLayout.LayoutParams(180,250);
        imageLayout.AddRule(LayoutRules.CenterHorizontal);
        imageLayout.AddRule(LayoutRules.Below, text.Id);

        RelativeLayout.LayoutParams text2Layout = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent,
                ViewGroup.LayoutParams.WrapContent);
        text2Layout.AddRule(LayoutRules.CenterHorizontal);
        text2Layout.AddRule(LayoutRules.Below, image.Id);


        RelativeLayout.LayoutParams text3Layout = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent,
                ViewGroup.LayoutParams.WrapContent);
        text3Layout.AddRule(LayoutRules.CenterHorizontal);
        text3Layout.AddRule(LayoutRules.Below, text2.Id);
        text3Layout.TopMargin = 5;

        //adding to layout
        layout.AddView(text, textLayout);
        layout.AddView(text2, text2Layout);
        layout.AddView(image, imageLayout);
        layout.AddView(text3, text3Layout);

        return layout;
    }

}

滚动到视图中: mGridView.SmoothScrollToPosition(lastListItems - 24);

ItemsGrid课程中获取项目: mGridView.Adapter = new ItemsGrid(this, lastListItems, numberOfLinks, type);

因此,如果有人有任何提示可以分享如何顺利实现,请帮助我。 感谢

0 个答案:

没有答案