前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >笔记22 | 学习整理开源APP(BaseAnimation)程序源码“中的通讯录效果(三)

笔记22 | 学习整理开源APP(BaseAnimation)程序源码“中的通讯录效果(三)

作者头像
项勇
发布2018-06-19 15:19:11
6610
发布2018-06-19 15:19:11
举报
文章被收录于专栏:项勇

1.前言

整理学习”Android动画效果集合开源APP(BaseAnimation)程序源码“中的通讯录效果。 前人栽树:duguang 博客地址:http://blog.csdn.net/duguang77 下载地址:http://download.csdn.net/download/u011112840/6910683 后人乘凉:http://blog.csdn.net/xiangyong_1521/article/details/78273838


2.实现


3.目录

  • 3.1 A-Z的字母索引
  • 3.2 联系人界面ListView的数据填充
  • 3.3 联系人的搜索
3.1 A-Z的字母索引

通过自定义一个View界面,绘制一个A-Z竖向排列的布局,通过触摸事件监听,根据触摸的区域和字母高度的计算出position,再向联系人Listview提供一个方向输出position值!

>笔记20 | 学习整理开源APP(BaseAnimation)程序源码“中的通讯录效果(一)


3.2 通讯录界面ListView的数据填充

加载联系人的方法容易理解,排序》加载》处理

>笔记21 | 学习整理开源APP(BaseAnimation)程序源码“中的通讯录效果(二)


3.3 联系人的搜索

搜索部分难点是拿出搜索的内容,首先自定义了ClearEditText,给出了一个textchange的监听,然后进行匹配>筛选>排序>加载到联系人列表中。

  • xml
代码语言:javascript
复制
<com.example.book_mediarecorder.ClearEditText
        android:id="@+id/filter_edit"
        android:layout_marginTop="5dip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/sorlistview_search_bar_edit_selector"
        android:drawableLeft="@drawable/sorlistview_search_bar_icon_normal"
        android:hint="请输入关键字"
        android:singleLine="true"
        android:textSize="15.0dip" />
  • MainActivity
代码语言:javascript
复制
mClearEditText = (ClearEditText) findViewById(R.id.filter_edit);
        //根据输入框输入值的改变来过滤搜索
        mClearEditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //当输入框里面的值为空,更新为原来的列表,否则为过滤数据列表
                filterData(s.toString());
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

         /**
     * 根据输入框中的值来过滤数据并更新ListView
     * @param filterStr
     */
    private void filterData(String filterStr){
        List<SortModel> filterDateList = new ArrayList<SortModel>();

        if(TextUtils.isEmpty(filterStr)){
            filterDateList = SourceDateList;
        }else{
            filterDateList.clear();
            for(SortModel sortModel : SourceDateList){
                String name = sortModel.getName();
                if(name.indexOf(filterStr.toString()) != -1 || characterParser.getSelling(name).startsWith(filterStr.toString())){
                    filterDateList.add(sortModel);
                }
            }
        }
        // 根据a-z进行排序
        Collections.sort(filterDateList, pinyinComparator);
        adapter.updateListView(filterDateList);
    }
  • 自定义EditText
代码语言:javascript
复制
public class ClearEditText extends EditText implements  
        OnFocusChangeListener, TextWatcher { 
    /**
     * 删除按钮的引用
     */
    private Drawable mClearDrawable; 

    public ClearEditText(Context context) { 
        this(context, null); 
    } 

    public ClearEditText(Context context, AttributeSet attrs) { 
        //这里构造方法也很重要,不加这个很多属性不能再XML里面定义
        this(context, attrs, android.R.attr.editTextStyle); 
    } 

    public ClearEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }


    private void init() { 
        //获取EditText的DrawableRight,假如没有设置我们就使用默认的图片
        mClearDrawable = getCompoundDrawables()[2]; 
        if (mClearDrawable == null) { 
            mClearDrawable = getResources() 
                    .getDrawable(R.drawable.sorlistview_emotionstore_progresscancelbtn); 
        } 
        mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight()); 
        setClearIconVisible(false); 
        setOnFocusChangeListener(this); //注册一个回调,以便在该视图的焦点发生改变时调用。
        addTextChangedListener(this); 
    } 

    /**
     * 因为我们不能直接给EditText设置点击事件,所以我们用记住我们按下的位置来模拟点击事件
     * 当我们按下的位置 在  EditText的宽度 - 图标到控件右边的间距 - 图标的宽度  和
     * EditText的宽度 - 图标到控件右边的间距之间我们就算点击了图标,竖直方向没有考虑
     */
    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
        if (getCompoundDrawables()[2] != null) { 
            if (event.getAction() == MotionEvent.ACTION_UP) { 
                boolean touchable = event.getX() > (getWidth() 
                        - getPaddingRight() - mClearDrawable.getIntrinsicWidth()) 
                        && (event.getX() < ((getWidth() - getPaddingRight())));
                if (touchable) { 
                    this.setText(""); 
                } 
            } 
        } 
        return super.onTouchEvent(event); 
    } 

    /**
     * 当ClearEditText焦点发生变化的时候,判断里面字符串长度设置清除图标的显示与隐藏
     */
    @Override 
    public void onFocusChange(View v, boolean hasFocus) { 
        if (hasFocus) { 
            setClearIconVisible(getText().length() > 0); 
        } else { 
            setClearIconVisible(false); 
        } 
    } 

    /**
     * 设置清除图标的显示与隐藏,调用setCompoundDrawables为EditText绘制上去
     * @param visible
     */
    protected void setClearIconVisible(boolean visible) { 
        Drawable right = visible ? mClearDrawable : null; 
        setCompoundDrawables(getCompoundDrawables()[0], 
                getCompoundDrawables()[1], right, getCompoundDrawables()[3]); 
    } 

    /**
     * 当输入框里面内容发生变化的时候回调的方法
     */
    @Override 
    public void onTextChanged(CharSequence s, int start, int count, 
            int after) { 
        setClearIconVisible(s.length() > 0); 
    } 

    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, 
            int after) {    
    } 

    @Override 
    public void afterTextChanged(Editable s) { 
    } 
}

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-10-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 项勇 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.前言
  • 2.实现
  • 3.目录
    • 3.1 A-Z的字母索引
      • 3.2 通讯录界面ListView的数据填充
        • 3.3 联系人的搜索
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档