如何使用BaseAdapter过滤Gridview图像并从资产中加载图像?

时间:2019-10-23 10:43:37

标签: c# xamarin.android

如何使用BaseAdapter过滤Gridview图像并从资产中加载图像? 我没有找到搜索的方法。 我尝试了Fitter,但它不可用。

    private void SearchView_QueryTextChange(object sender, SearchView.QueryTextChangeEventArgs e)
    {
          //Need Help SearchView in GridView
    }

我还没有找到从资源中添加图像的方法。

public class ImageAdapter : BaseAdapter
{
    Context context;
    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View view;

        if (convertView == null)
        {
            view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.Item_ConvertView, parent, false);
                 /*Need Help Load From Assets*/
            ImageView imageView = view.FindViewById<ImageView>(Resource.Id.imageViewX);
            imageView.SetImageResource(thumbIds[position]);

            TextView textView = view.FindViewById<TextView>(Resource.Id.textViewX);
            textView.Text = nameIds[position];
        }
        else
        {
            view = convertView;
        }

        return view;
    }
    private int[] thumbIds = {
    Resource.Drawable.A, Resource.Drawable.B,Resource.Drawable.C
             };
    private string[] nameIds =
    {
        "A","B","C"
    };
}

我尝试使用Assets.Open,但不必使用。

1 个答案:

答案 0 :(得分:0)

使用AssetManager.Open()方法获取图像流。然后将流解码为位图,并将其设置为ImageView。

检查代码: Activity.cs

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    internal static MainActivity Instance { get; private set; }
    protected override void OnCreate(Bundle savedInstanceState)
    {
        Instance = this;
        ...
    }
}

custom_class.cs

Context context = MainActivity.Instance;

ImageView image = FindViewById<ImageView>(Resource.Id.image);

AssetManager assets = context.Assets;
Stream stream = assets.Open("code.png");
image.SetImageBitmap(BitmapFactory.DecodeStream(stream));