Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更新数据时如何保持gridview滚动条的位置_C#_Xamarin_Xamarin.android - Fatal编程技术网

C# 更新数据时如何保持gridview滚动条的位置

C# 更新数据时如何保持gridview滚动条的位置,c#,xamarin,xamarin.android,C#,Xamarin,Xamarin.android,我的gridview的更新设计行有以下子类: 在gridview中加载数据以使滚动条位置保持在当前位置时,最简单的方法是什么 public class TextAdapter : BaseAdapter { Context context; List<string> Sources; string res; public TextAdapter(Context c, List<string> s

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

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

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;
        }
公共类TextAdapter:BaseAdapter
{
语境;
列出来源;
字符串res;
公共文本适配器(上下文c,列表s)
{
上下文=c;
来源=s;
}
公共覆盖整数计数
{
获取{return Sources.Count;}
}
public override Java.Lang.Object GetItem(int位置)
{
返回null;
}
公共覆盖长GetItemId(int位置)
{
返回0;
}
//为适配器引用的每个项目创建新的ImageView
公共覆盖视图GetView(int位置、视图转换视图、视图组父视图)
{
文本视图文本视图;
if(convertView==null)
{
textView=新的textView(上下文);
textView.SetLines(6);
}
其他的
{
textView=(textView)convertView;
}
SetText(源[位置],空);
返回文本视图;
}
在gridview中加载数据以使滚动条位置保持在当前位置时,最简单的方法是什么

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:

<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);
readonly JavaList artigos=new JavaList();
List mItems=新列表();
GridView gvContage=FindViewById(Resource.Id.gvContage);
sqliteConnection con=新的sqliteConnection(“数据源=“+BaseDados”);
con.Open();
artigos.Clear();
string stm=“选择Artigo,从趋势中描述”;
使用(SqliteCommand cmd=newsqlitecommand(stm,con))
{
使用(SqliteDataReader rdr=cmd.ExecuteReader())
{
while(rdr.Read())
{
artigos.Add(rdr.GetValue(0)+rdr.GetValue(1));
}
}
}
gvcontagage.Adapter=新文本适配器(此为artigos);

事实证明,我能够用最少的努力制作一个工作样本

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

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

IParcelable gridViewState=gvContage.OnSaveInstanceState();//在添加/删除元素之前存储滚动条位置,然后在添加/删除元素之后重置滚动条位置也许?@jgoldberger MSFT谢谢,我该怎么做?不确定,需要更多上下文。如果您提供MCVE,我会研究它(最少的、完整的、可验证的示例。基本上提供重现问题的所有必要代码。@jgoldberger MSFT代码添加:)仍然不够,抱歉。请尝试在Visual Studio中创建一个简单的项目,该项目可以复制可运行到Android设备/模拟器的问题。然后提取并发布项目中的所有源代码和布局。使用上面的代码,我仍然需要创建一个数据库,仍然需要知道.cs文件的上下文(这只是.cs文件的一部分,就像它是完整的.cs文件一样,会有更多的代码、构造函数、使用语句等)请检查一下MCVE是什么:在这种情况下,没有问题,因为记录的数量总是相同的。已解决,谢谢!