RecyclerView全套方案系列二,移动应用开发案例教程

本文详细介绍了如何在Android中创建并使用RecyclerView,包括自定义适配器、点击事件监听和布局管理。通过实例展示了从XML布局到Activity的完整流程,帮助开发者掌握移动应用开发中的列表视图操作。
摘要由CSDN通过智能技术生成

public void onClick(View v) {

if (onItemClickListener != null) {

int index = holder.getAdapterPosition();

onItemClickListener.onItemClick(v, holder, index);

}

}

});

}

@Override

public int getItemCount() {

if (mList != null)

return mList.size();

return 0;

}

public interface OnItemClickListener {

void onItemClick(View view, RecyclerView.ViewHolder holder, int position);

}

static class MainViewHolder extends RecyclerView.ViewHolder {

MainViewHolder(@NonNull View itemView) {

super(itemView);

}

}

}

2、item_layout

==============

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:tools=“http://schemas.android.com/tools”

android:id="@+id/rv_item_name_tv"

android:layout_width=“match_parent”

android:layout_height=“45dp”

android:layout_marginTop=“10dp”

android:layout_marginBottom=“10dp”

android:background="#2d6baa"

android:gravity=“center”

android:text=“功能按钮”

android:textColor="#ffffff"

android:textSize=“18sp”>

3、MainAdapterClickListener

==========================

public class MainAdapterClickListener implements MainAdapter.OnItemClickListener {

@Override

public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {

processClickView(view.getContext(), position);

}

private void processClickView(Context context, int position) {

switch (position) {

case 0:

Toast.makeText(context, “111”, Toast.LENGTH_SHORT).show();

break;

case 1:

Toast.makeText(context, “222”, Toast.LENGTH_SHORT).show();

break;

case 2:

Toast.makeText(context, “333”, Toast.LENGTH_SHORT).show();

break;

case 3:

Toast.makeText(context, “444”, Toast.LENGTH_SHORT).show();

break;

}

}

}

4、MainActivity

==============

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

}

private void initView() {

RecyclerView rvMain = findViewById(R.id.rvMain);

LinearLayoutManager layoutManager = new LinearLayoutManager(this);

rvMain.setLayoutManager(layoutManager);

DividerItemDecoration decoration = new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL);

rvMain.addItemDecoration(decoration);

MainAdapter adapter = new MainAdapter(getData());

rvMain.setAdapter(adapter);

adapter.setItemListener(new MainAdapterClickListener());

}

private List getData() {

String[] name = getResources().getStringArray(R.array.rv_item_name);

if (name == null || name.length == 0) {

return Collections.emptyList();

}

return Arrays.asList(name);

}

}

5、activity_main

================

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:app=“http://schemas.android.com/apk/res-auto”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“vertical”

tools:context=".MainActivity">

<androidx.recyclerview.widget.RecyclerView

android:id=

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

“@+id/rvMain”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”

tools:listitem="@layout/item_module_function"

/>

6、strings.xml

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值