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{}
08-20
887
05-24
6922
11-20
1875