利用了RecyclerView来滑动选择,封装在了group里
JAVA:
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.outsite.R;
public class PageChangeList extends ViewGroup {
private int total=0,pages=0,current=0;
private ImageView last=new ImageView(getContext());
private ImageView next=new ImageView(getContext());
private RecyclerView re=new RecyclerView(getContext());
private Context context;
public PageChangeList(Context context) {
super(context);
this.context=context;
}
public PageChangeList(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.context=context;
}
public PageChangeList(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context=context;
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
int mViewGroupWidth = getMeasuredWidth(); //当前ViewGroup的总宽度
int mViewGroupHeight = getMeasuredHeight(); //当前ViewGroup的总高度
last.layout(0,0,last.getMeasuredWidth(),mViewGroupHeight);
re.layout(last.getMeasuredWidth(),0,mViewGroupWidth-next.getMeasuredWidth(),mViewGroupHeight);
next.layout(mViewGroupWidth-next.getMeasuredWidth(),0,mViewGroupWidth,mViewGroupHeight);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureChildren(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(widthMeasureSpec,100);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
public void setPageInfo(int total,int pages,int current){
this.total=total;
this.pages=pages;
this.current=current;
}
public void show(){
removeAllViews();
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(40,40);
last.setLayoutParams(layoutParams);
next.setLayoutParams(layoutParams);
last.setImageDrawable(getResources().getDrawable(R.drawable.lastblue));
next.setImageDrawable(getResources().getDrawable(R.drawable.nextblue));
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
layoutManager.setOrientation(RecyclerView.HORIZONTAL);
re.setLayoutManager(layoutManager);
re.scrollToPosition(current-1);
PageListAdapter adapter=new PageListAdapter(getContext(),total,pages,current);
re.setAdapter(adapter);
adapter.notifyDataSetChanged();
/* LinearLayout linearLayout=new LinearLayout(getContext());
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setGravity(Gravity.CENTER);
linearLayout.addView(last);
linearLayout.addView(re);
linearLayout.addView(next);
addView(linearLayout);*/
this.addView(last);
this.addView(re);
this.addView(next);
}
public void setlast(OnClickListener onClickListener){
last.setOnClickListener(onClickListener);
}
public void setnext(OnClickListener onClickListener){
next.setOnClickListener(onClickListener);
}
OnClickListener numclick;
public void setNum(OnClickListener onClickListener){
this.numclick=onClickListener;
}
public class PageListAdapter extends RecyclerView.Adapter<PageListAdapter.ViewHolder>{
Context context;
private int total,pages,current;
public PageListAdapter(Context context, int total, int pages, int current) {
this.context=context;
this.total=total;
this.pages=pages;
this.current=current;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.page_list,viewGroup,false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.num.setText((position+1)+"");
if(position+1==current){
holder.num.setTextColor(Color.RED);
}
holder.num.setOnClickListener(numclick);
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getItemCount() {
return pages;
}
class ViewHolder extends RecyclerView.ViewHolder{
TextView num;
public ViewHolder(View itemView) {
super(itemView);
num=itemView.findViewById(R.id.num);
}
}
}
}
其中图片是向左向右的两张小图。随便填俩吧
XML:就一个Linear和一个Text
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/pagelistlayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:gravity="center"
android:id="@+id/num"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
使用的话:
private PageChangeList pageChangeList;
pageChangeList.setPageInfo(total,pages,current);
pageChangeList.show();
pageChangeList.setlast(new View.OnClickListener() {
@Override
public void onClick(View v) {
请求下一页数据
}
});
pageChangeList.setnext(new View.OnClickListener() {
@Override
public void onClick(View v) {
请求上一页
}
});
pageChangeList.setNum(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView textView= (TextView) v;
int c= Integer.parseInt(textView.getText().toString());
请求某一页数据
});
|