zoukankan      html  css  js  c++  java
  • Android的Master/Detail风格界面中实现自定义ListView的单选

    原文在这里:http://duduli.iteye.com/blog/1453576

    可以实现多选,那么如何实现单选呢,这里我写了一个非常简单的方法:

        public void onListItemClick(ListView listView, View view, int position, long id) {
            super.onListItemClick(listView, view, position, id);
            CheckedTextView ct;
            CheckedTextView multiple;
            int count = listView.getChildCount();
            for (int i = 0 ; i < count ; i ++ ) {
                ct = (CheckedTextView)listView.getChildAt(i).findViewById(R.id.checkText);
                ct.setChecked(false);
            }
            multiple = (CheckedTextView)view.findViewById(R.id.checkText);
            multiple.toggle();
        }

    当然,你需要在你自定义的Adapter的getView()里面

    holder.checkedTextView = (CheckedTextView)convertView.findViewById((R.id.checkText));

    这里是我定义的ListView中每个View的xml布局

    <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            xmlns:android="http://schemas.android.com/apk/res/android">
    
        <ImageView
                android:layout_width="wrap_content"
                android:padding="0dp"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:background="@drawable/navigation_category_icon"
                android:id="@+id/imageView"
                android:layout_gravity="center_horizontal|top"/>
    
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="Large Text"
                android:id="@+id/textView"
                android:layout_gravity="center_horizontal|top"
                android:layout_toRightOf="@+id/imageView"
                android:layout_marginBottom="11dp"/>
        <CheckedTextView
                android:id="@+id/checkText"
                android:layout_width="fill_parent"
                android:layout_height="?android:attr/listPreferredItemHeight"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:gravity="center_vertical"
                android:checkMark="?android:attr/listChoiceIndicatorMultiple"
                android:paddingLeft="6dip"
                android:paddingRight="6dip"
                />
    </RelativeLayout>

    这里是效果图


  • 相关阅读:
    【剑指offer】和为S的连续整数序列
    【剑指offer】连续子数组最大和
    【剑指offer】从尾到头打印链表
    【Spark】概述
    【剑指offer】题目20 顺时针打印矩阵
    【剑指offer】题目36 数组中的逆序对
    【C语言】二维数组做形参
    【剑指offer】题目38 数字在排序数组中出现的次数
    SAP 锁对象 基本概念与基本操作 SE11
    线程特定数据TSD总结
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3190096.html
Copyright © 2011-2022 走看看