RecycleView添加头布局底布局,点击事件,多布局

Activity:
1:初始化控件

@BindView(R.id.recyclerviewhead)
public RecyclerView recyclerviewhead;
public ArrayList<String> data ;
public MyRecycleViewAdapter adapter;

:添加数据

ButterKnife.bind(this);
    /**
     * 模拟数据
     * */
    data =new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        data.add("数据"+i);
    }

3:设置方向

LinearLayoutManager linearLayoutManager =new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerviewhead.setLayoutManager(linearLayoutManager);

4:adapter处理

adapter =new MyRecycleViewAdapter(data,this);
    /**
     * 添加头布局
     * */
    adapter.addHeadView(View.inflate(getApplicationContext(),R.layout.header,null));
    /**
     * 添加底布局
     * */
    adapter.addFootView(View.inflate(getApplicationContext(),R.layout.footer,null));
    recyclerviewhead.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    /**
     * 分割线
     * */
    recyclerviewhead.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
    /**
     * 监听事件
     * */
    adapter.setOnItenClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Toast.makeText(MainRecycleViewActivity.this, "点击了"+position, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onLongItemClick(View view, int position) {
            Toast.makeText(MainRecycleViewActivity.this, "长按了"+position, Toast.LENGTH_SHORT).show();
        }
    });

Adapter :

1:继承 extends RecyclerView.Adapter<RecyclerView.ViewHolder>
2:添加布局判断

/**
 * 头布局
 */
public static final int HEADVIEW = 3;
//创建一个头布局的View
public View ViewHead;
public  static int headViewSize =0;
/**
 * 底布局
 */
public static final int FOOTVIEW = 4;
//创建一个头布局的View
public View ViewFoot;
public  static int footViewSize =0;
/**
 * 多布局
 */
public static final int TYPE_GROUP = 1;
public static final int TYPE_CHILD = 2;

3:区分那个布局

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(context);
    RecyclerView.ViewHolder holder = null;
    switch (viewType) {
        case 1:
            View view1 = inflater.inflate(R.layout.test_group_item, null);
            holder = new MyGroupHolder(view1);
            break;
        case 2:
            View view2 = inflater.inflate(R.layout.test_child_item, null);
            holder = new MyChildHolder(view2);
            break;
        case 3:
            //头布局
            holder = new MyHeadViewHolder(ViewHead);
            break;
        case 4:
            //底布局
            holder = new MyFootViewHolder(ViewFoot);
            break;
    }
    return holder;
}

4:绑定数据

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

  /**
   * 判断该位置是头布局还是底布局
   * */
    if (!isAddHeadView(position)&& !isAddFootView(position)) {
        if(ViewHead!=null){
            /**
             * 头布局的时候,这里要减1不然要越界
             * */
            position--;
        }
        if (holder instanceof MyGroupHolder) {
            ((MyGroupHolder) holder).tv_group_name.setText(data.get(position));

        } else if (holder instanceof MyChildHolder) {
            ((MyChildHolder) holder).tv_child_name.setText(data.get(position));
        }
    }
    View itemview = ((RelativeLayout) (holder.itemView)).getChildAt(0);
    if (onItemClickListener != null) {
        itemview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = holder.getLayoutPosition();
                onItemClickListener.onItemClick(holder.itemView, position);
            }
        });
    }
    //长按点击
    if (onLongItemClickListener != null) {
        itemview.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                int position = holder.getLayoutPosition();
                onLongItemClickListener.onLongItemClick(holder.itemView, position);
                return true;
            }
        });
    }

}

5:item个数

@Override
public int getItemCount() {
    Log.e("wudi", "getItemCount: "+(data.size()+headViewSize+footViewSize ));
    return (data.size()+headViewSize+footViewSize);
}

6:多布局区分类型

@Override
public int getItemViewType(int position) {

    if (isAddHeadView(position)) {
        return HEADVIEW;
    } else if (isAddFootView(position)) {
        return FOOTVIEW;
    } else {

        if (position > 0 && position < data.size()+headViewSize+footViewSize && position % 2 == 0) {
            return TYPE_CHILD;
        } else {
            return TYPE_GROUP;
        }
    }
}

7:添加头布局,底布局
/**
* 头布局
*/

public void addHeadView(View headView) {
    ViewHead = headView;
    headViewSize =1;
}

/**
 * 底布局
 */
public void addFootView(View footView) {
    ViewFoot = footView;
    footViewSize =1;
}

/**
 * 是否添加了头布局
 */
public boolean isAddHeadView(int position) {
    //第一个布局是头布局
    return ViewHead != null && position == 0;
}

/**
 * 是否添加了底布局
 */
public boolean isAddFootView(int position) {
    //最后一个item 是底布局
    return ViewFoot != null && position == (data.size()+headViewSize+footViewSize-1);
}

8: 数据布局
/**
* 正常布局2
*/

public static class MyGroupHolder extends RecyclerView.ViewHolder {

    @BindView(R.id.tv_group_name)
    public TextView tv_group_name;

    public MyGroupHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }
}

/**
 * 正常布局 1
 */
public static class MyChildHolder extends RecyclerView.ViewHolder {

    @BindView(R.id.tv_child_name)
    public TextView tv_child_name;

    public MyChildHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }
}

9:监听事件

//点击监听

private OnItemClickListener onItemClickListener;
//长按监听
private OnItemClickListener onLongItemClickListener;

public void setOnItenClickListener(OnItemClickListener listener) {
    onItemClickListener = listener;
    onLongItemClickListener = listener;
}

Interface:
//接口回调

public interface OnItemClickListener {

/**
 * 定义点击事件接口
 * */
void onItemClick(View view, int position);

/**
 * 定义长按点击事件
 * */
void onLongItemClick(View view, int position);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用ItemTouchHelper来实现RecycleView布局的拖拽和滑动删除功能。下面是一个简单的示例代码: 1. 创建ItemTouchHelper.Callback ``` public class MyItemTouchHelperCallback extends ItemTouchHelper.Callback { private final ItemTouchHelperAdapter mAdapter; public MyItemTouchHelperCallback(ItemTouchHelperAdapter adapter) { mAdapter = adapter; } @Override public boolean isLongPressDragEnabled() { return true; } @Override public boolean isItemViewSwipeEnabled() { return true; } @Override public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END; return makeMovementFlags(dragFlags, swipeFlags); } @Override public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { mAdapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition()); return true; } @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { mAdapter.onItemDismiss(viewHolder.getAdapterPosition()); } } ``` 2. 创建ItemTouchHelperAdapter ``` public interface ItemTouchHelperAdapter { void onItemMove(int fromPosition, int toPosition); void onItemDismiss(int position); } ``` 3. 在Adapter中实现ItemTouchHelperAdapter接口 ``` public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements ItemTouchHelperAdapter { private List<MyData> mData; ... @Override public void onItemMove(int fromPosition, int toPosition) { MyData item = mData.get(fromPosition); mData.remove(fromPosition); mData.add(toPosition, item); notifyItemMoved(fromPosition, toPosition); } @Override public void onItemDismiss(int position) { mData.remove(position); notifyItemRemoved(position); } } ``` 4. 在Activity或Fragment中添加ItemTouchHelper ``` ItemTouchHelper.Callback callback = new MyItemTouchHelperCallback(mAdapter); ItemTouchHelper touchHelper = new ItemTouchHelper(callback); touchHelper.attachToRecyclerView(mRecyclerView); ``` 注意:以上代码只是示例,具体实现需要根据自己的需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一声紫金,一生紫金

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值