数据更新时如何保持gridview滚动条位置 [英] How to maintain gridview scrollbar position when data is updated

查看:39
本文介绍了数据更新时如何保持gridview滚动条位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下子类用于更新我的 gridview 的设计行:

I have the following subclass for update design rows of my gridview:

gridview 加载数据时最简单的方法是什么让滚动条位置保持在当前位置?

What is the simplest way when loading data in gridview to keep the scrollbar position at the current position?

public class TextAdapter : BaseAdapter
    {
        Context context;

        List<string> Sources;

        string res;

        public TextAdapter(Context c, List<string> s)
        {
            context = c;
            Sources = s;
        }

        public override int Count
        {
            get { return Sources.Count; }
        }

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

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

        // create a new ImageView for each item referenced by the Adapter
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            TextView textView;

            if (convertView == null)
            {
                textView = new TextView(context);
                textView.SetLines(6);
            }
            else
            {
                textView = (TextView)convertView;
            }

            textView.SetText(Sources[position], null);

            return textView;
        }

gridview 加载数据时最简单的方法是什么让滚动条位置保持在当前位置?

What is the simplest way when loading data in gridview to keep the scrollbar position at the current position?

网格视图:

<GridView
android:id="@+id/gvContagem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:numColumns="1"
android:textStyle="bold"
android:layout_below="@+id/headerLabel"
android:layout_marginTop="33dp" />

.CS 文件:

 readonly JavaList<String> artigos = new JavaList<string>();

    List<string> mItems = new List<string>();

    GridView gvContagem = FindViewById<GridView>(Resource.Id.gvContagem);
    sqliteConnection con = new SqliteConnection("Data Source = " + BaseDados);

    con.Open();

    artigos.Clear();

    string stm = "SELECT Artigo, Descricao FROM Trend";

    using (SqliteCommand cmd = new SqliteCommand(stm, con))
    {
        using (SqliteDataReader rdr = cmd.ExecuteReader())
        {
            while (rdr.Read())
            {

                artigos.Add(rdr.GetValue(0) + rdr.GetValue(1));


            }

        }

    }

gvContagem.Adapter = new TextAdapter(this,artigos);

推荐答案

事实证明,我能够以相当小的工作量制作一个工作示例.

I was able to make a working sample with fairly minimal effort as it turns out.

尝试以下操作(添加两行代码,一行在创建新的 TextAdapter 之前和之后:

Try the following (adding two lines of code, one before and one after you create the new TextAdapter:

IParcelable gridViewState = gvContagem.OnSaveInstanceState(); // <-- save scroll position
gvContagem.Adapter = new TextAdapter(this, artigos);
gvContagem.OnRestoreInstanceState(gridViewState); // <-- Load scroll position

我测试了上面的方法并且它有效,但是如果新数据集小于旧数据集,并且您在旧数据集上滚动到超出新数据集扩展的位置,那么新数据将被滚动到最后.换句话说,如果旧数据集有 20 个项目,而新数据集有 10 个项目,并且您被滚动到第 15 个项目在底部,当加载新的较小的数据集时,您将被滚动以便项目10 在底部.

I tested the above and it works, but if the new data set is smaller than the old data set, and you were scrolled on the old data set to beyond where the new data set extends, then the new data will be scrolled to the end. In other words if the old data set had 20 items, and the new data set has 10 items, and you were scrolled so that item 15 is at the bottom, when the new smaller data set is loaded, you will be scrolled so that item 10 is at the bottom.

顺便说一下,这里回答了一个有点重复的问题:通过 Screen Rotation 保持 GridView 的滚动位置,但使用原生 Java 代码.

By the way, there was a somewhat duplicate question answered here: Maintain Scroll Position of GridView through Screen Rotation , but with native Java code.

这篇关于数据更新时如何保持gridview滚动条位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