Android Scrollview+Listview 实现不同条目点赞并计数功能及原理分析(3)

过了有点久,还好上次把代码发上去了,不会影响。现在我们来解决最后一个问题

点赞后计数改变

这个问题难在哪里呢,就是我们需要在自定义的adapter中设置每个item长什么样子,每个子控件的clickListener也需要在这里设置,但是有时候点击事件需要改变外层的页面属性。比如我在item中有一个button,点击它,整个屏幕就暗了,显然需要在外界得到这个button的click事件。解决这个问题有几种方法,下面一个个说;

  1. 使用回调函数(也是代码中使用的)

在adapter中设置一个interface,然后在click中调用interface的方法,外层的代码需要实现这个interface,这样实际button的click的具体实现方法就在外层去做了。
item实现代码:

public class SampleItem extends BaseAdapter implements View.OnClickListener{
    private Context context;
    private Callback callback;
    private List<ItemMsg> listMsg;


    public List<ItemMsg> getListMsg() {
        return listMsg;
    }

    public void setListMsg(List<ItemMsg> listMsg) {
        this.listMsg = listMsg;
    }

    public SampleItem(Context context,Callback callback,List<ItemMsg> list){
        this.context = context;
        this.callback = callback;
        this.listMsg = list;
    }

    public interface Callback{
        public void myClick(View view);
    }
    @Override
    public int getCount() {
        return listMsg.size();
    }

    @Override
    public Object getItem(int i) {
        return listMsg.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder= null;
        if (view == null){
            view = LayoutInflater.from(context).inflate(R.layout.item,null);
            viewHolder = new ViewHolder();
            viewHolder.up = (ImageButton)view.findViewById(R.id.up);
            viewHolder.down = (ImageButton)view.findViewById(R.id.down);
            viewHolder.itemMsg = (TextView)view.findViewById(R.id.item_text);
            view.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder)view.getTag();
        }
        viewHolder.up.setOnClickListener(this);
        viewHolder.up.setTag(i);
        viewHolder.down.setOnClickListener(this);
        viewHolder.down.setTag(i);
        viewHolder.itemMsg.setText(Integer.toString(listMsg.get(i).getI()));
        return view;
    }

    public class ViewHolder{
        private ImageButton up,down;
        private TextView itemMsg;
    }
    @Override
    public void onClick(View view) {
        callback.myClick(view);
    }
}

外层调用代码:

public class MainActivity extends Activity implements SampleItem.Callback{
    private ListView listView;
    private SampleItem sampleItemAdapter;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
    }

    @Override
    public void myClick(View view) {
        switch(view.getId()){
            case R.id.up:
                ItemMsg itemMsgUp = sampleItemAdapter.getListMsg().get((Integer)view.getTag());
                itemMsgUp.increase();
                sampleItemAdapter.notifyDataSetChanged();
                break;
            case R.id.down:
                ItemMsg itemMsgDown = sampleItemAdapter.getListMsg().get((Integer)view.getTag());
                itemMsgDown.decrease();
                sampleItemAdapter.notifyDataSetChanged();
                break;
        }
    }
}

2 传入handler,在每一次点击事件的时候发送一个msg,然后在外层拦截

这种方式就比较“Android”,但是并不是一个好方法,每一个点击事件都需要发送一个message,效率不高,也许有些场景会接受这种。
item代码:

public class SampleItem extends BaseAdapter implements View.OnClickListener{
    public static final int UP = 1;
    public static final int DOWN = 2;
    private Context context;
    private Callback callback;
    private List<ItemMsg> listMsg;
    private Handler handler;


    public List<ItemMsg> getListMsg() {
        return listMsg;
    }

    public void setListMsg(List<ItemMsg> listMsg) {
        this.listMsg = listMsg;
    }

    public SampleItem(Context context,Callback callback,List<ItemMsg> list){
        this.context = context;
        this.callback = callback;
        this.listMsg = list;
    }

    public SampleItem(Context context,List<ItemMsg> list,Handler handler){
        this.context = context;
        this.listMsg = list;
        this.handler = handler;
    }

    public interface Callback{
        public void myClick(View view);
    }
    @Override
    public int getCount() {
        return listMsg.size();
    }

    @Override
    public Object getItem(int i) {
        return listMsg.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder= null;
        if (view == null){
            view = LayoutInflater.from(context).inflate(R.layout.item,null);
            viewHolder = new ViewHolder();
            viewHolder.up = (ImageButton)view.findViewById(R.id.up);
            viewHolder.down = (ImageButton)view.findViewById(R.id.down);
            viewHolder.itemMsg = (TextView)view.findViewById(R.id.item_text);
            view.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder)view.getTag();
        }
        viewHolder.up.setOnClickListener(this);
        viewHolder.up.setTag(i);
        viewHolder.down.setOnClickListener(this);
        viewHolder.down.setTag(i);
        viewHolder.itemMsg.setText(Integer.toString(listMsg.get(i).getI()));
        return view;
    }

    public class ViewHolder{
        private ImageButton up,down;
        private TextView itemMsg;
    }
    @Override
    public void onClick(View view) {

        //way 1
// callback.myClick(view);

        //way 2
        Message msg = new Message();
        msg.arg1 = (Integer)view.getTag();

        switch (view.getId()){
            case R.id.up:
                msg.what = UP;
                handler.sendMessage(msg);
                break;
            case R.id.down:
                msg.what = DOWN;
                handler.sendMessage(msg);
                break;
        }
    }
}

外层代码:

Handler mHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what){
                    case SampleItem.UP:
                        ItemMsg itemMsgUp = sampleItemWithHandler.getListMsg().get(msg.arg1);
                        itemMsgUp.increase();
                        sampleItemWithHandler.notifyDataSetChanged();
                        break;
                    case SampleItem.DOWN:
                        ItemMsg itemMsgDown = sampleItemWithHandler.getListMsg().get(msg.arg1);
                        itemMsgDown.decrease();
                        sampleItemWithHandler.notifyDataSetChanged();
                        break;
                }
            }
        };

这两种方法都是可以发到目的,时间不早了,再不回宿舍就关门了。。。
接下来在这个基础上加上一个下拉刷新,尽请期待~~~
新加了一种实现方式,代码地址:
http://download.csdn.net/detail/wsrspirit/9294389

你可能感兴趣的:(android,ListView,handler,回调函数,点赞)