欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程之listView中checkbox用法實例分析

 更新時間:2016年01月13日 09:20:50   作者:chenguang79  
這篇文章主要介紹了Android編程之listView中checkbox用法,結(jié)合實例形式分析了Android中checkbox的頁面布局及功能實現(xiàn)相關(guān)技巧,需要的朋友可以參考下

本文實例講述了Android編程之listView中checkbox用法。分享給大家供大家參考,具體如下:

我們經(jīng)常會用到在listView中使用checkbox的情況。直接不回應(yīng)用后會發(fā)現(xiàn),ListView中的OnItemClickListener事件會和checkbox中的選擇事件發(fā)生沖突,這個怎么處理呢。直接上代碼。

list_item.xml代碼:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@color/color_while">
  <TextView
    android:id="@+id/txt_add_note_tag_list_name"
    android:layout_height="50dp"
    android:layout_width="fill_parent"
    android:gravity="center_vertical"
    android:textColor="@color/color_black"
    android:layout_marginLeft="8dp"
    />
  <CheckBox
    android:id="@+id/chk_add_note_tag_list_chk"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:layout_alignParentRight="true"
    android:layout_marginRight="8dp"
    android:focusable="false"   <!--這個是必須加上的,不然會出現(xiàn)沖突-->
    android:clickable="false"   <!--這個是必須加上的,不然會出現(xiàn)沖突-->
    />
</RelativeLayout>

BaseAdapter.java代碼:

package cg.guangda.androidnote;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cg.guangda.androidnote.Model.noteTag;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
public class Add_note_tag_list_BaseAdapter extends BaseAdapter {
  private LayoutInflater inflater;
  private List<noteTag> list_notetag_date;
  //定義多選框是否被選中
  public static Map<Integer, Boolean> isSelected;
  public Add_note_tag_list_BaseAdapter(Context context,List<noteTag> list_notetag_date)
  {
    this.inflater = LayoutInflater.from(context);
    this.list_notetag_date = list_notetag_date;
    //這兒定義isSelected這個map是記錄每個listitem的狀態(tài),初始狀態(tài)全部為false。
    isSelected = new HashMap<Integer, Boolean>();
    for (int i = 0; i < list_notetag_date.size(); i++) {
      isSelected.put(i, false);
    }
  }
  @Override
  public int getCount() {
    // TODO Auto-generated method stub
    return list_notetag_date.size();
  }
  @Override
  public Object getItem(int position) {
    // TODO Auto-generated method stub
    return list_notetag_date.get(position);
  }
  @Override
  public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    add_note_noteTag notetag = null;
    if(convertView==null)
    {
      convertView = inflater.inflate(R.layout.add_note_tag_list_item, null);
      notetag = new add_note_noteTag();
      notetag.txt_add_note_tag_list_name = (TextView)convertView.findViewById(R.id.txt_add_note_tag_list_name);
      notetag.chk_add_note_tag_list_chk = (CheckBox)convertView.findViewById(R.id.chk_add_note_tag_list_chk);
      convertView.setTag(notetag);
    }
    else {
      notetag = (add_note_noteTag)convertView.getTag();
    }
    notetag.txt_add_note_tag_list_name.setText(list_notetag_date.get(position).get_tagName());
    notetag.chk_add_note_tag_list_chk.setChecked(isSelected.get(position));
    return convertView;
  }
  public class add_note_noteTag
  {
    TextView txt_add_note_tag_list_name;
    CheckBox chk_add_note_tag_list_chk;
  }
}

應(yīng)用頁面:

list_popwin_note_tag.setAdapter(new Add_note_tag_list_BaseAdapter(this, list_noteTag_date));
list_popwin_note_tag.setOnItemClickListener(new noteTagListItemOnClickListener());

/**
* 點擊列表項事件
* @author cg
*
*/
class noteTagListItemOnClickListener implements OnItemClickListener{
    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int position,
        long arg3) {
      // TODO Auto-generated method stub
      add_note_noteTag vHollder = (add_note_noteTag) view.getTag();
      //在每次獲取點擊的item時將對于的checkbox狀態(tài)改變,同時修改map的值。
      vHollder.chk_add_note_tag_list_chk.setChecked(vHollder.chk_add_note_tag_list_chk.isChecked()==true ? false : true);
      boolean check = vHollder.chk_add_note_tag_list_chk.isChecked();
      Add_note_tag_list_BaseAdapter.isSelected.put(position, check);
      Log.v("noteTagListItemOnClickListener", list_noteTag_date.get(position).get_tagName() + check);
    }
}

希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

  • Android編程之創(chuàng)建自己的內(nèi)容提供器實現(xiàn)方法

    Android編程之創(chuàng)建自己的內(nèi)容提供器實現(xiàn)方法

    這篇文章主要介紹了Android編程之創(chuàng)建自己的內(nèi)容提供器實現(xiàn)方法,結(jié)合具體實例形式分析了Android創(chuàng)建內(nèi)容提供器的原理、步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08
  • Android Toast使用的簡單小結(jié)(推薦)

    Android Toast使用的簡單小結(jié)(推薦)

    這篇文章主要介紹了Android Toast使用的簡單小結(jié),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • ListView下拉列表控件使用方法詳解

    ListView下拉列表控件使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了ListView下拉列表控件的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Android 安全加密:Https編程詳解

    Android 安全加密:Https編程詳解

    本文主要介紹Android安全加密Https編程的知識,這里整理了詳細(xì)的資料及說明解決方案和驗證,有興趣的小伙伴可以參考下
    2016-09-09
  • Android 程序應(yīng)用的生命周期

    Android 程序應(yīng)用的生命周期

    本篇文章小編為大家介紹,Android 程序應(yīng)用的生命周期。需要的朋友參考下
    2013-04-04
  • Flutter路由框架Fluro使用簡介

    Flutter路由框架Fluro使用簡介

    這篇文章主要介紹了Flutter路由框架Fluro使用簡介,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • 讓Android應(yīng)用不被殺死(killer)的方法

    讓Android應(yīng)用不被殺死(killer)的方法

    這篇文章主要介紹了讓Android應(yīng)用不被殺死(killer)的方法,本文講解了實現(xiàn)方法和原理分析,需要的朋友可以參考下
    2015-04-04
  • Android studio設(shè)置指定的簽名文件教程

    Android studio設(shè)置指定的簽名文件教程

    這篇文章主要介紹了Android studio設(shè)置指定的簽名文件教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • 一分鐘快速定位Android啟動耗時問題

    一分鐘快速定位Android啟動耗時問題

    做開發(fā)除了實現(xiàn)功能,還要注重優(yōu)化,性能優(yōu)化包括的東西還是非常多的,下面這篇文章主要給大家介紹了關(guān)于如何通過一分鐘快速定位Android啟動耗時問題的相關(guān)資料,需要的朋友可以參考下
    2021-07-07
  • Gradle 依賴切換源碼實踐示例詳解

    Gradle 依賴切換源碼實踐示例詳解

    這篇文章主要為大家介紹了Gradle 依賴切換源碼實踐示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12

最新評論