featuredrecyclerview是自定义ViewGroup的reyclerview延伸了。它具有的第一个item是在顶部(通过设置高度为featureditemheight)。 感觉很好!
效果图:
添加依赖:
compile 'com.github.developer-shivam:FeaturedRecyclerView:1.0.0'
xml中使用:
<shivam.developer.featuredrecyclerview.FeaturedRecyclerView
android:id="@+id/featuredRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultItemHeight="120dp"
app:featuredItemHeight="300dp" />
在Activity中使用:
public class MainActivity extends AppCompatActivity {
private FeaturedRecyclerView featuredRecyclerView;
private CustomRecyclerViewAdapter customRecyclerViewAdapter;
private List<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = new ArrayList<>();
featuredRecyclerView = (FeaturedRecyclerView) findViewById(R.id.featuredRecyclerView);
//你必须使用featuredlinearlayoutmanager避免闪烁。
FeatureLinearLayoutManager featureLinearLayoutManager = new FeatureLinearLayoutManager(this);
featuredRecyclerView.setLayoutManager(featureLinearLayoutManager);
for (int i = 0; i < 30; i++) {
list.add("item " + i);
}
customRecyclerViewAdapter = new CustomRecyclerViewAdapter(this, list);
featuredRecyclerView.setAdapter(customRecyclerViewAdapter);
}
}
CustomRecyclerViewAdapter中:
public class CustomRecyclerViewAdapter extends FeatureRecyclerViewAdapter<CustomRecyclerViewAdapter.CustomRecyclerViewHolder> {
private List<String> dataList;
private Context context;
private int[] images = new int[5];
public CustomRecyclerViewAdapter(Context context, List<String> list) {
this.dataList = list;
this.context = context;
images[0] = R.drawable.image_one;
images[1] = R.drawable.image_three;
images[2] = R.drawable.image_two;
images[3] = R.drawable.image_four;
images[4] = R.drawable.image_five;
}
@Override
public CustomRecyclerViewHolder onCreateFeaturedViewHolder(ViewGroup parent, int viewType) {
return new CustomRecyclerViewHolder(
LayoutInflater.from(parent.getContext())
.inflate(R.layout.simple_reycler_view_layout, parent, false));
}
@Override
public void onBindFeaturedViewHolder(CustomRecyclerViewHolder holder, int position) {
Picasso.with(context)
.load(images[position % 4]).into(holder.ivBackground);
holder.tvHeading.setText(dataList.get(position));
}
@Override
public int getFeaturedItemsCount() {
return dataList.size();
}
//使用featuredrecyclerviewadapter优点在于它包含两个以上的方法(onSmallItemResize、onBigItemResize)可以用来制作动画的属性的属性
@Override
public void onSmallItemResize(CustomRecyclerViewHolder holder, int position, float offset) {
holder.tvHeading.setAlpha(offset / 100f);
}
@Override
public void onBigItemResize(CustomRecyclerViewHolder holder, int position, float offset) {
holder.tvHeading.setAlpha(offset / 100f);
}
public static class CustomRecyclerViewHolder extends RecyclerView.ViewHolder {
ImageView ivBackground;
TextView tvHeading;
public CustomRecyclerViewHolder(View itemView) {
super(itemView);
ivBackground = (ImageView) itemView.findViewById(R.id.iv_background);
tvHeading = (TextView) itemView.findViewById(R.id.tv_heading);
}
}
}
simple_reycler_view_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/shadow_inverse" />
<TextView
android:id="@+id/tv_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/text_background"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:textColor="@android:color/white"
android:textSize="20sp" />
</RelativeLayout>
demo地址:
本人菜鸟一个,有什么不对的地方希望大家指出评论,大神勿喷,希望大家一起学习进步!