package com.yokeyword.sample.city;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import me.yokeyword.indexablelistview.IndexEntity;
import me.yokeyword.indexablelistview.IndexHeaderEntity;
import me.yokeyword.indexablelistview.IndexableStickyListView;

import com.yokeyword.sample.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 选择城市
 * Created by YoKeyword on 2016/3/20.
 */
public class PickCityActivity extends AppCompatActivity {
    public static final String TAG = PickCityActivity.class.getSimpleName();

    private static final String EXTRA_LARGE = "extra_large";

    private IndexableStickyListView mIndexableStickyListView;
    private SearchView mSearchView;

    private CityAdapter mAdapter;
    private List<CityEntity> mCities = new ArrayList<>();

    private String[] mHotCities = new String[]{"杭州市", "北京市", "上海市", "广州市"};

    public static Intent getCallingIntent(Context context, boolean isLarge) {
        Intent intent = new Intent(context, PickCityActivity.class);
        intent.putExtra(EXTRA_LARGE, isLarge);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pick_city);

        initView();
    }

    protected void initView() {
        setTitle("选择城市");
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        mIndexableStickyListView = (IndexableStickyListView) findViewById(R.id.indexListView);
        mSearchView = (SearchView) findViewById(R.id.searchview);

        mAdapter = new CityAdapter(this);
        mIndexableStickyListView.setAdapter(mAdapter);

        // 初始化数据
        List<String> cityStrings = Arrays.asList(getResources().getStringArray(R.array.city_array));
        for (String item : cityStrings) {
            CityEntity cityEntity = new CityEntity();
            cityEntity.setName(item);
            mCities.add(cityEntity);
        }

        if (getIntent().getBooleanExtra(EXTRA_LARGE, false)) {
            mCities.addAll(mCities);
            mCities.addAll(mCities);
            mCities.addAll(mCities);
            mCities.addAll(mCities);
            mCities.addAll(mCities);
            mCities.addAll(mCities);
            mCities.addAll(mCities);
        }

        // 添加定位城市Header
        ArrayList<CityEntity> gpsIndexEntityList = new ArrayList<>();
        final CityEntity gpsEntity = new CityEntity();
        gpsEntity.setName("定位中...");
        gpsIndexEntityList.add(gpsEntity);
        IndexHeaderEntity<CityEntity> gpsHeader = new IndexHeaderEntity<>("定", "GPS自动定位", gpsIndexEntityList);

        // 添加热门城市Header
        IndexHeaderEntity<CityEntity> hotHeader = new IndexHeaderEntity<>();
        ArrayList<CityEntity> hotIndexEntityList = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            CityEntity hotEntity = new CityEntity();
            hotEntity.setName(mHotCities[i]);
            hotIndexEntityList.add(hotEntity);
        }

        hotHeader.setHeaderTitle("热门城市");
        hotHeader.setIndex("热");
        hotHeader.setHeaderList(hotIndexEntityList);

        // 绑定数据
        mIndexableStickyListView.bindDatas(mCities, gpsHeader, hotHeader);

        // 搜索
        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                // 委托处理搜索
                mIndexableStickyListView.searchTextChange(newText);
                return true;
            }
        });
        mIndexableStickyListView.setOnItemContentClickListener(new IndexableStickyListView.OnItemContentClickListener() {
            @Override
            public void onItemClick(View v, IndexEntity indexEntity) {
                CityEntity cityEntity = (CityEntity) indexEntity;
                Toast.makeText(PickCityActivity.this, "选择了" + cityEntity.getName(), Toast.LENGTH_SHORT).show();
            }
        });

        mIndexableStickyListView.setOnItemTitleClickListener(new IndexableStickyListView.OnItemTitleClickListener() {
            @Override
            public void onItemClick(View v, String title) {
                Toast.makeText(PickCityActivity.this, "点击了" + title, Toast.LENGTH_SHORT).show();
            }
        });

        // 模拟定位
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                gpsEntity.setName("杭州市");
                mAdapter.notifyDataSetChanged();
            }
        }, 3000);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}

@@ -2017,244 +2017,31 @@
     
-List%3CString%3E cityStrings = Arrays.asList(getResources().getStringArray(R.array.city_array));%0A        for (String item : cityStrings) %7B%0A            CityEntity cityEntity = new CityEntity();%0A            cityEntity.setName(item);%0A 
+initCities();%0A%0A
         
    m
@@ -2040,46 +2040,18 @@
     
-   mCities.add(cityEntity);%0A        %7D%0A
+// 128%E5%80%8D%E5%A4%A7%E6%95%B0%E6%8D%AE
 %0A   
@@ -2122,38 +2122,27 @@
         
-mCities.addAll(m
+init
 Cities
+(
 );%0A     
@@ -2148,38 +2148,27 @@
         
-mCities.addAll(m
+init
 Cities
+(
 );%0A     
@@ -2174,38 +2174,27 @@
         
-mCities.addAll(m
+init
 Cities
+(
 );%0A     
@@ -2200,38 +2200,27 @@
         
-mCities.addAll(m
+init
 Cities
+(
 );%0A     
@@ -2226,38 +2226,27 @@
         
-mCities.addAll(m
+init
 Cities
+(
 );%0A     
@@ -2252,38 +2252,27 @@
         
-mCities.addAll(m
+init
 Cities
+(
 );%0A     
@@ -2282,30 +2282,19 @@
     
-mCities.addAll(m
+init
 Cities
+(
 );%0A 
@@ -4676,32 +4676,353 @@
 , 3000);%0A    %7D%0A%0A
+    private void initCities() %7B%0A        List%3CString%3E cityStrings = Arrays.asList(getResources().getStringArray(R.array.city_array));%0A        for (String item : cityStrings) %7B%0A            CityEntity cityEntity = new CityEntity();%0A            cityEntity.setName(item);%0A            mCities.add(cityEntity);%0A        %7D%0A    %7D%0A%0A
     @Override%0A  

添加多音字的处理demo (128倍大数据)