【发布时间】:2019-10-28 18:57:43
【问题描述】:
我有以下子类用于更新我的 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;
}
在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);
【问题讨论】:
-
在添加/删除元素之前存储滚动条位置,然后在添加/删除元素后重置滚动条位置?
-
@jgoldberger-MSFT 谢谢,我该怎么做?
-
不确定,需要更多上下文。如果您提供 MCVE(最小、完整、可验证的示例。stackoverflow.com/help/mcve 基本上提供重现问题所需的所有代码。
-
@jgoldberger-MSFT 代码已添加 :)
-
仍然不够,抱歉。尝试在 Visual Studio 中创建一个简单的项目,以重现可以在 Android 设备/模拟器上运行的问题。然后提取并发布项目中的所有源代码和布局。有了上面的代码,我还需要创建一个数据库,还需要知道.cs文件的上下文(这只是.cs文件的一部分,好像是完整的.cs文件会有很多更多代码、构造函数、using 语句等)请查看 MCVE 是什么:stackoverflow.com/help/mcve
标签: c# xamarin xamarin.android