预先导入数据模式:
/**
* Created by lenovo on 2018/5/18.
*/
public class DeivceListData {
private DeivceListData(){
}
//创建点击接口
public interface ClickEvent{
public abstract void onClick(Context c);
}
//创建数据单例的内部类
public class Item{
//添加需要的属性
private int image;
private String name;
private ClickEvent click;
public ClickEvent setClick(){
if(this.click != null){
return this.click;
}
return null;
}
public int getImage(){
return this.image;
}
public String getName(){
return this.name;
}
public Item (int image,String name,ClickEvent click){
this.image = image;
this.name = name;
this.click = click;
}
}
//创建list
private List<Item> list = new ArrayList<Item>();
//添加数据,并且返回集合
public List<Item> initdata(){
list.clear();
list.add(new Item(R.mipmap.default_avatar, "儿子", new ClickEvent() {
@Override
public void onClick(Context c) {
Toast.makeText(c,"你点击了儿子",Toast.LENGTH_SHORT).show();
}
}));
list.add(new Item(R.mipmap.default_avatar, "女儿", new ClickEvent() {
@Override
public void onClick(Context c) {
Toast.makeText(c,"你点击了女儿",Toast.LENGTH_SHORT).show();
}
}));
return this.list;
}
//实现class的单例模式
private static DeivceListData deivceListData ;
public static DeivceListData I (){
if(deivceListData == null){
deivceListData = new DeivceListData();
}
return deivceListData;
}
}
2.将数据导入适配器
DeivceListAdapter deivceListAdapter = new DeivceListAdapter(DeivceListData.I().initdata(),this);
recyclerView.setAdapter(deivceListAdapter);
3.在适配器里实现的点击事件,回调:
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.device_list_row_layout,parent,false);
final ViewHolder holder = new ViewHolder(view);
/*
添加选中的打勾显示
*/
holder.mListLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//将点击的位置传出去
mposition = holder.getAdapterPosition();
//将点击回调出去
mList.get(mposition).setClick().onClick(mContext);
}
});
return holder;
}
另一种形式,这种形式可以只需要一个position值(positon值可以从ListView的适配器里的点击事件用接口传出)就可以在任意的activity去操作remove或者clear在或者add,也可以直接修改某个item的值,但是需要注意clear否则在某些activity的listView里可能重复导入数据。
/**
content:每个设备的数据处理class
time:2018-07-17 14:35:49
*/
public class ChildListData {
private static ChildListData mChidListData;
private List<Item> mList = new ArrayList<>();
private ChildListData(){
}
public static ChildListData getI(){
if(mChidListData == null){
mChidListData = new ChildListData();
}
return mChidListData;
}
/**
* 添加一个item到mList中
* @param item ChildList的item
*/
public void addItem(Item item){
if(item !=null){
mList.add(item);
}
}
public List<Item> getList() {
return mList;
}
class Item{
public String name;
public String filePath;
public LatLng latLng;
public String time;
public String battery;
public int positioningMode;
public int mode;
/**
*
* @param name 名称
* @param imageFile 头像文件路径
* @param latLng 坐标
* @param time 时间
* @param battery 电量
* @param positioningMode 定位模式
* @param mode 实时模式
*/
public Item(String name, String imageFile, LatLng latLng, String time, String battery, int positioningMode, int mode) {
this.name = name;
this.filePath = imageFile;
this.latLng = latLng;
this.time = time;
this.battery = battery;
this.positioningMode = positioningMode;
this.mode = mode;
}
}
}