ListView with BaseAdapter和LinearLayout?

时间:2015-04-27 18:57:41

标签: android android-layout listview android-listview

我正在尝试为ListView创建一个BaseAdapter。问题是当我在其他LinearLayout中创建一个LinearLayout时,监听器OnItemClickListener不起作用。如果我将组件放在LinearLayout之外工作正常。

我怎么能这样做?

的ListView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/lvEntregasPendente"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        ></ListView>
</LinearLayout>

适配器XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffe3b3"
    android:layout_margin="5dp"
    android:padding="2dp"
    android:id="@+id/llEntregaPendenteVendas">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tvVenda"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Venda"
                android:textColor="@color/action_bar"
                android:textStyle="bold"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Entrega em: "
                android:textColor="@color/action_bar"
                android:textStyle="bold"
                android:layout_marginLeft="10dp"
                />

            <TextView
                android:id="@+id/tvDataEntrega"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Data"
                android:textColor="@color/action_bar"
                android:textStyle="bold"
                android:layout_weight="1"
                />

            <CheckBox
                android:id="@+id/cbEntregue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/action_bar"
                android:text="Entregue"
                />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tvAtrasoEntrega"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Atraso de: 20 dias"
                android:textColor="#FF0000"
                android:padding="5dp"
                android:visibility="visible"
                android:layout_weight="1"
                android:gravity="right"/>

        </LinearLayout>
    </LinearLayout>

</LinearLayout>

适配器

public class EntregaPendenteListAdapter extends BaseAdapter {
    private Context context;
    private List<Venda> lista;
    private DateControl dateControl;
    private EntregaPendenteFrag rpf;
    private Venda venda;


    public EntregaPendenteListAdapter(Context context, List<Venda> lista, EntregaPendenteFrag rpf) {
        this.context = context;
        this.lista = lista;
        this.rpf = rpf;
        dateControl = new DateControl();
    }

    /** limpa a lista */
    public void clearList(){
        lista.clear();
        notifyDataSetChanged();
    }

    /** altera lista */
    public void changeList(List<Venda> lista){
        this.lista = lista;
        notifyDataSetChanged();
    }

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

    @Override
    public Object getItem(int position) {
        return lista.get(position);
    }

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



    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        Venda venda = lista.get(position);
        if (convertView == null) {
            holder = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.entregas_pendente_adapter, parent, false);

            holder.llEntregaPendenteVendas = (LinearLayout) convertView.findViewById(R.id.llEntregaPendenteVendas);
            holder.tvVenda = (TextView) convertView.findViewById(R.id.tvVenda);
            holder.tvDataEntrega = (TextView) convertView.findViewById(R.id.tvDataEntrega);
            holder.tvAtrasoEntrega = (TextView) convertView.findViewById(R.id.tvAtrasoEntrega);
            holder.cbEntregue = (CheckBox)convertView.findViewById(R.id.cbEntregue);

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

        holder.tvVenda.setText("Venda: " + FormataCodigo.getCodFormat(venda.getId()));


        if(venda.getData_entrega() != null){
            holder.tvDataEntrega.setText(new SimpleDateFormat("dd-MM-yyyy").format(venda.getData_entrega()));
            if(dateControl.getDiasVencido(venda.getData_entrega()) > 0){
                holder.tvAtrasoEntrega.setText("Atraso de: " + new DateControl().getDiasVencido(venda.getData_entrega()) + "dias");
                holder.tvAtrasoEntrega.setVisibility(View.VISIBLE);
            }
        }

        if((position % 2) == 0){
            holder.llEntregaPendenteVendas.setBackgroundColor(Color.parseColor("#ffe3b3"));
        }else{
            holder.llEntregaPendenteVendas.setBackgroundColor(Color.WHITE);
        }


        return convertView;
    }


    private static class ViewHolder{
        LinearLayout llEntregaPendenteVendas;
        TextView tvVenda;
        TextView tvDataEntrega;
        TextView tvAtrasoEntrega;
        CheckBox cbEntregue;
    }
}

活动

//listview
lvEntregasPendente = (ListView)view.findViewById(R.id.lvEntregasPendente);
lvEntregasPendente.setOnItemClickListener(this);

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i("ITEM->", position + "");
    }

1 个答案:

答案 0 :(得分:2)

似乎你的CheckBox正在窃取焦点。 尝试在其上设置这些属性:

android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"