diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..39fb081 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +*.iml +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures +.externalNativeBuild diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..7ac24c7 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..7158618 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1.8 + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..4869d02 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..2edb9c9 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,32 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 25 + buildToolsVersion "25.0.2" + defaultConfig { + applicationId "com.yu.indexalpha" + minSdkVersion 16 + targetSdkVersion 25 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { + exclude group: 'com.android.support', module: 'support-annotations' + }) + compile 'com.android.support:appcompat-v7:25.+' + compile 'com.android.support.constraint:constraint-layout:1.0.2' + compile 'com.android.support:recyclerview-v7:25.3.1' + compile 'com.jakewharton:butterknife:7.0.1' + testCompile 'junit:junit:4.12' +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..daa37ac --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,25 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /Users/yu/Library/Android/sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/app/src/androidTest/java/com/yu/indexalpha/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/yu/indexalpha/ExampleInstrumentedTest.java new file mode 100644 index 0000000..babe415 --- /dev/null +++ b/app/src/androidTest/java/com/yu/indexalpha/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.yu.indexalpha; + +import android.content.Context; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumentation test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() throws Exception { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getTargetContext(); + + assertEquals("com.yu.indexalpha", appContext.getPackageName()); + } +} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..42dac45 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/yu/indexalpha/MainActivity.java b/app/src/main/java/com/yu/indexalpha/MainActivity.java new file mode 100644 index 0000000..9a4cdb0 --- /dev/null +++ b/app/src/main/java/com/yu/indexalpha/MainActivity.java @@ -0,0 +1,279 @@ +package com.yu.indexalpha; + +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.text.TextUtils; +import android.view.View; +import android.widget.LinearLayout; +import android.widget.TextView; + +import com.yu.indexalpha.adapter.SaleDelegateAdapter; +import com.yu.indexalpha.model.SaleDelegateModel; +import com.yu.indexalpha.view.IndexBarView; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import butterknife.Bind; +import butterknife.ButterKnife; + +public class MainActivity extends AppCompatActivity { + @Bind(R.id.recycler_view) + RecyclerView mRecyclerView; + @Bind(R.id.ll_index) + LinearLayout indexLayout; + @Bind(R.id.tv_index) + TextView tvIndex; + @Bind(R.id.index_bar) + IndexBarView mIndexBarView; + @Bind(R.id.tv_toast) + TextView tvToast; + + private SaleDelegateAdapter mAdapter; + private LinearLayoutManager mLayoutManager; + private List mList = new ArrayList<>(); + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_sale_delegate); + ButterKnife.bind(this); + initRecycleView(); + initIndexBar(); + initData(); + initFlowIndex(); + } + + private void initRecycleView() { + mLayoutManager = new LinearLayoutManager(this); + mRecyclerView.setLayoutManager(mLayoutManager); + mAdapter = new SaleDelegateAdapter(this, mList); + mRecyclerView.setAdapter(mAdapter); + } + + private void initIndexBar() { + mIndexBarView.setSelectedIndexTextView(tvToast); + mIndexBarView.setOnIndexChangedListener(new IndexBarView.OnIndexChangedListener() { + @Override + public void onIndexChanged(String index) { + for (int i = 0; i < mList.size(); i++) { + String firstWord = mList.get(i).getFirstWord(); + if (index.equals(firstWord)) { + // 滚动列表到指定的位置 + mLayoutManager.scrollToPositionWithOffset(i, 0); + return; + } + } + } + }); + } + + private void initData() { + Map map = convertSortList(getData()); + mList.clear(); + mList.addAll((List) map.get("sortList")); + mAdapter.notifyDataSetChanged(); + } + + private void initFlowIndex() { + mRecyclerView.addOnScrollListener(new MScrollListener()); + //设置首项的索引字母 + if (mList.size() > 0) { + tvIndex.setText(mList.get(0).getFirstWord()); + indexLayout.setVisibility(View.VISIBLE); + } + } + + private class MScrollListener extends RecyclerView.OnScrollListener { + private int mFlowHeight; + private int mCurrentPosition = -1; + + @Override + public void onScrollStateChanged(RecyclerView recyclerView, int newState) { + mFlowHeight = indexLayout.getMeasuredHeight(); + } + + @Override + public void onScrolled(RecyclerView recyclerView, int dx, int dy) { + int firstVisibleItemPosition = mLayoutManager.findFirstVisibleItemPosition(); + View view = mLayoutManager.findViewByPosition(firstVisibleItemPosition + 1); + + if (view != null) { + if (view.getTop() <= mFlowHeight && isItem(firstVisibleItemPosition + 1)) { + indexLayout.setY(view.getTop() - mFlowHeight); + } else { + indexLayout.setY(0); + } + } + + if (mCurrentPosition != firstVisibleItemPosition) { + mCurrentPosition = firstVisibleItemPosition; + tvIndex.setText(mList.get(mCurrentPosition).getFirstWord()); + } + mIndexBarView.setTouchIndex(mList.get(mCurrentPosition).getFirstWord()); + } + + /** + * @param position 对应项的下标 + * @return 是否为标签项 + */ + private boolean isItem(int position) { + return mAdapter.getItemViewType(position) == SaleDelegateAdapter.VIEW_INDEX; + } + } + + /** + * 按首字母将数据排序 + * + * @param list 需要排序的数组 + * @return 返回按首字母排序的集合(集合中插入标签项),及所有出现的首字母数组 + */ + public Map convertSortList(List list) { + HashMap> map = new HashMap<>(); + for (SaleDelegateModel item : list) { + String firstWord; + if (TextUtils.isEmpty(item.getFirstWord())) { + firstWord = "#"; + } else { + firstWord = item.getFirstWord().toUpperCase(); + } + if (map.containsKey(firstWord)) { + map.get(firstWord).add(item); + } else { + List mList = new ArrayList<>(); + mList.add(item); + map.put(firstWord, mList); + } + } + + Object[] keys = map.keySet().toArray(); + Arrays.sort(keys); + List sortList = new ArrayList<>(); + + for (Object key : keys) { + SaleDelegateModel t = getIndexItem(key.toString()); + sortList.add(t); + sortList.addAll(map.get(key.toString())); + } + + HashMap resultMap = new HashMap<>(); + resultMap.put("sortList", sortList); + resultMap.put("keys", keys); + return resultMap; + } + + private SaleDelegateModel getIndexItem(String firstWord) { + SaleDelegateModel entity = new SaleDelegateModel(); + entity.setFirstWord(firstWord); + entity.setIndex(true); + return entity; + } + + private List getData() { + List list = new ArrayList<>(); + list.add(new SaleDelegateModel("伽利略", "J")); + list.add(new SaleDelegateModel("为啥子", "W")); + list.add(new SaleDelegateModel("张三", "Z")); + list.add(new SaleDelegateModel("阿玛尼", "A")); + list.add(new SaleDelegateModel("卡卡罗特", "K")); + list.add(new SaleDelegateModel("乔布斯", "Q")); + list.add(new SaleDelegateModel("奥林匹克", "A")); + list.add(new SaleDelegateModel("马上发", "M")); + list.add(new SaleDelegateModel("爱丽丝", "A")); + list.add(new SaleDelegateModel("海明威", "H")); + list.add(new SaleDelegateModel("奈斯", "N")); + list.add(new SaleDelegateModel("佛山省", "F")); + list.add(new SaleDelegateModel("耀达", "Y")); + list.add(new SaleDelegateModel("卡布达", "K")); + list.add(new SaleDelegateModel("邓超", "D")); + list.add(new SaleDelegateModel("挤挤更健康", "J")); + list.add(new SaleDelegateModel("跑步", "P")); + list.add(new SaleDelegateModel("大家伙", "D")); + list.add(new SaleDelegateModel("瓦特", "W")); + list.add(new SaleDelegateModel("袜子", "W")); + list.add(new SaleDelegateModel("还钱", "H")); + list.add(new SaleDelegateModel("爆胎", "B")); + list.add(new SaleDelegateModel("螺蛳粉", "L")); + list.add(new SaleDelegateModel("家是温馨的港湾", "J")); + list.add(new SaleDelegateModel("李敖", "L")); + list.add(new SaleDelegateModel("耐力强大", "N")); + list.add(new SaleDelegateModel("篙傅升", "G")); + list.add(new SaleDelegateModel("波霸奶茶", "B")); + list.add(new SaleDelegateModel("YY语音", "Y")); + list.add(new SaleDelegateModel("擦来了", "C")); + list.add(new SaleDelegateModel("吃大树", "C")); + list.add(new SaleDelegateModel("出撤的", "C")); + list.add(new SaleDelegateModel("炒饭哥", "C")); + list.add(new SaleDelegateModel("i开头", "I")); + list.add(new SaleDelegateModel("i字姓名", "I")); + list.add(new SaleDelegateModel("I大神", "I")); + list.add(new SaleDelegateModel("保罗", "B")); + list.add(new SaleDelegateModel("欧耶打个分", "O")); + list.add(new SaleDelegateModel("欧阳震华", "O")); + list.add(new SaleDelegateModel("呕吐的人儿", "O")); + list.add(new SaleDelegateModel("准备", "Z")); + list.add(new SaleDelegateModel("中考", "Z")); + list.add(new SaleDelegateModel("这里怎么走", "Z")); + list.add(new SaleDelegateModel("最好的幸福", "Z")); + list.add(new SaleDelegateModel("赚钱之路", "Z")); + list.add(new SaleDelegateModel("走一遭", "Z")); + list.add(new SaleDelegateModel("祖坟", "Z")); + list.add(new SaleDelegateModel("注册成功", "Z")); + list.add(new SaleDelegateModel("鲨鱼巨人", "S")); + list.add(new SaleDelegateModel("煞风景", "S")); + list.add(new SaleDelegateModel("他在哪", "T")); + list.add(new SaleDelegateModel("他去哪里了", "T")); + list.add(new SaleDelegateModel("它他它", "T")); + list.add(new SaleDelegateModel("踏破铁鞋无觅处", "T")); + list.add(new SaleDelegateModel("藕练车场大神", "O")); + list.add(new SaleDelegateModel("帕克科斯塔", "P")); + list.add(new SaleDelegateModel("屁股缝里", "P")); + list.add(new SaleDelegateModel("皮子", "P")); + list.add(new SaleDelegateModel("巧不死", "Q")); + list.add(new SaleDelegateModel("桥丹凤", "Q")); + list.add(new SaleDelegateModel("V装个样子咯", "V")); + list.add(new SaleDelegateModel("V模拟下数据", "V")); + list.add(new SaleDelegateModel("小明", "X")); + list.add(new SaleDelegateModel("小红", "X")); + list.add(new SaleDelegateModel("晓东", "X")); + list.add(new SaleDelegateModel("笑傲江湖", "X")); + list.add(new SaleDelegateModel("肖金凤", "X")); + list.add(new SaleDelegateModel("赵东明", "Z")); + list.add(new SaleDelegateModel("肇事者", "Z")); + list.add(new SaleDelegateModel("昭告天下", "Z")); + list.add(new SaleDelegateModel("张赞超", "Z")); + list.add(new SaleDelegateModel("赵军", "Z")); + list.add(new SaleDelegateModel("张杰", "Z")); + list.add(new SaleDelegateModel("敲木鱼", "Q")); + list.add(new SaleDelegateModel("瞧一瞧看一看", "Q")); + list.add(new SaleDelegateModel("日头出来了", "R")); + list.add(new SaleDelegateModel("日头下山了", "R")); + list.add(new SaleDelegateModel("氜第三方", "R")); + list.add(new SaleDelegateModel("日币", "R")); + list.add(new SaleDelegateModel("啥的吗", "S")); + list.add(new SaleDelegateModel("傻蛋大春", "S")); + list.add(new SaleDelegateModel("削苹果", "X")); + list.add(new SaleDelegateModel("潇洒哥", "X")); + list.add(new SaleDelegateModel("张三疯", "Z")); + list.add(new SaleDelegateModel("赵冬梅", "Z")); + list.add(new SaleDelegateModel("杀人了", "S")); + list.add(new SaleDelegateModel("沙师弟", "S")); + list.add(new SaleDelegateModel("莎莎", "S")); + list.add(new SaleDelegateModel("支付宝", "Z")); + list.add(new SaleDelegateModel("周五前", "Z")); + list.add(new SaleDelegateModel("猪脚饭", "Z")); + list.add(new SaleDelegateModel("塔字", "T")); + list.add(new SaleDelegateModel("踏青", "T")); + list.add(new SaleDelegateModel("U哟哟", "U")); + list.add(new SaleDelegateModel("U哄哄哄哄", "U")); + list.add(new SaleDelegateModel("章子怡", "Z")); + list.add(new SaleDelegateModel("樟木头", "Z")); + list.add(new SaleDelegateModel("制杖", "Z")); + return list; + } +} diff --git a/app/src/main/java/com/yu/indexalpha/adapter/SaleDelegateAdapter.java b/app/src/main/java/com/yu/indexalpha/adapter/SaleDelegateAdapter.java new file mode 100755 index 0000000..4403d91 --- /dev/null +++ b/app/src/main/java/com/yu/indexalpha/adapter/SaleDelegateAdapter.java @@ -0,0 +1,81 @@ +package com.yu.indexalpha.adapter; + +import android.content.Context; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import com.yu.indexalpha.R; +import com.yu.indexalpha.model.SaleDelegateModel; + +import java.util.List; + +public class SaleDelegateAdapter extends RecyclerView.Adapter { + + public final static int VIEW_INDEX = 0; + public final static int VIEW_CONTENT = 1; + + private Context mContext; + private List mList; + + public SaleDelegateAdapter(Context context, List List) { + this.mContext = context; + this.mList = List; + } + + @Override + public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + if (viewType == VIEW_INDEX) { + View view = LayoutInflater.from(mContext).inflate(R.layout.item_index_sale_delegate, parent, false); + IndexViewHolder holder = new IndexViewHolder(view); + holder.tvIndex = (TextView) view.findViewById(R.id.tv_index); + return holder; + } else { + View view = LayoutInflater.from(mContext).inflate(R.layout.item_content_sale_delegate, parent, false); + ContentViewHolder holder = new ContentViewHolder(view); + holder.tvName = (TextView) view.findViewById(R.id.tv_name); + return holder; + } + } + + @Override + public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { + if (getItemViewType(position) == VIEW_INDEX) { + ((IndexViewHolder) holder).tvIndex.setText(mList.get(position).getFirstWord()); + } else { + ((ContentViewHolder) holder).tvName.setText(mList.get(position).getName()); + } + } + + @Override + public int getItemCount() { + return mList.size(); + } + + @Override + public int getItemViewType(int position) { + if (mList.get(position).isIndex()) { + return VIEW_INDEX; + } else { + return VIEW_CONTENT; + } + } + + private static class IndexViewHolder extends RecyclerView.ViewHolder { + TextView tvIndex; + + IndexViewHolder(View itemView) { + super(itemView); + } + } + + private static class ContentViewHolder extends RecyclerView.ViewHolder { + TextView tvName; + + ContentViewHolder(View itemView) { + super(itemView); + } + } +} diff --git a/app/src/main/java/com/yu/indexalpha/model/SaleDelegateModel.java b/app/src/main/java/com/yu/indexalpha/model/SaleDelegateModel.java new file mode 100755 index 0000000..edbe890 --- /dev/null +++ b/app/src/main/java/com/yu/indexalpha/model/SaleDelegateModel.java @@ -0,0 +1,40 @@ +package com.yu.indexalpha.model; + +public class SaleDelegateModel { + + private String name; + private String firstWord; + private boolean isIndex; + + public SaleDelegateModel() { + } + + public SaleDelegateModel(String name, String firstWord) { + this.name = name; + this.firstWord = firstWord; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getFirstWord() { + return firstWord; + } + + public void setFirstWord(String firstWord) { + this.firstWord = firstWord; + } + + public boolean isIndex() { + return isIndex; + } + + public void setIndex(boolean index) { + isIndex = index; + } +} diff --git a/app/src/main/java/com/yu/indexalpha/util/UnitConverter.java b/app/src/main/java/com/yu/indexalpha/util/UnitConverter.java new file mode 100755 index 0000000..dc4f881 --- /dev/null +++ b/app/src/main/java/com/yu/indexalpha/util/UnitConverter.java @@ -0,0 +1,16 @@ +package com.yu.indexalpha.util; + +import android.content.Context; +import android.util.DisplayMetrics; + +public class UnitConverter { + + public static int dip2px(Context context, int dpValue) { + //获取屏幕密度 + DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); + //屏幕密度的比例值 + float density = displayMetrics.density; + //将dp转换为px + return (int) (dpValue * density + 0.5); + } +} diff --git a/app/src/main/java/com/yu/indexalpha/view/IndexBarView.java b/app/src/main/java/com/yu/indexalpha/view/IndexBarView.java new file mode 100755 index 0000000..1ea9a6d --- /dev/null +++ b/app/src/main/java/com/yu/indexalpha/view/IndexBarView.java @@ -0,0 +1,203 @@ +package com.yu.indexalpha.view; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Rect; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.view.View; +import android.widget.TextView; + +import com.yu.indexalpha.util.UnitConverter; + +public class IndexBarView extends View { + + //索引字母颜色 + private static final int LETTER_COLOR = 0xFF333333; + + //索引字母背景圆圈颜色 + private static final int BG_COLOR = 0xFF18ad84; + + //索引字母数组 + public String[] indexs = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"}; + + //控件的宽高 + private int mWidth; + + private int mHeight; + + //单元格的高度 + private float mCellHeight; + + //顶部间距 + private float mMarginTop; + + //手指按下的字母索引 + private int touchIndex = 0; + + private Paint mPaint; + + private Paint bgPaint; + + public IndexBarView(Context context) { + super(context); + init(); + } + + public IndexBarView(Context context, AttributeSet attrs) { + super(context, attrs); + init(); + } + + private void init() { + setBackgroundColor(0xffffffff); + mPaint = new Paint(); + mPaint.setColor(LETTER_COLOR); + mPaint.setTextSize(UnitConverter.dip2px(getContext(), 9)); + mPaint.setAntiAlias(true); // 去掉锯齿,让字体边缘变得平滑 + + bgPaint = new Paint(); + bgPaint.setColor(BG_COLOR); + bgPaint.setAntiAlias(true); + } + + public void setIndexs(String[] indexs) { + this.indexs = indexs; + mMarginTop = (mHeight - mCellHeight * indexs.length) / 2; + invalidate(); + } + + @Override + protected void onDraw(Canvas canvas) { + //字母的坐标点:(x,y) + if (indexs.length <= 0) { + return; + } + for (int i = 0; i < indexs.length; i++) { + String letter = indexs[i]; + //判断是不是我们按下的当前字母 + if (touchIndex == i) { + //绘制文字圆形背景 + canvas.drawCircle(mWidth / 2, getTextHeight(letter) + mCellHeight * i + mMarginTop + UnitConverter.dip2px(getContext(), 2), UnitConverter.dip2px(getContext(), 7), bgPaint); + mPaint.setColor(Color.WHITE); + } else { + mPaint.setColor(LETTER_COLOR); + } + float x = mWidth / 2 - getTextWidth(letter) / 2; + float y = mCellHeight / 2 + getTextHeight(letter) / 2 + mCellHeight * i + mMarginTop; + canvas.drawText(letter, x, y, mPaint); + } + } + + /** + * 获取字符的宽度 + * + * @param text 需要测量的字母 + * @return 对应字母的高度 + */ + public float getTextWidth(String text) { + Rect bounds = new Rect(); + mPaint.getTextBounds(text, 0, text.length(), bounds); + return bounds.width(); + } + + /** + * 获取字符的高度 + * + * @param text 需要测量的字母 + * @return 对应字母的高度 + */ + public float getTextHeight(String text) { + Rect bounds = new Rect(); + mPaint.getTextBounds(text, 0, text.length(), bounds); + return bounds.height(); + } + + + @Override + protected void onSizeChanged(int w, int h, int oldw, int oldh) { + super.onSizeChanged(w, h, oldw, oldh); + mWidth = getMeasuredWidth(); + mHeight = getMeasuredHeight(); + mCellHeight = (mHeight * 1f / 27); //26个字母加上“#” + mMarginTop = (mHeight - mCellHeight * indexs.length) / 2; + } + + // 处理Touch事件 + @Override + public boolean onTouchEvent(MotionEvent event) { + switch (event.getAction()) { + case MotionEvent.ACTION_DOWN: + case MotionEvent.ACTION_MOVE: + // 按下字母的下标 + int letterIndex = (int) ((event.getY() - mMarginTop) / mCellHeight); + if (letterIndex != touchIndex) { + if (letterIndex > indexs.length - 1) { + touchIndex = indexs.length - 1; + } else if (letterIndex < 0) { + touchIndex = 0; + } else { + touchIndex = letterIndex; + } + } + // 判断是否越界 + if (letterIndex >= 0 && letterIndex < indexs.length) { + // 显示按下的字母 + if (textView != null) { + textView.setVisibility(View.VISIBLE); + textView.setText(indexs[letterIndex]); + } + //通过回调方法通知列表定位 + if (mOnIndexChangedListener != null) { + mOnIndexChangedListener.onIndexChanged(indexs[letterIndex]); + } + } + invalidate(); + break; + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_CANCEL: + if (textView != null) { + textView.setVisibility(View.GONE); + } + break; + } + + return true; + } + + public void setTouchIndex(String word) { + for (int i = 0; i < indexs.length; i++) { + if (indexs[i].equals(word)) { + touchIndex = i; + invalidate(); + return; + } + } + } + + public interface OnIndexChangedListener { + /** + * 按下字母改变了 + * + * @param index 按下的字母 + */ + void onIndexChanged(String index); + } + + private OnIndexChangedListener mOnIndexChangedListener; + + private TextView textView; + + public void setOnIndexChangedListener(OnIndexChangedListener onIndexChangedListener) { + this.mOnIndexChangedListener = onIndexChangedListener; + } + + /** + * 设置显示按下首字母的TextView + */ + public void setSelectedIndexTextView(TextView textView) { + this.textView = textView; + } +} diff --git a/app/src/main/res/drawable/toast_bg.xml b/app/src/main/res/drawable/toast_bg.xml new file mode 100644 index 0000000..0139e5a --- /dev/null +++ b/app/src/main/res/drawable/toast_bg.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..c5b1f78 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,19 @@ + + + + + + diff --git a/app/src/main/res/layout/activity_sale_delegate.xml b/app/src/main/res/layout/activity_sale_delegate.xml new file mode 100644 index 0000000..a7326e8 --- /dev/null +++ b/app/src/main/res/layout/activity_sale_delegate.xml @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/item_content_sale_delegate.xml b/app/src/main/res/layout/item_content_sale_delegate.xml new file mode 100755 index 0000000..8b4fb2e --- /dev/null +++ b/app/src/main/res/layout/item_content_sale_delegate.xml @@ -0,0 +1,15 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_index_sale_delegate.xml b/app/src/main/res/layout/item_index_sale_delegate.xml new file mode 100755 index 0000000..c861d72 --- /dev/null +++ b/app/src/main/res/layout/item_index_sale_delegate.xml @@ -0,0 +1,16 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher.png b/app/src/main/res/mipmap-hdpi/ic_launcher.png new file mode 100644 index 0000000..cde69bc Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher_round.png b/app/src/main/res/mipmap-hdpi/ic_launcher_round.png new file mode 100644 index 0000000..9a078e3 Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher.png b/app/src/main/res/mipmap-mdpi/ic_launcher.png new file mode 100644 index 0000000..c133a0c Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher_round.png b/app/src/main/res/mipmap-mdpi/ic_launcher_round.png new file mode 100644 index 0000000..efc028a Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher.png b/app/src/main/res/mipmap-xhdpi/ic_launcher.png new file mode 100644 index 0000000..bfa42f0 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png new file mode 100644 index 0000000..3af2608 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher.png b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png new file mode 100644 index 0000000..324e72c Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png new file mode 100644 index 0000000..9bec2e6 Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png new file mode 100644 index 0000000..aee44e1 Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png new file mode 100644 index 0000000..34947cd Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml new file mode 100644 index 0000000..42288e1 --- /dev/null +++ b/app/src/main/res/values/colors.xml @@ -0,0 +1,8 @@ + + + #3F51B5 + #303F9F + #FF4081 + + #FFFFFF + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml new file mode 100644 index 0000000..4f16dac --- /dev/null +++ b/app/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + IndexAlpha + diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml new file mode 100644 index 0000000..5885930 --- /dev/null +++ b/app/src/main/res/values/styles.xml @@ -0,0 +1,11 @@ + + + + + + diff --git a/app/src/test/java/com/yu/indexalpha/ExampleUnitTest.java b/app/src/test/java/com/yu/indexalpha/ExampleUnitTest.java new file mode 100644 index 0000000..4495957 --- /dev/null +++ b/app/src/test/java/com/yu/indexalpha/ExampleUnitTest.java @@ -0,0 +1,17 @@ +package com.yu.indexalpha; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Example local unit test, which will execute on the development machine (host). + * + * @see Testing documentation + */ +public class ExampleUnitTest { + @Test + public void addition_isCorrect() throws Exception { + assertEquals(4, 2 + 2); + } +} \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..1ea4bd0 --- /dev/null +++ b/build.gradle @@ -0,0 +1,23 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. + +buildscript { + repositories { + jcenter() + } + dependencies { + classpath 'com.android.tools.build:gradle:2.3.0' + + // NOTE: Do not place your application dependencies here; they belong + // in the individual module build.gradle files + } +} + +allprojects { + repositories { + jcenter() + } +} + +task clean(type: Delete) { + delete rootProject.buildDir +} diff --git a/gif/record.gif b/gif/record.gif new file mode 100644 index 0000000..13b2062 Binary files /dev/null and b/gif/record.gif differ diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..aac7c9b --- /dev/null +++ b/gradle.properties @@ -0,0 +1,17 @@ +# Project-wide Gradle settings. + +# IDE (e.g. Android Studio) users: +# Gradle settings configured through the IDE *will override* +# any settings specified in this file. + +# For more details on how to configure your build environment visit +# http://www.gradle.org/docs/current/userguide/build_environment.html + +# Specifies the JVM arguments used for the daemon process. +# The setting is particularly useful for tweaking memory settings. +org.gradle.jvmargs=-Xmx1536m + +# When configured, Gradle will run in incubating parallel mode. +# This option should only be used with decoupled projects. More details, visit +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects +# org.gradle.parallel=true diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..13372ae Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..431473e --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Fri Sep 22 16:37:21 CST 2017 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..9d82f78 --- /dev/null +++ b/gradlew @@ -0,0 +1,160 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; +esac + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..aec9973 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,90 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windowz variants + +if not "%OS%" == "Windows_NT" goto win9xME_args +if "%@eval[2+2]" == "4" goto 4NT_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* +goto execute + +:4NT_args +@rem Get arguments from the 4NT Shell from JP Software +set CMD_LINE_ARGS=%$ + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..e7b4def --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +include ':app'