android_recyclerView实现指示器效果

效果图:

android_recyclerView实现指示器效果_第1张图片

layout:



    
        
        
        
    

item:



    

java:

public class IndicatorActivity extends SwipeBackActivity {


    @BindView(R.id.iv_imgLeft)
    ImageView ivImgLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recyc)
    RecyclerView recyc;

    private int indicatorTag;
    private IndicatorAdapter indicatorAdapter;
    private List indicatorList= Arrays.asList("头条","头条","头条","头条","头条","头条","头条","头条","头条","头条","头条","头条","头条","头条","头条","头条");//数据源

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_indicator);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        ivImgLeft.setImageResource(R.drawable.left);
        ivImgLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvTitle.setText("indicator");

        indicatorAdapter=new IndicatorAdapter();//创建适配器对象
        recyc.setAdapter(indicatorAdapter);//recyclerView加载适配器
	//布局管理器
        LinearLayoutManager lin=new LinearLayoutManager(this);
        lin.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyc.setLayoutManager(lin);
    }


	//创建recyclerView 的适配器 内部类构造方法就没有创建
    private class IndicatorAdapter extends RecyclerView.Adapter{
	//创建viewHolder
        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyViewHolder(LayoutInflater.from(IndicatorActivity.this).inflate(R.layout.item_indicator,parent,false));
        }
	//绑定数据
        @Override
        public void onBindViewHolder(MyViewHolder holder, final int position) {
	//绑定数据
            holder.name.setText(indicatorList.get(position));
	//判断是否为第一个 如果是第一个颜色为蓝色 不是第一个颜色为黑色  初始化属性(成员变量)时没有添加值系统会自动添加一个初始化值(0)
            if(position==indicatorTag){
		//getResources().getColor(R.color.blue) 这种获取颜色也行 但是画横线了 尽量别用
                holder.name.setTextColor(ContextCompat.getColor(IndicatorActivity.this, R.color.blue));
            }else{
                holder.name.setTextColor(ContextCompat.getColor(IndicatorActivity.this, R.color.black));
            }
	//点击事件这里是关键 获取点击的position(位置) 赋值给indicatorTag 刷新adapter
            holder.view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    indicatorTag=position;
                    indicatorAdapter.notifyDataSetChanged();
                }
            });
        }
	//获取item的个数
        @Override
        public int getItemCount() {
            return indicatorList.size();
        }
	//创建viewholder
        public class MyViewHolder extends RecyclerView.ViewHolder {
            private TextView name;
            private View view;
            public MyViewHolder(View itemView) {
                super(itemView);
                view=itemView;
                name=itemView.findViewById(R.id.name);
            }
        }

    }



}

你可能感兴趣的:(android_recyclerView实现指示器效果)