卜若的代码笔记-android系列-插件:带图片的Spinner插件

1.该插件是我自己封装了一下,提高用户体验,具体解释请转以下:

https://blog.csdn.net/Zaajuan1150/article/details/81704865

 

2.插件封装:

package com.draw.depp.deeplearningdrawer.Plugins.ImageSpinnerPlugins;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;

import com.draw.depp.deeplearningdrawer.R;
import com.draw.depp.deeplearningdrawer.Tool.CarColorUtil;
import com.draw.depp.deeplearningdrawer.Tool.Debug;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ImageAdapter extends SimpleAdapter {


    public Context ct;
    public ImageAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
        ct = context;
    }


    public static ImageAdapter createImageAdapter(Context ctt,List<Map<String, Object>> dataMap){

     ImageAdapter  adp =  new ImageAdapter(ctt, dataMap, R.layout.spinner_2_example,
                new String[] { "image", "text" }, new int[] {R.id.colorview, R.id.textview });

     return adp;
    }

    public static List<Map<String,Object>> mapList = new ArrayList<>();

    public static ImageAdapter createImageAdapter(Context ct2){

        List<Map<String,Object>> l2 = new ArrayList<>();

        for (Map k: mapList
             ) {
            l2.add(k);
        }

        mapList.clear();

        ImageAdapter i2 = createImageAdapter(ct2,l2);
        return i2;


    }
    public static void  addSpinner(String textValue,Object obj){

        Map<String,Object> m2 = new HashMap<String,Object>();

        m2.put("text",textValue);
        m2.put("image",obj);
        mapList.add(m2);


    }



}

3.使用到的资源:R.layout.spinner_2_example

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">

    <ImageView
        android:id="@+id/colorview"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginLeft="10dp"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center|left"
        android:paddingLeft="10dp"
        android:textSize="15dp" />

</LinearLayout>

4.调用:

 Spinner spinner = findViewById(R.id.type);
        ImageAdapter.addSpinner("广州",R.mipmap.ic_launcher);
        ImageAdapter.addSpinner("贵阳",R.mipmap.ic_launcher);
        ImageAdapter.addSpinner("华西",R.mipmap.ic_launcher);
        ImageAdapter adp = ImageAdapter.createImageAdapter(MainActivity.this);
        spinner.setAdapter(adp);

5.预览:

6.关于怎么抽出text的值,一个自写解析器:

package com.draw.depp.deeplearningdrawer.Plugins.ImageSpinnerPlugins;

public class ImageAdapterAnalyser {

    public String analyserValue  = "";

    public String[] analyserList ;
    public String text;
    public String image;
    public ImageAdapterAnalyser(String key){

        this.analyserValue = key;

        char[] charArray = new char[key.length() - 2];
        int index = 0;
        for (char a : analyserValue.toCharArray()){

            if (a== '{'){

                continue;
            }
            if(a == '}'){

                continue;
            }
            charArray[index] = a;
            index++;
        }
        analyserValue = new String(charArray);
        analyserList =  analyserValue.split(",");

        image = analyserList[0].split("=")[1].trim();
        text = analyserList[1].split("=")[1].trim();



    }
}

7.解析调用:

   spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                String content = parent.getItemAtPosition(position).toString();

                Debug.Log(new ImageAdapterAnalyser(content).text);
                type = content;

            }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值