如何实现一个横向的recyclerview?

public   class MainActivity extends AppCompatActivity implements RecyAdapter.OnItemClickListener   {
private String TAG="HorizontalActivity";
private Integer[] mImgIds = {R.mipmap.weather,   R.mipmap.ic_launcher, R.mipmap.weather, R.mipmap.ic_launcher,   R.mipmap.weather,
R.mipmap.weather,
R.mipmap.ic_launcher_round,   R.mipmap.ic_launcher, R.mipmap.weather,
R.mipmap.ic_launcher_round, R.mipmap.weather};
private List   datas;
private RecyAdapter   recyAdapter;
private Handler mHandler=new Handler();
private LinearLayoutManager   layoutManager;
private int oldItem=0;
private RecyclerView   recyclerview;
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState)   {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView)   findViewById(R.id.img);
recyclerview = (RecyclerView)   findViewById(R.id.recyclerview);
initData();
initRecy();
img.setImageResource(datas.get(0));
recyAdapter.setOnItemClickListener(this);
}
private void initRecy() {
recyAdapter=new RecyAdapter(this,datas);
layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerview.setLayoutManager(layoutManager);
recyclerview.setAdapter(recyAdapter);
}
private void initData() {
datas=new ArrayList<>();
for (int i   = 0; i   0){
//                  oldItem=firstItem;
//                  img.setImageResource(datas.get(oldItem%datas.size()));
//              }
//
//              Log.e(TAG, "run: firstItem:"+firstItem   );
//
//
//              mHandler.postDelayed(scrollRunnable,10);
//          }
//      };
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
@Override
protected void onResume() {
super.onResume();
//          mHandler.postDelayed(scrollRunnable,10);
}
@Override
protected void onStop() {
super.onStop();
//          mHandler.removeCallbacks(scrollRunnable);
}
@Override
public void onItemClick(View view, int tag) {
//          img.setImageResource(datas.get(tag));//设置图片
Toast.makeText(this,"第"+tag+"张图片被点击了",Toast.LENGTH_SHORT).show();
}
}
2.RecyAdapter
public class RecyAdapter extends RecyclerView.Adapter   implements View.OnClickListener   {
private Context   context;
private List   datas;
private OnItemClickListener onItemClickListener;
public RecyAdapter(Context context,   List datas) {
this.context = context;
this.datas = datas;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup   parent, int viewType) {
View view =   LayoutInflater.from(context).inflate(R.layout.item_horizontal, parent,   false);
ViewHolder vh=new ViewHolder(view);
view.setOnClickListener(this);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
int newPos=position%datas.size();
holder.img.setImageResource(datas.get(newPos));
holder.itemView.setTag(position);
}
@Override
public int getItemCount() {
return datas.size();
}//Integer.MAX_VALUE
@Override
public void onClick(View view) {
if(onItemClickListener!=null){
onItemClickListener.onItemClick(view,   (Integer) view.getTag());
}
}
class ViewHolder extends RecyclerView.ViewHolder   {
ImageView   img;
public ViewHolder(View itemView) {
super(itemView);
img=itemView.findViewById(R.id.img);
}
}
public void setOnItemClickListener(OnItemClickListener   listener){
this.onItemClickListener=listener;
}
interface OnItemClickListener{}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值