我创建了一个可以查看图像和文本的gridview适配器。它工作正常,但当我向上滚动时,位置不对齐。但是当我向下滚动时,对齐位置可以很好地工作。
这是我向上滚动时的图片:
这是我的代码:
gridview_layout.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="15dp"
android:id="@+id/imageViewGrid"/>
<TextView
android:layout_marginTop="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewGrid"/>
</LinearLayout>
fragment_layout.axml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:padding="16dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<GridView
android:id="@+id/grid_view_image_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="110dp"
android:gravity="center"
android:numColumns="auto_fit" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center" />
</FrameLayout>
CustomeGridViewAdapter.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Java.Lang;
using Square.Picasso;
namespace testing_code
{
public class CustomGridViewAdapter : BaseAdapter
{
private Context context;
private string[] gridViewString;
private string[] gridViewImage;
public CustomGridViewAdapter(Context context, string[] gridViewstr, string[] gridViewImage )
{
this.context = context;
gridViewString = gridViewstr;
this.gridViewImage = gridViewImage;
}
public override int Count
{
get
{
return gridViewString.Length;
}
}
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)
{
View view;
LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
if(convertView == null)
{
view = new View(context);
view = inflater.Inflate(Resource.Layout.gridview_layout, null);
TextView txtview = view.FindViewById<TextView>(Resource.Id.textViewGrid);
ImageView imgview = view.FindViewById<ImageView>(Resource.Id.imageViewGrid);
txtview.Text = gridViewString[position];
Picasso.With(context)
.Load((gridViewImage[position]))
.Into(imgview);
}
else
{
view = (View)convertView;
}
return view;
}
}
}
Fragment1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using SupportFragment = Android.Support.V4.App.Fragment;
using Android.Support.Design.Widget;
using Android.Support.V7.App;
using Android.Support.V4.Widget;
using Android.Support.V4.App;
using MySql.Data.MySqlClient;
using System.Data;
namespace testing_code
{
public class Fragment1 : SupportFragment
{
MySqlConnection conn = new MySqlConnection();
string query = "server=sample.com;port=3306;database=db_sample;user id=root;password=123";
GridView gridview;
List<string> gridviewstring = new List<string>();
List<string> imgview = new List<string>();
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
// return inflater.Inflate(Resource.Layout.YourFragment, container, false);
View view = inflater.Inflate(Resource.Layout.fragment_layout, container, false);
conn.ConnectionString = query;
MySqlCommand cmd = new MySqlCommand("Select * from wp_mobile_product", conn);
try
{
conn.Open();
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
string get_id = reader["product_title"].ToString();
string get_img = reader["product_image"].ToString();
gridviewstring.Add(get_id);
imgview.Add(get_img);
}
}
catch (MySqlException ex)
{
Android.Support.V7.App.AlertDialog.Builder except = new Android.Support.V7.App.AlertDialog.Builder(Activity);
except.SetTitle("Please report this to our website(error server timeout)");
except.SetMessage(ex.ToString());
except.SetPositiveButton("Ok", (senderAlert, args) =>
{
except.Dispose();
});
except.Show();
}
finally
{
conn.Close();
}
string[] GridViewStringArray = gridviewstring.ToArray();
string[] GridImgViewArray = imgview.ToArray();
CustomGridViewAdapter adapter = new CustomGridViewAdapter(Activity, GridViewStringArray, GridImgViewArray);
gridview = view.FindViewById<GridView>(Resource.Id.grid_view_image_text);
gridview.Adapter = adapter;
return view;
}
}
}
答案 0 :(得分:1)
我已经测试了您的代码,原因似乎是您的skipWaiting()
中有一个很长的文字。
有几种解决方案:
TextView
,而不是使用TextView
。wrap_content
。android:singleLine
设为true
TextView
设置android:minLines
的最小行号,例如:TextView