Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Day23-AutoCompleteTextView和Scroll

Day23-AutoCompleteTextView和Scroll

編輯:關於Android編程

最簡單的使用

xml布局文件

    
    

Activity.java

List list = new ArrayList<>();
list.add("北京");
list.add("上海");
list.add("廣東");
list.add("上海");
list.add("上海");
list.add("上海");
list.add("山東");
list.add("山東");
list.add("山東");
ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
AutoCompleteTextView auto= (AutoCompleteTextView) findViewById(R.id.main_auto);
auto.setAdapter(adapter);

自定義拼音, 自動補全

布局文件還是使用上面的

寫個實體類, 還有文字和相應的拼音

public class Entry {
    private String text;//顯示的數據
    private String pinyin;//過濾的數據

    public Entry(String text, String pinyin) {
        this.text = text;
        this.pinyin = pinyin;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getPinyin() {
        return pinyin;
    }

    public void setPinyin(String pinyin) {
        this.pinyin = pinyin;
    }

    @Override
    public String toString() {
        return text;
    }
}

AutoActivity.java

此處需要注意: public < T extends ListAdapter & Filterable > void setAdapter(T adapter)
這就說明這個適配器, 在需要ListAdapter和Filterable, 所以應該像下面這樣寫:

package com.lulu.day23;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by Lulu on 2016/8/27.
 *
 * Filterable: 過濾器
 */
public class AutoAdapter extends BaseAdapter implements Filterable {
    //所有跟畫面相關的都需要一個context
    private Context context;
    private List list;
    private List backup;
    private Filter filter;
    public AutoAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }
    @Override
    public int getCount() {
        return list.size();
    }
    //得到數據源中的一條數據
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }
    //返回當前ID
    @Override
    public long getItemId(int position) {
        return position;
    }
    //指定位置的元素該如何展示在view上
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = new TextView(context);
        }
        ((TextView) convertView).setText(list.get(position).getText());
        return convertView;
    }
    @Override
    public Filter getFilter() {
        if (filter == null) {
            filter = new AutoFilter();
       }
        return filter;
    }

    /**過濾
     * 一定要用非靜態的內部類
     */
    public class AutoFilter extends Filter{
        @Override//產生過濾結果
        protected FilterResults performFiltering(CharSequence constraint) {
            if (backup == null) {
                backup = new ArrayList<>(list);
            }
            FilterResults results = new FilterResults();
            if (!TextUtils.isEmpty(constraint)) {
                List entries = new ArrayList<>();
                for (Entry  entry: backup) {
                    String pinyin = entry.getPinyin();
                    if (pinyin.startsWith(constraint.toString())) {
                        entries.add(entry);
                    }
                }
                results.count = entries.size();
                results.values = entries;
            } else {
                results.count = 0;
                results.values = new ArrayList<>();
            }
            return results;
        }
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            list.clear();
            list.addAll((Collection) results.values);
            notifyDataSetChanged();
        }
    }


}

MainActivity.java

 List list = new ArrayList<>();
 list.add(new Entry("北京", "beijing"));
 list.add(new Entry("上海", "shanghai"));
 list.add(new Entry("廣東", "guangdong"));
 list.add(new Entry("", "shandong"));
 list.add(new Entry("上海", "shagnhai"));
 list.add(new Entry("上海", "shagnhai"));
 AutoAdapter adapter = new AutoAdapter(this, list);
 AutoCompleteTextView auto = (AutoCompleteTextView) findViewById(R.id.main_auto);
 auto.setAdapter(adapter);

使用第三方包來實現拼音自動補全

使用的第三方的名字為: com.github.stuxuhai:jpinyin:1.1.7, 需要手動引入

PinyinAdapter.java

package com.lulu.day23;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by Lulu on 2016/8/27.
 */
public class PinyinAdapter extends BaseAdapter implements Filterable {
    private Context context;
    private List list;
    private List backup;
    private Filter filter;

