1,xml文件布局
<ListView
android:id="@+id/listView"
android:divider="@null"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<GridView
android:id="@+id/gridView"
android:numColumns="2"
android:scrollbars="none"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
2,适配器
继承BaseAdapter,并实现抽象方法,封装为MyBaseAdapter
package com.shilian.yibo.base;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Toast;
import java.util.List;
/**
* BaseAdapter的二次封装
* Created by Burn on 2016/11/21.
*/
public abstract class MyBaseAdapter<T> extends BaseAdapter {
//属性,数据源
protected List<T> res_list;
protected Context context;
protected LayoutInflater inflater;
/**
* 构造方法
* */
public MyBaseAdapter(List<T> res_list, Context context) {
this.res_list = res_list;
this.context=context;
inflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return res_list.size();
}
@Override
public Object getItem(int position) {
return res_list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return myGetView(position,convertView,parent);
}
/**
* 抽象类实现布局
* */
public abstract View myGetView(int position, View convertView, ViewGroup parent);
/**
* toast显示
*/
public void toast(String content){
Toast.makeText(context, content, Toast.LENGTH_SHORT).show();
}
}
继承MyBaseAdapter实现抽象方法,实现数据适配
package com.shilian.yibo.adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.shilian.yibo.R;
import com.shilian.yibo.base.MyBaseAdapter;
import com.shilian.yibo.entity.GoodsBean;
import com.shilian.yibo.utils.UrlUtils;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* 商品管理中商品列表的适配器
* Created by Burn on 2018/1/4.
*/
public class GoodsListAdapter extends MyBaseAdapter<GoodsBean> {
private boolean isUp;
/**
* 构造方法
*/
public GoodsListAdapter(List<GoodsBean> res_list, Context context,boolean isUp) {
super(res_list, context);
this.isUp=isUp;
}
@Override
public View myGetView(int position, View convertView, ViewGroup parent) {
ViewHolder vh;
if (convertView == null) {
convertView = inflater.inflate(R.layout.layout_item_list_goods, null);
vh=new ViewHolder(convertView);
convertView.setTag(vh);
}else {
vh= (ViewHolder) convertView.getTag();
}
String img=res_list.get(position).getImg();
if (img!=null&&!img.equals("")){
Glide.with(context).load(UrlUtils.getPublicImgUrl(img)).error(R.drawable.img_default).into(vh.iv_goods);
}
vh.tv_name.setText(res_list.get(position).getName());
vh.tv_price.setText(res_list.get(position).getPrice()+"元");
vh.tv_type.setText(res_list.get(position).getType());
if (!isUp){
vh.tv_status.setText("已下架");
vh.tv_status.setTextColor(0xff2c2c2c);
vh.cb_show.setVisibility(View.GONE);
vh.tv_change_status.setText("上架");
}else {
vh.cb_show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
vh.cb_show.setChecked(res_list.get(position).getShow()==0);
}
vh.tv_edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
vh.tv_change_status.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return convertView;
}
static class ViewHolder {
@BindView(R.id.imageView_goods_item_goods_list)
ImageView iv_goods;
@BindView(R.id.textView_name_item_goods_list)
TextView tv_name;
@BindView(R.id.textView_price_item_goods_list)
TextView tv_price;
@BindView(R.id.textView_type_item_goods_list)
TextView tv_type;
@BindView(R.id.textView_status_item_goods_list)
TextView tv_status;
@BindView(R.id.checkBox_show_item_goods_list)
CheckBox cb_show;
@BindView(R.id.textView_edit_item_goods_list)
TextView tv_edit;
@BindView(R.id.textView_change_status_item_goods_list)
TextView tv_change_status;
ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}
设置适配器
listview.setAdapter(adapter);
gridview.setAdapter(adapter);
3,设置监听器
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
子项点击事件
}
});
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
子项点击事件
}
});
4,注意:listview和GridView在可滑动控件scrollview中会布局冲突导致只显示一行,解决办法在下一篇博客“scrollview中的listview和GridView”中