<repo_name>jimi9527/AutoLocationRecyclerView<file_sep>/app/src/main/java/com/example/dengjx/verticalrecylerviewselector/RulerRecyclerView.java
package com.example.dengjx.verticalrecylerviewselector;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

/**
 * Created by dengjx on 2017/12/27.
 */

public class RulerRecyclerView extends RecyclerView {
    //偏移的距离
    private int delta;
    public RulerRecyclerView(Context context) {
        this(context,null);
    }

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

    @Override
    public void onScrolled(int dx, int dy) {
        super.onScrolled(dx, dy);
         delta = delta + dx;
    }

    @Override
    public void onScrollStateChanged(int state) {
        super.onScrollStateChanged(state);

        // 手抬起的动作
        if(state == SCROLL_STATE_IDLE){

        }

    }
}
<file_sep>/app/src/main/java/com/example/dengjx/verticalrecylerviewselector/RulerView.java
package com.example.dengjx.verticalrecylerviewselector;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;

/**
 * Created by dengjx on 2017/12/26.
 */

public class RulerView extends View{

    private static final String TAG ="RulerView";
    //中间标准刻度的颜色
    private int mCenterColor = Color.RED;
    // 中间刻度文字的颜色
    private int mCenterTextColor = Color.BLUE;
    // 中间刻度线的高大小
    private int mCenterHigh = 80;
    // 中间线条的宽度
    private int mCenterWidth = 6;
    private int leftMargin = 50;
    private int mCenterTextHeight ;
    // 刻度的起点X
    private int ruleX;

    // 显示刻度
    private String mCenterText = "5";
    // 偏移值
    private int offset = 5;
    // 平均刻度的值
    private int avagerNum;

    // 刻度的高度
    private int ruleHeigt;
    // 滚动器
    private RulerViewScroller scroller;

    // 是否执行滚动
    private boolean isScrollingPerformed;
    // 屏幕的宽度
    private int mScreenWidth;


    public RulerView(Context context) {
        this(context,null);
    }

