1. 程式人生 > >使用sticky-headers-recyclerview 完成 recyclerview粘性頭部

使用sticky-headers-recyclerview 完成 recyclerview粘性頭部

二. 使用方式

首先是xml  
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout         xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows
="true">
<android.support.v7.widget.RecyclerView android:id="@+id/RecyclerView" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" android:scrollbars="none" /> </RelativeLayout
>
Activity 初始化 RecyclerView
public class WithLookPlanActivity extends Activity {

    private StickyRecyclerHeadersDecoration topStickyHeadersItemDecoration;
    private RecyclerView recyclerView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        initView();
    }


    private
void initView() { recyclerView = (RecyclerView) findViewById(R.id.withLookRv); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerViewAdapter adapter= new recyclerViewAdapter(dataList); recyclerView.setAdapter(recyclerViewAdapter); topStickyHeadersItemDecoration = new StickyRecyclerHeadersDecoration(recyclerViewAdapter); //設定recyclerView需要的Decoration }
RecyclerViewAdapter -- 繫結head
public class WithLookPlanAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements StickyRecyclerHeadersAdapter<RecyclerView.ViewHolder> {
//繼承並實現StickyRecyclerHeadersAdapter

    private List<dataList> dataList;

    public WithLookPlanAdapter(List<WithLookPlansListData> dataList) {
        this.dataList = dataList;
        setHasStableIds(true);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent, false);
        return new MyViewHolder(itemView);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        final MyViewHolder myViewHolder = (MyViewHolder) holder;
        if (position >= dataList.size()) {
            return;
        }

        String name = dataList.get(position).name;
        String age = dataList.get(position).age

    }


    @Override
    public int getItemCount() {
        return dataList.size();  
    }

    //必須重寫  不然item會錯亂
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_header_view, parent, false);
        return new MyHeaderViewHolder(itemView);
    } //headView 

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        MyHeaderViewHolder myHeaderViewHolder = (MyHeaderViewHolder) viewHolder;
        myHeaderViewHolder.title.setText(dataList.get(position).name);  //設定head資料
    }


    @Override
    public long getHeaderId(int position) {
        long headName = dataList.get(position).name.hashCode();

        return headName;
        //得到headId 唯一性
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView nameTv;
        public TextView ageTv;

        public MyViewHolder(View view) {
            super(view);
            nameTv = (TextView) view.findViewById(R.id.nameTv);
            ageTv = (TextView) view.findViewById(R.id.ageTv);
        }
    }// item

    public static class MyHeaderViewHolder extends RecyclerView.ViewHolder {

        TextView title;

        public MyHeaderViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.HeadTv);
        } //head 
    }