Android中featuredrecyclerview的使用

featuredrecyclerview是自定義ViewGroup的reyclerview延伸了。它具有的第一個item是在頂部(通過設置高度爲featureditemheight)。 感覺很好!

https://github.com/developer-shivam/FeaturedRecyclerView

效果圖:

這裏寫圖片描述

添加依賴:

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地址:

http://download.csdn.net/detail/afanbaby/9877639

本人菜鳥一個,有什麼不對的地方希望大家指出評論,大神勿噴,希望大家一起學習進步!

發佈了132 篇原創文章 · 獲贊 174 · 訪問量 53萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章