Skip to content

Commit

Permalink
#add RecyclerWheelView use custom Annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Dawish committed Aug 9, 2017
1 parent 934a3b8 commit f4754dc
Show file tree
Hide file tree
Showing 14 changed files with 325 additions and 32 deletions.
5 changes: 4 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions AppLibrary/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 22,9 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'

compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:recyclerview-v7:23.4.0'

compile "com.android.support:appcompat-v7:$rootProject.ext.supportVersion"
compile "com.android.support:recyclerview-v7:$rootProject.ext.supportVersion"
compile "com.android.support:support-v4:$rootProject.ext.supportVersion"
compile 'com.squareup.okio:okio:1.13.0'

compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
Expand Down
17 changes: 0 additions & 17 deletions AppLibrary/src/main/java/danxx/library/tools/AppUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -404,21 404,4 @@ private Bitmap adjustBitmapSimpleSize(int resId, Context context, int height, in
return BitmapFactory.decodeResource(context.getResources(),resId, opts);
}


/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale 0.5f);
}

/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale 0.5f);
}

}
15 changes: 15 additions & 0 deletions AppLibrary/src/main/java/danxx/library/tools/UIUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 155,19 @@ public static int getChildDrawingOrder(int childCount, int i, int focusedIndex)
return i;
}

/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dp2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale 0.5f);
}

/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale 0.5f);
}
}
222 changes: 222 additions & 0 deletions AppLibrary/src/main/java/danxx/library/widget/RecyclerWheelView.java
Original file line number Diff line number Diff line change
@@ -0,0 1,222 @@
package danxx.library.widget;
import danxx.library.R;
import danxx.library.tools.UIUtils;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSnapHelper;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
/**
* Created by dawish on 2017/8/9.
*/

public class RecyclerWheelView extends RecyclerView {
public interface OnSelectListener {
void onSelect(int position);
}

private ArrayList<String> mData;
private int mOffset = 2;//默认每页显示五条
private float mItemHeight;
private int mWidth;
private LinearLayoutManager mManager;
private LinearSnapHelper mSnapHelper;

private OnSelectListener mOnSelectListener;

public RecyclerWheelView(Context context) {
super(context);
initData();
}

public RecyclerWheelView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initData();
}

public RecyclerWheelView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initData();
}

private void initData() {
mData = new ArrayList<>();

mItemHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 45, getResources().getDisplayMetrics());
mManager = new LinearLayoutManager(getContext());
mManager.setOrientation(LinearLayoutManager.VERTICAL);

if (null == paint) {
paint = new Paint();
paint.setColor(Color.parseColor("#83cde6"));
paint.setStrokeWidth(UIUtils.dp2px(getContext(), 1f));
}
post(new Runnable() {
@Override
public void run() {//RecyclerView还未初始化完成不能直接调用其方法否则会报空指针,类构造方法中只能进行初始化成员变量
getLayoutParams().height = (int) (mItemHeight * (mOffset * 2 1));
setLayoutManager(mManager);

setAdapter(new MySpaceAdapter());

getAdapter().notifyDataSetChanged();

mWidth = getWidth();
}
});

mSnapHelper = new LinearSnapHelper();
mSnapHelper.attachToRecyclerView(this);
}

public ArrayList<String> getData() {
if (mData.size() < 1) {
return null;
}
ArrayList<String> data = new ArrayList<>();
for (int i = 1; i < mData.size() - 1; i ) {
data.add(mData.get(i));
}
return data;
}

public void setData(ArrayList<String> data) {
this.mData.clear();
this.mData.addAll(data);
mData.add(0, null);
mData.add(null);
if (getAdapter() != null)
getAdapter().notifyDataSetChanged();
}

private List<TextView> textViews;

/**
* RecyclerView实现WheelView
*/
class MySpaceAdapter extends RecyclerView.Adapter {


MySpaceAdapter() {
textViews = new ArrayList<>();
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == 1) {
return new SpaceViewHolder(new View(getContext()));
} else {
View view = LayoutInflater.from(getContext()).inflate(R.layout.item_recycler_wheel, parent, false);
textViews.add((TextView) view);
return new NormalViewHolder(view);
}
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof NormalViewHolder) {
TextView textView = ((NormalViewHolder) holder).mText;
textView.setText(mData.get(position));
}
}

@Override
public int getItemCount() {
return mData.size();
}

@Override
public int getItemViewType(int position) {
return (position == 0 || position == mData.size() - 1) ? 1 : 0;
}
}

