为了账号安全,请及时绑定邮箱和手机立即绑定

适配器的封装

标签:
Android

[代码]java代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

public abstract class AbsBaseAdapter<t>   extends BaseAdapter   {

 

    private final int resId;

    private List<t> list;

    private Context context;

    private ViewHolder viewHolder;

 

    public AbsBaseAdapter(Context context,int resId) {

        this.context   = context;

        list   = new ArrayList<t>();

        this.resId   = resId;

    }

    public void setData(List<t deep="10"> list){

        this.list   = list;

        notifyDataSetChanged();

    }

    public void addData(T data){

        if(!list.contains(data)){

            list.add(data);

            notifyDataSetChanged();

        }

    }

    public void clearData(){

        list.clear();

        notifyDataSetChanged();

    }

    public void remove(int poaition){

        list.remove(poaition);

        notifyDataSetChanged();

    }

    @Override

    public int getCount() {

        return  list.size();

    }

     

    @Override

    public Object getItem(int i) {

        return list.get(i);

    }

 

    @Override

    public long getItemId(int i) {

        return i;

    }

 

    @Override

      public View getView(int i, View convertView, ViewGroup viewGroup)   {

        View   view;

        if(   convertView== null){

            view   = LayoutInflater.from(context).inflate(resId,viewGroup,false);

            viewHolder   = new ViewHolder(view);

            view.setTag(viewHolder);

 

        }else{

            view   = convertView;

            viewHolder   = (ViewHolder) view.getTag();

        }

        bindData(list.get(i),viewHolder);

        return view;

    }

    public abstract void bindData(T   data,ViewHolder viewHolder);

     

    class ViewHolder{

        View   layoutView;

        Map<integer,view>   map = new HashMap<integer,view>();

 

        public ViewHolder(View layoutView) {

            this.layoutView   = layoutView;

        }

        public View getView(int id){

            View   view;

            if(map.containsKey(id)){

                view   = map.get(id);

            }else{

                view   = layoutView.findViewById(id);

                map.put(id,view);

            }

            return view;

        }

    }</integer,view></integer,view></t></t></t></t>

原文链接:http://www.apkbus.com/blog-807598-61343.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消