    public RulerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RulerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setBackgroundColor(Color.BLUE);
      //  scroller = new RulerViewScroller(context,scrollinglistener);
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        mScreenWidth = displayMetrics.widthPixels;
        Log.d(TAG,"mScreenWidth:"+ mScreenWidth);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureWidthSize(widthMeasureSpec),measureHeightSize(heightMeasureSpec));
    }

    private int measureHeightSize(int heightMeasureSpec) {
        int result;
        int mode=MeasureSpec.getMode(heightMeasureSpec);
        int size=MeasureSpec.getSize(heightMeasureSpec);
        if(mode==MeasureSpec.EXACTLY){
            result=size;
        }else{
            result = getHeight() + getPaddingTop() + getPaddingBottom();
            if(mode==MeasureSpec.AT_MOST){
                result=Math.min(result,size);
            }
        }
        Log.d(TAG,"result---HEIGHT:"+result);
        return  result;
    }

    private int measureWidthSize(int widthMeasureSpec) {
        int result;
        int mode=MeasureSpec.getMode(widthMeasureSpec);
        int size=MeasureSpec.getSize(widthMeasureSpec);
        if(mode==MeasureSpec.EXACTLY){
            result=size;
        }else{
            result=size + getPaddingLeft() + getPaddingRight();
            if(mode==MeasureSpec.AT_MOST){
                result=Math.min(result,size);
            }
        }
        Log.d(TAG,"result:"+result);
        return  result;
    }
    public int returnAvg(){
        return avagerNum;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //drawCenterLine(canvas);
        drwacenterBottonLine(canvas);


    }
    // 绘制底部直线
    public void drwacenterBottonLine(Canvas canvas){
        // 底部直线
        Paint paint = new Paint();
        paint.setColor(mCenterColor);
        paint.setStrokeWidth(mCenterWidth / 2);
        canvas.drawLine(leftMargin+mCenterWidth/2 ,getMeasuredHeight() / 2  +mCenterHigh + offset / 2,
                mScreenWidth - leftMargin ,getMeasuredHeight() / 2  + mCenterHigh + offset / 2,paint);


        // 绘制底部数字
        for(int i = 1; i <= 10; i++){
            Paint mTextPaint = new Paint();
            mTextPaint.setTextSize(40);
            mTextPaint.setColor(Color.BLACK);
            Rect mCenterRect = new Rect();

            mTextPaint.getTextBounds(String.valueOf(i),0,1,mCenterRect);
            Log.d(TAG,"mCenterRect.width:"+mCenterRect.width());
            mCenterTextHeight = mCenterRect.height();
            avagerNum = (mScreenWidth-leftMargin *2 ) / 9 ;
            ruleX = leftMargin - mCenterRect.width() / 2;
            Log.d(TAG,"ruleX.width:"+ ruleX);
            canvas.drawText(String.valueOf(i),ruleX + avagerNum * (i-1) ,
                    getMeasuredHeight() / 2 + mCenterHigh + 50   ,mTextPaint);
        }


        // 绘制刻度
        for(int j = 1; j <= 10; j++){
            Paint linepaint = new Paint();
            linepaint.setColor(mCenterColor);
            linepaint.setStrokeWidth(mCenterWidth /2 );
            if(j == 1 || j == 10){
                ruleHeigt = 50;
            }else {
                ruleHeigt = 30;
            }

            canvas.drawLine(leftMargin+mCenterWidth/2 + avagerNum * ( j - 1) ,
                    getMeasuredHeight() / 2  + mCenterHigh + offset / 2 - ruleHeigt,
                    leftMargin+mCenterWidth/2  + avagerNum *(j - 1) ,
                    getMeasuredHeight() / 2  + mCenterHigh + offset / 2 + mCenterWidth /2  ,linepaint);
        }


    }



    // 绘制中间的线和数字
    public void drawCenterLine(Canvas canvas){
        Paint mTextPaint = new Paint();
        mTextPaint.setTextSize(50);
        mTextPaint.setColor(mCenterTextColor);
        Rect mCenterRect = new Rect();

        mTextPaint.getTextBounds(mCenterText,0,1,mCenterRect);
        Log.d(TAG,"mCenterRect.height:"+mCenterRect.height());
        mCenterTextHeight = mCenterRect.height();
        canvas.drawText(mCenterText,getMeasuredWidth()/2 - mCenterRect.width()/2 ,getMeasuredHeight()/2,mTextPaint);

        Paint paint = new Paint();
        paint.setColor(mCenterColor);
        paint.setStrokeWidth(mCenterWidth);
        canvas.drawLine(getMeasuredWidth()/2 + mCenterWidth/2 ,getMeasuredHeight() / 2 + mCenterRect.height()+offset ,
                getMeasuredWidth()/2 + mCenterWidth / 2 ,getMeasuredHeight() / 2 +
                        mCenterRect.height() + offset + mCenterHigh,paint);

    }

}
<file_sep>/app/src/main/java/com/example/dengjx/verticalrecylerviewselector/BaseFootViewAdapter.java
package com.example.dengjx.verticalrecylerviewselector;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by dengjx on 2017/12/29.
 */

public abstract class BaseFootViewAdapter<E> extends BaseAdapter {
    public static final int DEFAULT_SHOW_COUNT = 3;
    protected Context mContext;
    protected ListView mListView;
    protected LayoutInflater inflater;
    protected LinearLayout headView;
    protected TextView mBtnMore;
    protected ArrayList<E> mShowObjects = new ArrayList<>();
    protected ArrayList<E> mAllObjects = new ArrayList<>();
    protected boolean shrink = true;

    private  BaseFootViewAdapter(){
    }
    public BaseFootViewAdapter(Context context, ListView listView){
        this.mListView = listView;
        this.mContext = context;
        inflater = LayoutInflater.from(mContext);
        headView = (LinearLayout) inflater.inflate(R.layout.iv_footer_button,null);
        mBtnMore = headView.findViewById(R.id.text_show);
        mBtnMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeShow();
            }
        });
        mListView.addFooterView(headView,null,false);
    }
    public void setAapterData(ArrayList<E> mAllObjects){
        this.mAllObjects = mAllObjects;
        mShowObjects.clear();
        if(mAllObjects != null){
            if(mAllObjects.size() <= DEFAULT_SHOW_COUNT){
                headView.setVisibility(View.GONE);
                mShowObjects.addAll(mAllObjects);
            }else {
                headView.setVisibility(View.VISIBLE);
                for(int i = 0; i < DEFAULT_SHOW_COUNT; i++){
                    mShowObjects.add(mAllObjects.get(i));
                }
            }
        }
        notifyDataSetChanged();
        setListViewHeightBaseOnChildern(mListView);
    }

    @Override
    public int getCount() {
        int showCount = 0;
        if(mShowObjects != null){
            showCount = mShowObjects.size();
        }
        return showCount;
    }
    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public E getItem(int i) {
        E object = null;
        if(mShowObjects != null){
            object = mShowObjects.get(i);
        }
        return object;
    }

    private void changeShow() {
        if(headView.getVisibility() == View.GONE){
            headView.setVisibility(View.VISIBLE);
        }
        mShowObjects.clear();
        if(shrink){
            shrink = false;
            mShowObjects.addAll(mAllObjects);
            mBtnMore.setText("收起");
        }else {
            shrink = true;
            for(int i = 0; i < DEFAULT_SHOW_COUNT; i++){
                mShowObjects.add(mAllObjects.get(i));
            }
             mBtnMore.setText("查看所有的评论");
        }
       notifyDataSetChanged();
        setListViewHeightBaseOnChildern(mListView);
    }


    public void setListViewHeightBaseOnChildern(ListView listView) {
        if(listView == null){
            return;
        }
        ListAdapter listAdapter = listView.getAdapter();
        if(listAdapter == null){
            return;
        }
        int totalHeight = 0;
        for(int i = 0; i < listAdapter.getCount(); i++){
            View listItem = listAdapter.getView(i,null,listView);
            listItem.measure(0,0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() -1));
        listView.setLayoutParams(params);

    }
}
<file_sep>/app/src/main/java/com/example/dengjx/verticalrecylerviewselector/RulerRecylercView.java
package com.example.dengjx.verticalrecylerviewselector;

import android.content.Context;
import android.content.Loader;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;

/**
 * Created by dengjx on 2017/12/29.
 */

public class RulerRecylercView extends RecyclerView {
    private static final String TAG = "RulerRecylercView";
    // 偏移
    private int deltay;
    private RulerView mRulerView;
    private int avagerNum;
    private int leftMargin = 50;
    private int revleftMargin = -50;
    // 屏幕的宽度
    private int mScreenWidth;
    // 当前定位的数字刻度值
    private int mCurNum;
    private onReturnNum onreturnNum;
    int curDeltay;

    public RulerRecylercView(Context context) {
        this(context, null);
    }

    boolean isFinish;
    private Context context;
    public RulerRecylercView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        mScreenWidth = displayMetrics.widthPixels;
        Log.d(TAG, "mScreenWidth:" + mScreenWidth);
        avagerNum = (mScreenWidth - leftMargin * 2) / 9;
        Log.d(TAG, "avagerNum:" + avagerNum);
        curDeltay = mScreenWidth / 2;
    }

    public void setOnReturnNum(onReturnNum onReturnNum) {
        this.onreturnNum = onReturnNum;
    }


    public interface onReturnNum {
        void onNum(int num);
    }

    @Override
    public void onScrollStateChanged(int state) {
        super.onScrollStateChanged(state);

        if (state == SCROLL_STATE_IDLE) {

            Log.d(TAG, "delaty:" + deltay);
            mCurNum = ((deltay - leftMargin) / avagerNum );
            Log.d(TAG, "(deltay - leftMargin )/ avagerNum :" + ((deltay - leftMargin) / avagerNum ) * avagerNum);
            int mscroll = ((deltay - leftMargin) / avagerNum ) * avagerNum - deltay + leftMargin + dptopx()/2 ;
            Log.d(TAG, "mscroll:" + mscroll);
            Log.d(TAG, "mCurNum:" + mCurNum);
            //判断是否大于3分之2则刻度直接加1
            if(mCurNum > 0 && mCurNum < 9) {
                if (Math.abs(mscroll) >= (avagerNum * 2 / 3)) {
                    mCurNum = mCurNum + 1;
                    mscroll = avagerNum - Math.abs(mscroll);
                }
            }
            Log.d(TAG, "mCurNum:" + mCurNum);
            onreturnNum.onNum(mCurNum);
            scrollBy(mscroll, 0);
            getAdapter().notifyDataSetChanged();

        }
    }

    @Override
    public void onScrolled(int dx, int dy) {
        super.onScrolled(dx, dy);
        deltay = deltay + dx;
    }
    // dp转px 4为中间指针的宽度
    public int dptopx(){
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (4 * scale + 0.5f);
    }


}
<file_sep>/app/src/main/java/com/example/dengjx/verticalrecylerviewselector/RulerViewAdapter.java
package com.example.dengjx.verticalrecylerviewselector;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by dengjx on 2017/12/27.
 */

public class RulerViewAdapter extends RecyclerView.Adapter {
    private static final String TAG ="RulerViewAdapter";
    private Context context;
    private final static int TYPE_ITEM = 1;
    private final static int TYPE_HEAD_FOOT =2;

    public RulerViewAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
        if(position == 0 || position == getItemCount() -1){
            return TYPE_HEAD_FOOT;
        }else {
            return TYPE_ITEM;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == TYPE_HEAD_FOOT){
            View view = new View(context);
            int headerFooterWidth = parent.getMeasuredWidth() / 2  ;
            Log.d(TAG,"headerFooterWidth:"+headerFooterWidth);
            RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(headerFooterWidth, 100);
            view.setLayoutParams(params);
            return new HeadViewHodler(view);
        }else {
            RulerView rulerview = new RulerView(context);
            RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    300);
            rulerview.setLayoutParams(params);
            Log.d(TAG,"params.width:"+params.width);
            Log.d(TAG,"params.height:"+params.height);
            return new itemViewHolder(rulerview);
        }


    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        return 3;
    }

    class HeadViewHodler extends RecyclerView.ViewHolder{

        public HeadViewHodler(View itemView) {
            super(itemView);
        }
    }
    class itemViewHolder extends RecyclerView.ViewHolder{

        public itemViewHolder(View itemView) {
            super(itemView);
        }
    }
}
<file_sep>/app/src/main/java/com/example/dengjx/verticalrecylerviewselector/MainActivity.java
package com.example.dengjx.verticalrecylerviewselector;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

public class MainActivity extends AppCompatActivity implements VerticalRecyclerviewAdapter.OnItemClick {
    AutoLocateHorizontalView mRecycleView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecycleView = (AutoLocateHorizontalView) findViewById(R.id.recyclerview);
        VerticalRecyclerviewAdapter verticalRecyclerviewAdapter = new VerticalRecyclerviewAdapter(this);
        verticalRecyclerviewAdapter.setOnItemClick(this);
        if(null != verticalRecyclerviewAdapter){
            mRecycleView.setInitPos(1);
            mRecycleView.setAdapter(verticalRecyclerviewAdapter);
        }
    }

    @Override
    public void onItemClickListner(int postion) {
            mRecycleView.updateData(postion);
    }
}
<file_sep>/app/src/main/java/com/example/dengjx/verticalrecylerviewselector/FootViewAdapter.java
package com.example.dengjx.verticalrecylerviewselector;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

/**
 * Created by dengjx on 2017/12/29.
 */

public class FootViewAdapter extends BaseFootViewAdapter<StationImage>{

    public FootViewAdapter(Context context, ListView listView) {
        super(context, listView);
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ItemHoder itemHoder = null;
        if(view == null){
            itemHoder = new ItemHoder();
            view = inflater.inflate(R.layout.item_list,viewGroup,false);
            itemHoder.mName = view.findViewById(R.id.name);
            itemHoder.mContent = view.findViewById(R.id.content);
            view.setTag(itemHoder);
        }else {
            itemHoder = (ItemHoder) view.getTag();
        }
        StationImage stationImage = getItem(i);
        Log.d("test","stationImage.getName():" +stationImage.getName());
        Log.d("test","itemHoder.mName:"+itemHoder.mName);
        itemHoder.mName.setText(stationImage.getName());
        itemHoder.mContent.setText(stationImage.getContent());
        return view;
    }

    class ItemHoder{
        TextView mName;
        TextView mContent;
    }
}
<file_sep>/app/src/main/java/com/example/dengjx/verticalrecylerviewselector/RulerActivity.java
package com.example.dengjx.verticalrecylerviewselector;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import static com.example.dengjx.verticalrecylerviewselector.RulerRecylercView.*;

/**
 * Created by dengjx on 2017/12/26.
 */

public class RulerActivity extends Activity implements onReturnNum {
    private static final String TAG = "RulerActivity" ;
    RulerRecylercView mRulerView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ruleract);
        mRulerView = findViewById(R.id.rulerview);
        RulerViewAdapter rulerViewAdapter = new RulerViewAdapter(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRulerView.setLayoutManager(linearLayoutManager);
        mRulerView.setAdapter(rulerViewAdapter);
        mRulerView.setOnReturnNum(this);
        mRulerView.smoothScrollToPosition(1);


    }

    @Override
    public void onNum(int num) {
        Log.d(TAG, "NUM :" +num);
    }
}
