下拉控件再常见不过了,今天做一个类别的下拉菜单用到了自定义Spinner控件,再次记录一下,
首先是布局:
<Spinner
android:id="@+id/blog_spinner"
android:layout_width="wrap_content"
android:overlapAnchor="false"
android:spinnerMode="dropdown"
android:layout_height="wrap_content"
android:background="@null" />
代码部分,因为点击的时候要用到id就自己组装了下数据源给个大概吧实体类:与数据源遍历:
List<BlogClassBean> classBeanList =new ArrayList();
List<BlogClassBean> classData = new ArrayList<BlogClassBean>();
for (BlogTypeBean.ListBean listBean : response.getData().getList()) {
if (TextUtils.equals(listBean.getType(), "Class")) {
classData.add(new BlogClassBean(
listBean.getSort(),
listBean.getTypeName(),
listBean.getId(),
listBean.getType()
));
}
}
classBeanList.setValue(classData);
LogUtils.e("m_tag_blog_class", "新版组装classBeanList类别数据源:" + GsonUtil.obj2Json(classData));
public class BlogClassBean {
public BlogClassBean(String sort, String typeName, int id, String type) {
this.sort = sort;
this.typeName = typeName;
this.id = id;
this.type = type;
}
/**
* sort : 1
* typeName : FASHION
* id : 19.0
* type : Class
*/
private String sort;
private String typeName;
private int id;
private String type;
public String getSort() {
return sort;
}
public void setSort(String sort) {
this.sort = sort;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
// 建立Adapter绑定数据源
SpinnerAdapter spinnerAdapter = new SpinnerAdapter(getActivity(), new ArrayList<>());
//绑定Adapter
binding.blogSpinner.setAdapter(spinnerAdapter);
binding.blogSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
LogUtils.e("m_tag_blog_class", "选择类别为:" ++spinnerAdapter.getItem(position));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
自定义适配器:
public class SpinnerAdapter extends BaseAdapter {
private List<BlogClassBean> mList;
private Context mContext;
public SpinnerAdapter(Context pContext, List<BlogClassBean> pList) {
this.mContext = pContext;
this.mList = pList;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
/**
* 下面是重要代码
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
convertView = layoutInflater.inflate(R.layout.spinner_layout, null);
if (convertView != null) {
TextView textView = (TextView) convertView.findViewById(R.id.tv_spinner);
textView.setText(mList.get(position).getTypeName());
}
return convertView;
}
}
spinner item布局就一个textview
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tv_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:padding="8dp"
android:textSize="@dimen/textsize_12sp"
tools:text="video" />
|