多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > Android ListView 分分鐘實現Item單選、多選效果

Android ListView 分分鐘實現Item單選、多選效果

來源:程序員人生   發布時間:2017-03-10 09:41:18 閱讀次數:9221次

ListView中有1個屬性:android:choiceMode,對應3個可選值:

  • singleChoice 單選
  • multipleChoice 多選
  • none 默許情況,沒有選中效果

在ListView的布局中設置了android:choiceMode屬性后,item布局需要實現checkable,才有選中效果。

那末我們先來看1下這個checkable接口:

/**
 * Defines an extension for views that make them checkable.
 *
 */
public interface Checkable {

    /**
     * Change the checked state of the view
     * 
     * @param checked The new checked state
     */
    void setChecked(boolean checked);

    /**
     * @return The current checked state of the view
     */
    boolean isChecked();

    /**
     * Change the checked state of the view to the inverse of its current state
     *
     */
    void toggle();
}

接口很簡單,就3個方法:

  • setChecked(boolean checked) 設置是不是選中。當我們點擊item的時候,會調用這個方法。
  • boolean isChecked() 判斷是不是選中。
  • toggle() 開關,如果當前是選中的狀態,調用該方法后取消選中,反之,選中。

實現單選效果:

1、 ListView布局中android:choiceMode設置為singleChoice。
2、選取實現了checkable接口的View或ViewGroup作為item布局控件。

  • 當item展現的數據比較簡單,例如就是1段文本,item布局可以直接使用系統自帶的CheckedTextView控件,該控件有1個屬性:android:checkMark=”?android:listChoiceIndicatorSingle”為單選樣式;“?android:listChoiceIndicatorMultiple”為多選樣式。若要修改顯示的樣式,可以自己寫1個selector,然后checkMark指定為這個selector。例如:

    在drawable文件夾下面創建1個ic_hideable_item.xml文件。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:drawable="@mipmap/ic_hideable_item_unchecked" />
    <item android:drawable="@mipmap/ic_hideable_item_checked" />
</selector>

checkMark指定為上面的那個xml文件:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tv_single_choice"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeightSmall"
    android:textSize="14sp"
    android:gravity="center_vertical"
    android:checkMark="@drawable/ic_hideable_item"
    android:paddingLeft="16dp"
    android:paddingRight="16dp">
</CheckedTextView>

實現多選效果:

1、 ListView布局中android:choiceMode設置為multipleChoice。
2、選取實現了checkable接口的View或ViewGroup作為item布局控件。
這里筆者自定義1個控件實現Checkable接口。代碼以下:

public class CheckableLayout extends RelativeLayout implements Checkable {

    private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked};

    private boolean mChecked;

    public CheckableLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    public void setChecked(boolean b) {

        if (b != mChecked){
            mChecked = b;
            refreshDrawableState();
        }
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }

    @Override
    public void toggle() {

        setChecked(!mChecked);
    }


    @Override
    protected int[] onCreateDrawableState(int extraSpace) {

        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);

        if (isChecked()) mergeDrawableStates(drawableState, CHECKED_STATE_SET);

        return drawableState;
    }
}

利用到item布局:

<com.jm.customchoicelist.CheckableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">


    <TextView
        android:id="@+id/tv_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:duplicateParentState="true"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="30dp"
        android:textColor="@color/hideable_text_color"
        tools:text="測試數據"/>


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:duplicateParentState="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="20dp"
        android:src="@drawable/ic_hideable_item"/>

</com.jm.customchoicelist.CheckableLayout>

注意到上面TextView、ImageView控件中的android:duplicateParentState屬性,
該屬性表示當前控件是不是跟隨父控件的狀態(點擊、焦點等)。若將TextView的該屬性置為false,則文字無變色效果;若將ImageView的該屬性置為false,則無選中效果。

最后怎樣獲得選中item對應的位置呢?

  • 單選—> 通過ListView的getCheckedItemPosition()獲得選中的位置。
  • 多選—> 通過ListView的getCheckedItemPositions()得到1個SparseBooleanArray,key為position,value為是不是選中。
mSingleListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                int checkedItemPosition = mSingleListView.getCheckedItemPosition();
                Toast.makeText(MainActivity.this, "you chose item " + checkedItemPosition, Toast.LENGTH_SHORT).show();
            }
        });


        mMultipleListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SparseBooleanArray checkedItemPositions = mMultipleListView.getCheckedItemPositions();
                boolean isChecked = checkedItemPositions.get(position);
                Toast.makeText(MainActivity.this, "item " + position + " isChecked=" + isChecked, Toast.LENGTH_SHORT).show();
            }
        });

源碼傳送門

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 亚洲免费福利视频 | 欧洲性大片xxxxx久久久 | 国产成人高清视频 | 色久阁 | 国产精品久久视频 | 欧美艳星xxx| 亚洲精彩视频在线观看 | 亚洲欧美色一区二区三区 | 一级a毛片免费 | 亚洲精品一区二区三区不卡 | 日本护士18 | 国产精品99一区二区三区 | 四色网站 | 中文字幕一区二区三区免费看 | 亚洲高清在线天堂精品 | 亚洲欧美另类在线视频 | 中文字幕成人免费高清在线 | 日韩小视频在线播放 | 久久成人小视频 | www午夜| 亚洲欧美日韩另类 | 国产精品亚洲精品观看不卡 | 久久天天躁夜夜躁狠狠躁2020 | 国内精品久久久久影院中国 | 一二三四日本手机高清视频 | 国产日韩在线观看视频 | 欧美偷拍自拍视频 | 尤物网站永久在线观看 | 日本不卡一区二区三区视频 | 亚洲精品第四页中文字幕 | www视频网站 | 乌克兰鲜嫩xxxx | 最近更新中文字幕3 | 精品久久久久久无码中文字幕 | 亚洲成人福利在线观看 | 日本免费网站视频www区 | 欧美日韩午夜精品不卡综合 | 被公侵犯肉体中文字幕一区二区 | 伊人婷婷色| 国产精品久久久久一区二区 | 2021年中文字幕视频 |