本文将带你了解Android应用开发Andriod自定义RecyclerView实现Gallery效果横向滑动,希望本文对大家学Android有所帮助。

Andriod自定义RecyclerView实现Gallery效果横向滑动。

大家好,一个RecyclerView   横向滑动的效果是不是值得借鉴呢,下面我们来看看我做的效果:

下面是需求的效果图:

因为需要做一个天气的界面效果,所以之前没有经验的我不得不搜索资料来实现,下面我们来看看代码吧:

1.Activity

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{
&nbs