列表视图项内的复选框更改状态

时间:2013-10-02 11:52:03

标签: android android-listview

大家好我有一个简单的listview和一个扩展的baseadapter,我希望实现以下功能。

我有一个简单的列表项布局:1个复选框和两个列表视图。

当点击列表项目时(整个布局,而不仅仅是复选框),复选框状态应该会改变。

如何实现此功能?

这是我到目前为止所做的事情:

我的适配器:

 package ro.gebs.captoom.adapters;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;

import java.text.DecimalFormat;
import java.util.ArrayList;

import ro.gebs.captoom.R;
import ro.gebs.captoom.datamodel.Mileage;
import ro.gebs.captoom.datamodel.Receipt;
import ro.gebs.captoom.interfaces.MileageReceiptInterface;
import ro.gebs.captoom.utils.fonts.CustomFontTextView;

/**
 * Created by Adrian on 9/19/13.
 */
public class SyncItemAdapter extends BaseAdapter {

    private ArrayList<MileageReceiptInterface> data;
    private Context context;

    public SyncItemAdapter(Context context, ArrayList<MileageReceiptInterface> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public MileageReceiptInterface getItem(int position) {
        return data.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final MileageReceiptInterface item = getItem(position);
        ViewHolder holder = null;

        if (convertView == null) {
            LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (item != null) {
                holder = new ViewHolder();
                if (item.getMyType() == MileageReceiptInterface.I_AM_MILEAGE) {
                    convertView = li.inflate(R.layout.sync_mileage_item, parent, false);
                } else {
                    convertView = li.inflate(R.layout.sync_receipt_item, parent, false);
                }

                holder.clickMe = (LinearLayout) convertView;
                holder.itemDate = (CustomFontTextView) convertView.findViewById(R.id.item_date);
                holder.itemStatus = (CheckBox) convertView.findViewById(R.id.item_status);
                holder.itemValue = (CustomFontTextView) convertView.findViewById(R.id.value);

                convertView.setTag(holder);
            }

            final ViewHolder finalHolder = holder;
            holder.clickMe.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finalHolder.itemStatus.toggle();
                }
            });

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        DecimalFormat df = new DecimalFormat();
        df.setMaximumFractionDigits(2);

        if (item != null) {
            if (item.getMyType() == MileageReceiptInterface.I_AM_MILEAGE) {
                final Mileage mileage = (Mileage) item;
                holder.itemDate.setText(mileage.getStartDate());
                holder.itemValue.setText(df.format(mileage.getDistance() * mileage.getPrice()) + " " + mileage.getCurrency());
            } else {
                final Receipt receipt = (Receipt) item;
                holder.itemDate.setText(receipt.getDate());
                holder.itemValue.setText(df.format(receipt.getValue()) + " " + receipt.getCurrency());
            }
        }


        return convertView;
    }

    private class ViewHolder {
        LinearLayout clickMe;
        CheckBox itemStatus;
        CustomFontTextView itemDate;
        CustomFontTextView itemValue;
    }


}

这里我将我的适配器链接到我的列表:

    final MergeAdapter all_adapter = new MergeAdapter();

        ArrayList<Folder> folders = new FolderDataSource().selectAllFolders();
        for (Folder folder : folders) {
            final TreeMap<Long, ArrayList<MileageReceiptInterface>> sortedFolderItems = new ReceiptDataSource().loadUnsyncReceipts(folder.getId());
            if (!sortedFolderItems.isEmpty()) {
                final View header1 = inflater.inflate(R.layout.receipt_list_header, null);
                assert header1 != null;
                CustomFontTextView receipt_header_txt = (CustomFontTextView) header1.findViewById(R.id.receipt_header_txt);
                receipt_header_txt.setText(folder.getTitle());
                all_adapter.addView(header1);

                for (long receiptTimeStamp : sortedFolderItems.descendingKeySet()) {
                    SyncItemAdapter folder_stuff = new SyncItemAdapter(mContext, sortedFolderItems.get(receiptTimeStamp));
                    all_adapter.addAdapter(folder_stuff);
                }
            }
        }

        folder_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(mContext, "You clicked an item", Toast.LENGTH_SHORT).show();
            }
        });

然而,当我点击列表中的任何项目时,我似乎无法正确捕捉项目点击事件,因为没有吐司...

任何想法?

已编辑:对我的适配器类别进行了小规模更改,现在它的工作非常精细

2 个答案:

答案 0 :(得分:1)

尝试在xml中的复选框上添加clickable = false,focusable = false。点击列表项后你应该得到祝酒词。在onItemClick(...)方法中执行view.findViewById(CheckboxId).toggle();

我最近没有测试过,但它应该可以工作:)

答案 1 :(得分:0)

将以下属性添加到列表项的根布局中。这将阻止项目中复选框上的事件:

android:descendantFocusability="blocksDescendants"