布局
布局代码 <LinearLayout android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recycle" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> <RelativeLayout android:id="@+id/parent_layout" android:layout_gravity="center" android:layout_width="100dp" android:layout_height="5dp" android:background="@drawable/bg_fast_view"> <View android:id="@+id/main_line" android:layout_width="30dp" android:layout_height="5dp" android:background="@drawable/bg_scroll_bar" android:layout_centerVertical="true"/> </RelativeLayout> </LinearLayout>
adapter代码 主要是设置item的宽度。 public class IndexMenuAdapter extends RecyclerView.Adapter<IndexMenuAdapter.ViewHolder> { private List<MenuBean.DataEntity> data=new ArrayList<>(); private Context mContext; private int width; public IndexMenuAdapter(Context context) { mContext=context; width = (ScreenUtils.getScreenW(context) - ScreenUtils.dip2px(context,20)) / 5; } public List<MenuBean.DataEntity> getData() { return data; } public void setData(List<MenuBean.DataEntity> data) { this.data = data; notifyDataSetChanged(); } @Override public IndexMenuAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View contentView = LayoutInflater.from(parent.getContext()).inflate(R.layout.index_menu_item, parent, false); IndexMenuAdapter.ViewHolder viewHolder = new IndexMenuAdapter.ViewHolder(contentView); ViewGroup.LayoutParams params = contentView.getLayoutParams(); params.width = width; contentView.setLayoutParams(params); return viewHolder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { RequestOptions options=new RequestOptions(); options.placeholder(R.mipmap.index_zwt_icon); options.error(R.mipmap.index_zwt_icon); Glide.with(mContext).load(data.get(position).getImg()).apply(options).into(holder.icon); holder.name.setText(data.get(position).getName()); } @Override public int getItemCount() { if (data!=null) { return data.size(); }else{ return 0; } } public class ViewHolder extends RecyclerView.ViewHolder { private ImageView icon; private TextView name; public ViewHolder(View itemView) { super(itemView); icon = (ImageView) itemView.findViewById(R.id.index_menu_icon); name = (TextView) itemView.findViewById(R.id.index_menu_name); } } }
滑动主要代码 mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); //整体的总宽度,注意是整体,包括在显示区域之外的。 int temp=mRecyclerView.computeHorizontalScrollRange(); if (temp>range){ range=temp; } int offset = mRecyclerView.computeHorizontalScrollOffset(); int extent = mRecyclerView.computeHorizontalScrollExtent(); float proportion = (float) (offset*1.0 / (range-extent)); //计算滚动条宽度 float transMaxRange = mLayout.getWidth() - mLineView.getWidth(); //设置滚动条移动 mLineView.setTranslationX(transMaxRange * proportion); Log.e("scrll",transMaxRange+"---"+range+"---"+"---"+proportion+"---"+offset+"----"+extent); } });