Skip to content

Commit

Permalink
整理代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Othershe committed Oct 26, 2017
1 parent cadd266 commit 47989f6
Show file tree
Hide file tree
Showing 19 changed files with 277 additions and 123 deletions.
108 changes: 107 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,107 @@
# GroupIndexLib
# GroupIndexLib

### 基础效果:
|![](image/1.gif)|![](image/2.gif)|
|---|---|
### 基本用法:
**Step 1. 添加JitPack仓库**
在当前项目等根目录下的 `build.gradle` 文件中添加如下内容:
``` gradle
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
```
**Step 2. 添加项目依赖**
``` gradle
dependencies {
compile 'com.github.Othershe:GroupIndex:1.0.0'
}
```
**Step 3.设置ItemDecoration**

```java
//得到tags集合,即对数据源排序后,每个数据会有一个唯一标识字段(tag),然后提取该字段值,组成一个tags集合
//这里提供一个按字母升序排列数据源的方法
//ItemData可以替换为自己的数据Bean
//排序的这部分可以完全自己实现哦,最终的目的只是得到数据源的所有唯一标识字段值集合
SortHelper<ItemData> sortHelper = new SortHelper<ItemData>() {
@Override
public String sortField(ItemData data) {
//用哪个字段的值排序就返回对应字段值
return data.getTitle();
}
};
//对datas数据集排序
sortHelper.sortByLetter(datas);
//提取tag值
List<String> tags = sortHelper.getTags(datas);

//设置GroupHeader
recyclerView.addItemDecoration(new GroupHeaderItemDecoration(this)
.setTags(tags)//设置准备好的tags集合
.setGroupHeaderHeight(30)//设置GroupHeader的高度,单位dp
.setGroupHeaderLeftPadding(20)//设置GroupHeader左padding,单位dp
.setGroupHeaderColor("#FFEEEEEE")//设置GroupHeader的背景色
.setGroupHeaderTextColor("#FF999999")//设置GroupHeader的上的文字颜色
.setGroupHeaderTextSize(12))//设置GroupHeader上的文字尺寸
.showSuspensionGroupHeader(flase)////默认显示
//自定义绘制GroupHeader,可参考代码中的例子
.setOnDrawItemDecorationListener(new OnDrawItemDecorationListener() {
@Override
public void onDrawGroupHeader(Canvas c, Paint paint, TextPaint textPaint, int[] params, int position) {

}

@Override
public void onDrawSuspensionGroupHeader(Canvas c, Paint paint, TextPaint textPaint, int[] params, int position) {

}
})
);

//设置分割线
recyclerView.addItemDecoration(new DivideItemDecoration().setTags(tags));
```
**Step 4.使用SideBar**
```java
<com.othershe.groupindexlib.SideBar
android:id="@+id/side_bar"
android:layout_width="20dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
app:text_color="#000000"
app:text_size="14"
app:touch_color="#88999999"
app:untouch_color="#00FFFFFF" />
```
```java
//设置SideBar上显示的索引字符数组,默认如gif图所示
sideBar.setIndexsArray();
//设置排序后的列表数据源的要在GroupHeader上显示的tags集合、以及SideBar的触摸反馈接口
sideBar.setOnSideBarTouchListener(tags, new OnSideBarTouchListener() {
@Override
public void onTouch(String text, int position) {
//手指触摸
//text 代表当前手指触摸的字符
//position 代表RecyclerView将要滚动到的位置,-1代表没找到对应position,则不用滚动列表
//滚动则使用layoutManager.scrollToPositionWithOffset(position, 0)方法
}

@Override
public void onTouchEnd() {
//手指触摸结束
}
});
```
SideBar相关自定义属性:

|属性名|含义
|---|---|
|text_color| 设置索引字符颜色
|text_size|设置索引字符尺寸(sp)
|touch_color|设置SideBar被触摸时的背景颜色
|untouch_color|设置SideBar默认背景颜色
2 changes: 1 addition & 1 deletion app/src/main/java/com/test/groupindex/ItemData.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.test.groupindex;

import com.othershe.groupindexlib.BaseItem;
import com.othershe.groupindexlib.bean.BaseItem;