    public PinyinAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }
    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = new TextView(context);
    }
        ((TextView) convertView).setText(list.get(position));
        return convertView;
    }
    @Override
    public Filter getFilter() {
        if (filter == null) {
            filter = new PinyinFilter();
        }
        return filter;
    }
    private class PinyinFilter extends Filter {
        //必須要備份
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            if (backup == null) {
                backup = new ArrayList<>(list);
            }

            FilterResults results = new FilterResults();
            if (!TextUtils.isEmpty(constraint)) {
                List strings = new ArrayList<>();
                for (String str : backup) {
                    try {
                        String pinyin = PinyinHelper.convertToPinyinString(str, "", PinyinFormat.WITH_TONE_MARK);

                        if (pinyin.contains(constraint)) {
                            strings.add(str);
                        } else {
                            String shortPinyin = PinyinHelper.getShortPinyin(str);
                            if (shortPinyin.contains(constraint)) {
                                strings.add(str);
                            }
                        }
                    } catch (PinyinException e) {
                        e.printStackTrace();
                    }
                    results.count = strings.size();
                    results.values = strings;
                }
            } else {
                results.count = 0;
                results.values = new ArrayList<>();
            }
            return results;
        }
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            list.clear();
            list.addAll((Collection) results.values);
            notifyDataSetChanged();
        }
    }
}

MainActivity.java

 List list = new ArrayList<>();
 list.add("北京");
 list.add("上海");
 list.add("廣東");
 list.add("上海");
 list.add("上海");
 list.add("上海");
 list.add("山東");
 list.add("山東");
 list.add("山東");
 PinyinAdapter adapter = new PinyinAdapter(this, list);
 AutoCompleteTextView auto = (AutoCompleteTextView) findViewById(R.id.main_auto);
 auto.setAdapter(adapter);

簡單的介紹一下MultiAutoCompleteTextView的使用方法

布局文件

   

   

MainActivity.java

MultiAutoCompleteTextView multi = (MultiAutoCompleteTextView) findViewById(R.id.mian_muti);
multi.setAdapter(adapter);
MultiAutoCompleteTextView.CommaTokenizer tokenizer = new MultiAutoCompleteTextView.CommaTokenizer();
tokenizer.terminateToken(",");
multi.setTokenizer(tokenizer);

Scroll

同樣的道理, 我們一般不會使用原生的Sroll而是使用v4包中的android.support.v4.widget.NestedScrollView

垂直滾動 - 固定表頭滑動

繼承自FrameLayout, 但是只能放一個子控件, 多個會報錯
一般會放一個布局
如果有高度需求: 我們需要使用minHeight 只能給定值
scrollbars=”none: 將滾動條去掉
scrollbarStyle 在裡面還是在外面
scrollbarTrackVertical:滾動框
scrollbarThumbVertical : 進度條樣式
elevation : 海拔
有些控件默認有海拔, 所有要添加海拔高度覆蓋
對於垂直方向來說, ScrollView中的子控件高度是無效的(都是根據父控件的高低來的, 高度未知)
若想在垂直方向有長度, 可以使用minHeight

先查看它的預覽效果:

描述

xml 布局文件<喎?/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPjwvcD4NCjxwcmUgY2xhc3M9"brush:java;"> ...

MainActivity.java

public class MainActivity extends AppCompatActivity implements NestedScrollView.OnScrollChangeListener{
    private NestedScrollView scrollView;
    private TextView floating;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        scrollView = (NestedScrollView) findViewById(R.id.main_scroll);
        floating = ((TextView) findViewById(R.id.main_floating));
        scrollView.setOnScrollChangeListener(this);
        floating.setText("第一組");
    }

    @Override
    public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

        //因為在ScrollVIew中包裹了一層LinerLayout
        LinearLayout layout = (LinearLayout) v.getChildAt(0);

        //在屏幕上方外部最後一個(部分)
        TextView text1 = null;
        //在屏幕中的第一個(完全)
        TextView text2 = null;

        for (int i = 0; i < layout.getChildCount(); i++) {
            View view = layout.getChildAt(i);
            if(view.getTop() > scrollY) {
                if(view instanceof  TextView) {
                    text2 = (TextView)view;
                }
                break;
            }
            if(view instanceof TextView) {
                text1 = (TextView) view;
            }
        }
        floating.setText(text1.getText());
        if (text2 != null) {
            int offset = text2.getTop() - scrollY;
            floating.setTranslationY(
                    Math.min(offset - text2.getHeight(), 0)
            );
        } else {
            floating.setTranslationY(0);
        }
    }
}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved