android recyclerview 横向移动,Android -RecyclerView 横向滑动 且嵌套索引表

1.实现该功能需要在Eclipse工具的SDK路径下添加 android-5.0,1 android-5.1 android- 5.1.1版本,同时需要引用android-support-v7-appcompat和recycleview库文件。

2.布局文件代码:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#000000"

android:orientation="vertical" >

android:id="@+id/body"

android:layout_width="match_parent"

android:layout_height="344dp" >

android:id="@+id/recycler_view"

android:layout_width="match_parent"

android:layout_height="344dp"

android:layout_gravity="center_vertical"

android:layout_marginLeft="8dp" >

android:id="@+id/sidrbar"

android:layout_width="match_parent"

android:layout_height="48dp"

android:layout_below="@+id/body"

android:layout_gravity="center"

android:layout_marginLeft="3dp"

android:layout_marginTop="15dp"

android:orientation="horizontal" />

3.自定义SideBar代码如下:

public class SideBar  extends View{

private static final String TAG = "SideBar";

private static String minue="A";

/**

* 触摸事件

*/

private ITouchingLetterChangedListener onTouchingLetterChangedListener;

/**

* 侧边栏显示字母

*/

private String[] words = { "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 choose = -1;

private int chooseup=-1;

/**

* 相应的画笔

*/

private Paint paint;

/**

* 构造函数 数据初始化

* @param context 上下文对象

* @param attrs   属性列表

* @param defStyleAttr 默认样式

*/

public SideBar(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

init();

}

/**

* 初始化相应的数据

*/

private void init() {

paint = new Paint();

}

/**

* 构造函数 数据初始化

* @param context 上下文对象

* @param attrs  属性列表

*/

public SideBar(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

/**

* 构造函数 数据初始化

* @param context  上下文对象

*/

public SideBar(Context context) {

this(context, null);

}

/**

* 绘制列表控件的方法

* 将要绘制的字母以从上到下的顺序绘制在一个指定区域

* 如果是进行选中的字母就进行高亮显示

*/

@Override

protected void onDraw(Canvas canvas) {

// TODO Auto-generated method stub

super.onDraw(canvas);

int height = getHeight();// 获取对应高度

int width = getWidth(); // 获取对应宽度

int singleWidth = width / words.length;// 获取每一个字母的宽度

for (int i = 0; i < words.length; i++) {

paint.setColor(getResources().getColor(R.color.indexadc));

// paint.setColor(Color.WHITE);

paint.setTypeface(Typeface.DEFAULT_BOLD);

paint.setTextSize(20f);

// 选中的状态

if (i == choose) {

chooseup=choose;

paint.setColor(getResources().getColor(R.color.indexabc));

paint.setFakeBoldText(true);

}

// x坐标等于中间-字符串宽度的一半.

//float yPos = height / 2.0f - paint.measureText(words[i]) / 2.0f;

float yPos = height / 2.0f - paint.measureText(words[0]) / 2.0f;

float xPos = singleWidth * i + singleWidth/2;

if (words[i]=="I") {  //解决当绘制字母I时会左右距离不同问题

xPos = singleWidth * i + singleWidth/2+paint.measureText(words[0]) / 2.0f;

}

canvas.drawText(words[i], xPos, yPos, paint);

paint.reset();// 重置画笔

}

/*if(isup==true){

paint.setColor(getResources().getColor(R.color.indexabc));

paint.setFakeBoldText(true);

paint.setTypeface(Typeface.DEFAULT_BOLD);

paint.setTextSize(20f);

// x坐标等于中间-字符串宽度的一半.

float yPos = height / 2.0f - paint.measureText(words[chooseup]) / 2.0f;

float xPos = singleWidth * chooseup + singleWidth/2;

if (words[chooseup]=="I") {  //解决当绘制字母I时会左右距离不同问题

xPos = singleWidth * chooseup + singleWidth/2+paint.measureText(words[0]) / 2.0f;

}

float yPos = height / 2.0f - paint.measureText(words[0]) / 2.0f;

float xPos = singleWidth * chooseup + singleWidth/2;

if (words[chooseup]=="I") {  //解决当绘制字母I时会左右距离不同问题

xPos = singleWidth * chooseup + singleWidth/2+paint.measureText(words[0]) / 2.0f;

}

//            float yPos = height / 2 - paint.measureText(words[chooseup]) / 2;

//            float xPos = singleWidth * chooseup + singleWidth;

canvas.drawText(words[chooseup], xPos, yPos, paint);

paint.reset();// 重置画笔

*/        //}

/*if(isshow==true){

paint.setColor(getResources().getColor(R.color.indexabc));

paint.setFakeBoldText(true);

paint.setTypeface(Typeface.DEFAULT_BOLD);

paint.setTextSize(20f);

// x坐标等于中间-字符串宽度的一半.

float yPos = height / 2 - paint.measureText(minue) / 2;

float xPos = singleWidth * chooseup + singleWidth;

canvas.drawText(minue, xPos, yPos, paint);

paint.reset();// 重置画笔

}*/

}

boolean isup=false;

//boolean isshow=false;

/**

* 处理触摸事件的方法

* 用户按下时候,整个控件背景变化

* 根据按下x坐标 判断究竟用户按下那个字母

* 当前字母高亮显示 将其字母显示屏幕中央

*/

@SuppressWarnings("deprecation")

@Override

public boolean dispatchTouchEvent(MotionEvent event) {

int action = event.getAction();

final float x = event.getX();// 点击x坐标

final int oldChoose = choose;

final ITouchingLetterChangedListener listener = onTouchingLetterChangedListener;

final int c = (int) (x / getWidth() * words.length);// 点击x坐标所占总宽度的比例*b数组的长度就等于点击b中的个数.

switch (action) {

case MotionEvent.ACTION_UP:

isup=true;

FragmentContacts.hideADCtip();

setBackgroundDrawable(new ColorDrawable(0x00000000));

choose = -1;//

invalidate();

break;

default:

isup=false;

//setBackgroundResource(R.drawable.sidebar_background);

if (oldChoose != c) {

if (c >= 0 && c < words.length) {

if (listener != null) {

listener.OnTouchingLetterChanged(words[c]);

}

choose = c;

invalidate();

}

}

break;

}

return true;

}

//listView滑动可见第一个字母

public void setPosition(String position){

Log.i(TAG, "position"+position);

//isshow=true;

minue=position;

}

/**

* 接口

*/

public interface ITouchingLetterChangedListener {

void OnTouchingLetterChanged(String cString);

}

/**

* 字母改变的监听器

* @return  获取字母改变的监听器

*/

public ITouchingLetterChangedListener getOnTouchingLetterChangedListener() {

return onTouchingLetterChangedListener;

}

/**

* 设置改变的监听器

* @param onTouchingLetterChangedListener 设置字母改变的监听器

*/

public void setOnTouchingLetterChangedListener(

ITouchingLetterChangedListener onTouchingLetterChangedListener) {

this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;

}

}

4.获取 RecyclerView第一个可见View和最后一个可见的View代码:

mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {

@Override

public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

super.onScrolled(recyclerView, dx, dy);

}

@Override

public void onScrollStateChanged(RecyclerView recyclerView,

int newState) {

super.onScrollStateChanged(recyclerView, newState);

RecyclerView.LayoutManager layoutManager = recyclerView

.getLayoutManager();

// 判断是当前layoutManager是否为LinearLayoutManager

// 只有LinearLayoutManager才有查找第一个和最后一个可见view位置的方法

if (layoutManager instanceof LinearLayoutManager) {

LinearLayoutManager linearManager = (LinearLayoutManager) layoutManager;

// 获取最后一个可见view的位置

int lastItemPosition = linearManager

.findLastVisibleItemPosition();

// 获取第一个可见view的位置

int firstItemPosition = linearManager

.findFirstVisibleItemPosition();

String a = mContactsList.get(firstItemPosition)

.getSortLetters();

sideBar.setPosition(a);

}

}

});

5.加载联系人代码和滑动监听索引表事件代码:

/**

* 加载所有联系人(仅限主线程中调用)

*/

private void showAllContacts() {

// 不要在子线程中直接修改mContanctsList里的内容

DataManager.getInstance().getAllContacts(mContactsList);

layoutManager = new LinearLayoutManager(context);

// 设置显示的方向

layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);

mRecyclerView.setLayoutManager(layoutManager);

mListAdapter = new ListAdapter(context, mContactsList);

// 设置适配器 mListAdapter ---> List

mRecyclerView.setAdapter(mListAdapter);

// 更新完后,设置回到第一项

if (mListAdapter.getItemCount() > 0) {

layoutManager.scrollToPosition(0);

}

// 设置字母索引表被触摸时的监听

sideBar.setOnTouchingLetterChangedListener(new ITouchingLetterChangedListener() {

@Override

public void OnTouchingLetterChanged(String selectedChar) {

// 该字母首次出现的位置

int position = mListAdapter.getPositionForSection(selectedChar

.charAt(0));

if (!selectedChar.equals("")) {

index.setVisibility(View.VISIBLE);

}

Log.i("TAG", "position=" + position + ",被点击的字母: selectedChar="

+ selectedChar);

if (position != -1) {

// 跳转到指定的item

layoutManager.scrollToPositionWithOffset(position, 0);

index.setText(selectedChar);

}

}

});

}

6.根据分类的首字母的Char ascii值获取其第一个出现该首字母的位置代码:

@Override

publicintgetPositionForSection(intsection) {

for(inti = 0; i < getItemCount(); i++) {

String sortStr = mContactsList.get(i).getSortLetters();

charfirstChar = sortStr.toUpperCase().charAt(0)

if(firstChar == section)

returi;

}

return

7.RecycleView点击监听代码:

view.setOnClickListener (newOnClickListener

@Override

publicvoidonClick(View v) {

if(mContactsList != null&& mContactsList.size()!=0) {

Contacts contacts = mContactsList.get(getPosition());

if(contacts != null) {

// 点击直接拨号

DataManager.getInstance().Call(contacts.getNum())

}

}

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值