public class ItemData extends BaseItem {

Expand Down
70 changes: 55 additions & 15 deletions app/src/main/java/com/test/groupindex/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
package com.test.groupindex;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
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.TextPaint;
import android.view.View;
import android.widget.TextView;

import com.othershe.groupindexlib.DivideItemDecoration;
import com.othershe.groupindexlib.GroupIndexItemDecoration;
import com.othershe.groupindexlib.OnGroupHeaderViewListener;
import com.othershe.groupindexlib.OnSideBarTouchListener;
import com.othershe.groupindexlib.SideBar;
import com.othershe.groupindexlib.SortHelper;
import com.othershe.groupindexlib.ViewHolder;
import com.othershe.groupindexlib.decoration.DivideItemDecoration;
import com.othershe.groupindexlib.decoration.GroupHeaderItemDecoration;
import com.othershe.groupindexlib.helper.Utils;
import com.othershe.groupindexlib.listener.OnDrawItemDecorationListener;
import com.othershe.groupindexlib.listener.OnSideBarTouchListener;
import com.othershe.groupindexlib.weiget.SideBar;
import com.othershe.groupindexlib.helper.SortHelper;

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

public class MainActivity extends AppCompatActivity {

private Context context;

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

RecyclerView list = (RecyclerView) findViewById(R.id.list);
context = this;

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
SideBar sideBar = (SideBar) findViewById(R.id.side_bar);
final TextView tip = (TextView) findViewById(R.id.tip);

final List<ItemData> datas = new ArrayList<>();
ItemData data = new ItemData("北京");
final ItemData data = new ItemData("北京");
datas.add(data);
ItemData data1 = new ItemData("上海");
datas.add(data1);
Expand Down Expand Up @@ -110,18 +120,48 @@ public String sortField(ItemData data) {
}
};
sortHelper.sortByLetter(datas);
List<String> tags = sortHelper.getTags(datas);
final List<String> tags = sortHelper.getTags(datas);

MyAdapter adapter = new MyAdapter(this, datas, false);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
list.setLayoutManager(layoutManager);
list.addItemDecoration(new DivideItemDecoration().setTags(tags));
list.addItemDecoration(new GroupIndexItemDecoration(this)
recyclerView.setLayoutManager(layoutManager);

recyclerView.addItemDecoration(new GroupHeaderItemDecoration(this)
.setTags(tags)
.setGroupHeaderHeight(30)
.setGroupHeaderLeftPadding(20));
list.setAdapter(adapter);
.setGroupHeaderLeftPadding(20)
// .setOnDrawItemDecorationListener(new OnDrawItemDecorationListener() {
// @Override
// public void onDrawGroupHeader(Canvas c, Paint paint, TextPaint textPaint, int[] params, int position) {
// c.drawRect(params[0], params[1], params[2], params[3], paint);
//
// int x = params[0] + Utils.dip2px(context, 20);
// int y = params[1] + (Utils.dip2px(context, 30) + Utils.getTextHeight(textPaint, tags.get(position))) / 2;
//
// Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher, null);
// Bitmap icon1 = Bitmap.createScaledBitmap(icon, Utils.dip2px(context, 20), Utils.dip2px(context, 20), true);
// c.drawBitmap(icon1, x, params[1] + Utils.dip2px(context, 5), paint);
//
// c.drawText(tags.get(position), x + Utils.dip2px(context, 25), y, textPaint);
// }
//
// @Override
// public void onDrawSuspensionGroupHeader(Canvas c, Paint paint, TextPaint textPaint, int[] params, int position) {
// c.drawRect(params[0], params[1], params[2], params[3], paint);
// int x = params[0] + Utils.dip2px(context, 20);
// int y = params[1] + (Utils.dip2px(context, 30) + Utils.getTextHeight(textPaint, tags.get(position))) / 2;
//
// Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher, null);
// Bitmap icon1 = Bitmap.createScaledBitmap(icon, Utils.dip2px(context, 20), Utils.dip2px(context, 20), true);
// c.drawBitmap(icon1, x, params[1] + Utils.dip2px(context, 5), paint);
//
// c.drawText(tags.get(position), x + Utils.dip2px(context, 25), y, textPaint);
// }
// })
);
recyclerView.addItemDecoration(new DivideItemDecoration().setTags(tags));
recyclerView.setAdapter(adapter);

sideBar.setOnSideBarTouchListener(tags, new OnSideBarTouchListener() {
@Override
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/com/test/groupindex/MyAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import com.othershe.baseadapter.ViewHolder;
import com.othershe.baseadapter.base.CommonBaseAdapter;
import com.othershe.groupindexlib.BaseItem;

import java.util.List;

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent" />

<com.othershe.groupindexlib.SideBar
<com.othershe.groupindexlib.weiget.SideBar
android:id="@+id/side_bar"
android:layout_width="20dp"
android:layout_height="match_parent"
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
3 changes: 3 additions & 0 deletions groupindexlib/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
apply plugin: 'com.android.library'

apply plugin: 'com.github.dcendents.android-maven'
group='com.github.Othershe'

android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.othershe.groupindexlib;
package com.othershe.groupindexlib.bean;

public class BaseItem {
private String tag;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.othershe.groupindexlib;
package com.othershe.groupindexlib.decoration;

import android.graphics.Canvas;
import android.graphics.Color;
Expand All @@ -8,6 +8,8 @@
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.othershe.groupindexlib.helper.Utils;

import java.util.List;

public class DivideItemDecoration extends RecyclerView.ItemDecoration {
Expand Down Expand Up @@ -35,7 +37,7 @@ public void getItemOffsets(Rect outRect, View view, RecyclerView parent, Recycle

int position = parent.getChildAdapterPosition(view);
if (!Utils.listIsEmpty(tags) && (position + 1) < tags.size() && tags.get(position).equals(tags.get(position + 1))) {
//当前itemView的data的tag和下一个item的不相等,则为当前itemView设置bottom padding值
//当前ItemView的data的tag和下一个ItemView的不相等,则为当前ItemView设置bottom 偏移量
outRect.set(0, 0, 0, divideHeight);
}
}
Expand Down
Loading

0 comments on commit 47989f6

Please sign in to comment.