class NormalViewHolder extends RecyclerView.ViewHolder {
TextView mText;

NormalViewHolder(View itemView) {
super(itemView);
mText = (TextView) itemView;
mText.getLayoutParams().height = (int) mItemHeight;
}
}

class SpaceViewHolder extends RecyclerView.ViewHolder {
SpaceViewHolder(View itemView) {
super(itemView);
//根据每一条高度及偏移量设置高度
float height = mOffset * mItemHeight;
itemView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) height));
}
}

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
/**
* 手动滑动或者惯性滑动时触发该方法,不过由于本身没有滑动所以l,t,oldl,oldt都是0
* 此处只是在滑动时设置相应状态
*/
View viewContainer = mSnapHelper.findSnapView(mManager);//获取中间位置的view
TextView textView = ((NormalViewHolder) getChildViewHolder(viewContainer)).mText;

int centerPosition = mData.indexOf(textView.getText().toString());
if (mCenterPosition == centerPosition) {
return;
}
mCenterPosition = centerPosition;

for (TextView view : textViews) {//设置选中颜色
view.setTextColor(Color.parseColor("#dddddd"));
if (view == textView) {
view.setTextColor(Color.parseColor("#0288ce"));
}
}

if (mOnSelectListener != null) {
//第一条是占位条
mOnSelectListener.onSelect(mCenterPosition - 1);
}
}

private int mCenterPosition;//获取选中位置时要减一(有占位条)
private Paint paint;

/**
* 获取选中区域的边界
*/
private int[] selectedAreaBorder;

private int[] obtainSelectedAreaBorder() {
if (null == selectedAreaBorder) {
selectedAreaBorder = new int[2];
selectedAreaBorder[0] = (int) (mItemHeight * mOffset);
selectedAreaBorder[1] = (int) (mItemHeight * (mOffset 1));
}
return selectedAreaBorder;
}

@Override
public void onDraw(Canvas c) {
super.onDraw(c);
c.drawLine(mWidth * 1.0F / 6, obtainSelectedAreaBorder()[0], mWidth * 5 / 6, obtainSelectedAreaBorder()[0], paint);
c.drawLine(mWidth * 1.0F / 6, obtainSelectedAreaBorder()[1], mWidth * 5 / 6, obtainSelectedAreaBorder()[1], paint);
}

public void setmOnSelectListener(OnSelectListener mOnSelectListener) {
this.mOnSelectListener = mOnSelectListener;
}
}
9 changes: 9 additions & 0 deletions AppLibrary/src/main/res/layout/item_recycler_wheel.xml
Original file line number Diff line number Diff line change
@@ -0,0 1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#333333"
android:gravity="center"
android:textSize="22sp">

</TextView>
5 changes: 3 additions & 2 deletions Channel1App/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 45,9 @@ android {
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
compile "com.android.support:appcompat-v7:$rootProject.ext.supportVersion"
compile "com.android.support:recyclerview-v7:$rootProject.ext.supportVersion"
compile "com.android.support:cardview-v7:$rootProject.ext.supportVersion"
compile 'com.squareup.okio:okio:1.13.0'

compile project(':AppLibrary')
Expand Down
11 changes: 5 additions & 6 deletions Channel2App/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 23,13 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile fileTree(include: ['*.jar'], dir: 'libs')
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:23.4.0'
compile 'com.android.support:support-v4:23.4.0'
compile 'com.android.support:recyclerview-v7:23.4.0'

compile "com.android.support:appcompat-v7:$rootProject.ext.supportVersion"
compile "com.android.support:support-v4:$rootProject.ext.supportVersion"
compile "com.android.support:appcompat-v7:$rootProject.ext.supportVersion"
testCompile 'junit:junit:4.12'
compile project(':AppLibrary')
}
2 changes: 1 addition & 1 deletion Channel2App/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 15,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".ui.ActivityRecyclerWheelView"></activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 1,39 @@
package com.anno.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import com.anno.R;
import com.anno.annotation.AnnotateUtils;
import com.anno.annotation.ViewInject;

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

import danxx.library.widget.RecyclerWheelView;

/**
* Created by dawish on 2017/8/9.
*/

public class ActivityRecyclerWheelView extends AppCompatActivity {

@ViewInject(R.id.recyclerWheelView)
RecyclerWheelView recyclerWheelView;

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

AnnotateUtils.inject(ActivityRecyclerWheelView.this);

ArrayList<String> data = new ArrayList<String>();
for(int i=0;i<29;i ){
data.add("data: " i);
}

recyclerWheelView.setData(data);

}
}
Loading

0 comments on commit f4754dc

Please sign in to comment.