Android App开发中使用RecyclerView实现Gallery画廊的实例

2019-12-10 18:27:32于海丽

很简单,创建一个接口,提供一个设置入口,然后在onBindViewHolder中判断即可。
最后在主Activity中设置监听:

mAdapter = new GalleryAdapter(this, mDatas); 
mAdapter.setOnItemClickLitener(new OnItemClickLitener() 
{ 
  @Override 
  public void onItemClick(View view, int position) 
  { 
    Toast.makeText(MainActivity.this, position+"", Toast.LENGTH_SHORT) 
        .show(); 
  } 
}); 
mRecyclerView.setAdapter(mAdapter); 

好了,这样就行了,看效果图:

Android,App开发,RecyclerView,Gallery

效果还是不错的,接下来我想改成相册效果,即上面显示一张大图,下面的RecyclerView做为图片切换的指示器。
3、自定义RecyclerView实现滚动时内容联动
首先修改下布局:
布局文件:

<LinearLayout xmlns:android="http://www.easck.com/apk/res/android" 
  xmlns:tools="http://www.easck.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:orientation="vertical" > 
 
  <FrameLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" > 
 
    <ImageView 
      android:id="@+id/id_content" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_gravity="center" 
      android:layout_margin="10dp" 
      android:scaleType="centerCrop" 
      android:src="@drawable/ic_launcher" /> 
  </FrameLayout> 
 
  <com.example.zhy_horizontalscrollview03.MyRecyclerView 
    android:id="@+id/id_recyclerview_horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="120dp" 
    android:layout_gravity="bottom" 
    android:background="#FF0000" 
    android:scrollbars="none" /> 
 
</LinearLayout>