RecycleView不同类型Item&添加HeaderView&条目点击事件

先来添加个头玩玩

这里写图片描述
实现方式

 GridLayoutManager manager = new GridLayoutManager(getApplicationContext(), 2);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            //如果是第一行就设一个条目占一行,如果不是第一行,就设定普通条目着一行;
            @Override
            public int getSpanSize(int position) {
                return position == 0 ? 2 : 1;
            }
        });
        manager.setOrientation(LinearLayoutManager.VERTICAL);

布局文件(主布局)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.oblivion.recycleview.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycleview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</RelativeLayout>

两个子Item的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/iv_recycle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:layout_marginLeft="10dp"
        android:id="@+id/tv_type2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="我是测试类型" />
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#660f0f0f"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_type1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="ccc"
        android:textColor="#fff"
        android:textSize="20sp" />
</LinearLayout>

这里写图片描述
为了方便我将所有逻辑放在了Adpater中,没有再抽取

package com.oblivion.recycleview.adpater;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.oblivion.recycleview.R;

import java.util.List;

/**
 * github : https://github.com/oblivion0001/AndroidStudioProjects
 * Blog : http://blog.csdn.net/qq_16666847
 * Created by oblivion on 2016/11/30.
 */
public class MyAdaper extends RecyclerView.Adapter<MyAdaper.ViewHolder> {
    private List<String> strings;
    private Context applicationContext;
    private OnItemClickListener onItemClickListener;
    private LayoutInflater layoutInflater;
    public static final int TYPE_1 = 0;
    public static final int TYPE_2 = 1;

    /**
     * 回调接口
     */
    public interface OnItemClickListener {
        void OnItemClick(int position, View v);
    }

    /**
     * 对外提供的接口响应
     *
     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        onItemClickListener = listener;
    }

    public MyAdaper(List<String> strings, Context applicationContext) {
        this.strings = strings;
        this.applicationContext = applicationContext;
        layoutInflater = LayoutInflater.from(applicationContext);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView;
        ViewHolder viewHolder;
        //根据不同类型设定不同条目
        if (viewType == TYPE_1) {
            itemView = layoutInflater.inflate(R.layout.item_type1_recycle,
                    parent, false);
            viewHolder = new Type1ViewHolder(itemView);
        } else {
            itemView = layoutInflater.inflate(R.layout.item_type2_recycle, parent, false);
            viewHolder = new Type2ViewHolder(itemView);
        }
        return viewHolder;
    }

    /**
     * 绑定数据
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
//        switch (holder.getItemViewType()) {
//            也可通过这种方式设定
//        }
        //根据不同类型设定不同条目
        if (holder instanceof Type1ViewHolder) {
            Type1ViewHolder viewHolder = (Type1ViewHolder) holder;
            viewHolder.tv.setText(strings.get(holder.getLayoutPosition()));
        } else {
            Type2ViewHolder viewHolder = (Type2ViewHolder) holder;
            viewHolder.tv2.setText(strings.get(holder.getLayoutPosition()));
        }
    }

    /**
     * 返回不同类型的ViewType
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        //模拟返回值类型
        if (position % 3 == TYPE_1) {
            return TYPE_1;
        } else {
            return TYPE_2;
        }
    }

    /**
     * 返回数值总量
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return strings.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public ViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
        }

        /**
         * 条目点击事件
         *
         * @param v
         */
        @Override
        public void onClick(View v) {
            int position = this.getLayoutPosition();
            onItemClickListener.OnItemClick(position, v);
        }
    }

    /**
     * 类型一的Holder
     */
    private class Type1ViewHolder extends ViewHolder {
        public TextView tv;

        public Type1ViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.tv_type1);
        }
    }

    /**
     * 类型二Holder
     */
    private class Type2ViewHolder extends ViewHolder {
        TextView tv2;
        ImageView imageView;

        public Type2ViewHolder(View itemView) {
            super(itemView);
            tv2 = (TextView) itemView.findViewById(R.id.tv_type2);
            imageView = (ImageView) itemView.findViewById(R.id.iv_recycle);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值