详解分别用Kotlin和java写RecyclerView的示例

网友投稿 394 2023-03-03


详解分别用Kotlin和java写RecyclerView的示例

本文介绍了分别用Kotlin和java写RecyclerView的示例,分享给大家,具体如下:

java:跟一般的写法一样,增加了按钮响应

MainActivity:

public class MainActivity extends AppCompatActivity implements RecyclerAdapter.OnItemClickListener{

private RecyclerView mRecyclerView;

private RecyclerView.LayoutManager mLayoutManager;

private RecyclerAdapter mRecyclerAdapter;

private ArrayListlist;

@Override

protected void onCreate(Bundlesaved InstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

RecyclerItem item;

list = new ArrayList<>();

item = new RecyclerItem();

item.setName("No."+0);

list.add(item);

mRecyclerView = findViewById(R.id.recyclerView);

mRecyclerAdapter = new RecyclerAdapter(this,list);

mRecyclerAdapter.setOnItemClickListener(this);

// 竖直方向的网格样式,每行一个Item

mLayoutManager=new GridLayoutManager(this,3,OrientationHelper.VERTICAL,false);

// 设置布局管理器

mRecyclerView.setLayoutManager(mLayoutManager);

// 设置adapter

mRecyclerView.setAdapter(mRecyclerAdapter);

// 设置Item添加和移除的动画

mRecyclerView.setItemAnimator(newDefaultItemAnimator());

}

//adapter的按钮点击事件

@Override

public void onItemContentClick(Viewview,intposition) {

//点击最后一个按钮增加一个item

if(position == list.size()-1) {

RecyclerItemitem=newRecyclerItem();

item.setName("No."+(position+1));

list.add(0,item);

//更新RecyclerView

mRecyclerAdapter.updateData(list);

}else{

Toast.makeText(this,"No."+position,Toast.LENGTH_SHORT).show();

}

}

}

Adapter:

public class RecyclerAdapter extends RecyclerView.Adapter{

private ArrayList mData;

private Context mContext;

private OnItemClickListener onItemClickListener;

public void setOnItemClickListener(OnItemClickListener listener) {

this.onItemClickListener = listener;

}

public RecyclerAdapter(Context mContext,ArrayList mData){

this.mData = mData;

this.mContext = mContext;

}

//刷新整个list

public void updateData(ArrayList data) {

this.mData = data;

notifyDataSetChanged();

}

//刷新局部list

public void updateDataItem(ArrayList data, int itemnm) {

this.mData = data;

notifyItemChanged(itemnm);

}

@Override

public View Holder onCreateViewHolder(ViewGroup parent, int viewType) {

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);

ViewHolder viewHolder = new ViewHolder(v);

return viewHolder;

}

@Override

public void onBindViewHolder(final ViewHolder holder, int position) {

holder.btn.setText(mData.get(position).getName());

holder.btn.setOnClickListener(newView.OnClickListener() {

@Override

public void onClick(View v) {

if(onItemClickListener != null) {

int pos = holder.getLayoutPosition();

onItemClickListener.onItemContentClick(holder.itemView, pos);

}

}

});

}

@Override

public int getItemCount() {

return mData == null ? 0 : mData.size();

}

public static class ViewHolder extends RecyclerView.ViewHolder{

Button btn;

public ViewHolder(View itemView) {

super(itemView);

btn = itemView.findViewById(R.id.recycle_name);

}

}

public interface OnItemClickListener{

void onItemContentClick(View view, int position);

}

}

RecyclerItem:

public class RecyclerItem{

String name;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

activity_main:

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"

tools:context="com.tudou.recycleviewdemo.MainActivity">

android:id="@+id/recyclerView"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#00000000"

android:paddingBottom="5dip">

recycler_item:

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="100dp"

android:layout_height="100dp">

Kotlin:语法略有不同,recyclerview调用类似

MainActivity:

class MainActivity : AppCompatActivity(), MainAdapter.ItemClick{

val items : MutableList = ArrayList()

var mainAdapter : MainAdapter ?= null

override fun onCreate(savedInstanceState:Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

//添加一个item

var item : RecyclerItem

item = RecyclerItem()

item.name = "No."+0

items.add(item)

val recyclerView = findViewById(R.id.recyclerView) as RecyclerView

recyclerView.layoutManager = GridLayoutManager(this, 3, OrientationHelper.VERTICAL, false)

mainAdapter=MainAdapter(items, this)

mainAdapter!!.setItemClickListener(this)

recyclerView.adapter = mainAdapter

recyclerView.itemAnimator = DefaultItemAnimator()

}

//adapter的按钮点击事件

override fun OnItemClick(v : View, position : Int) {

if(position == items.size - 1) {

val item = RecyclerItem()

item.name = "No." + (position + 1)

items.add(0, item)

//更新数据

mainAdapter!!.updateData(items)

}else{

Toast.makeText(this,items.get(position).name, Toast.LENGTH_SHORT).show()

}

}

}

MainAdapter:

class MainAdapter : RecyclerView.Adapter{

var items : List? = null

var context : Context ?= null

var itemClick : ItemClick ?= null

constructor(items : List, context : Context){

this.items = items

this.context = context

}

fun setItemClickListener(itemClick : ItemClick){

this.itemClick = itemClick

}

fun updateData(items : List){

this.items = items

notifyDataSetChanged()

}

override fun onBindViewHolder(holder : ViewHolder?, position : Int) {

var name=items!!.get(position).name

if(holder != null) {

holder.textName.setText(name)

holder.textName.setOnClickListener(View.OnClickListener{

if(itemClick != null) {

itemClick!!.OnItemClick(holder.itemView, position)

}

})

}

}

override fun getItemCount():Int{

return items!!.size

}

override fun onCreateViewHolder(parent : ViewGroup?, viewType : Int) : ViewHolder{

val v = LayoutInflater.from(parent!!.context).inflate(R.layout.recycler_item, parent, false) as RelativeLayout

val holder = ViewHolder(v)

return holder

}

class ViewHolder(itemView : View?) : RecyclerView.ViewHolder(itemView) {

var textName : TextView = itemView!!.findViewById(R.id.recycle_name) as TextView

}

interface ItemClick{

fun OnItemClick(v : View, position : Int);

}

}

RecyclerItem:

class RecyclerItem{

var name : String = ""LuHngcht

get

set

}

activity_main:

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"

tools:context="com.tudo.kotlintdemo.MainActivity">

android:id="@+id/recyclerView"

android:layout_width="match_parent"

android:layhttp://out_height="match_parent"

android:background="#00000000"

android:paddingBottom="5dip">

recycler_item:

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="100dp"

android:layout_height="100dp">

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"

tools:context="com.tudou.recycleviewdemo.MainActivity">

android:id="@+id/recyclerView"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#00000000"

android:paddingBottom="5dip">

android:id="@+id/recyclerView"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#00000000"

android:paddingBottom="5dip">

recycler_item:

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="100dp"

android:layout_height="100dp">

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="100dp"

android:layout_height="100dp">

android:id="@+id/recycle_name"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:text="No.1"/>

Kotlin:语法略有不同,recyclerview调用类似

MainActivity:

class MainActivity : AppCompatActivity(), MainAdapter.ItemClick{

val items : MutableList = ArrayList()

var mainAdapter : MainAdapter ?= null

override fun onCreate(savedInstanceState:Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

//添加一个item

var item : RecyclerItem

item = RecyclerItem()

item.name = "No."+0

items.add(item)

val recyclerView = findViewById(R.id.recyclerView) as RecyclerView

recyclerView.layoutManager = GridLayoutManager(this, 3, OrientationHelper.VERTICAL, false)

mainAdapter=MainAdapter(items, this)

mainAdapter!!.setItemClickListener(this)

recyclerView.adapter = mainAdapter

recyclerView.itemAnimator = DefaultItemAnimator()

}

//adapter的按钮点击事件

override fun OnItemClick(v : View, position : Int) {

if(position == items.size - 1) {

val item = RecyclerItem()

item.name = "No." + (position + 1)

items.add(0, item)

//更新数据

mainAdapter!!.updateData(items)

}else{

Toast.makeText(this,items.get(position).name, Toast.LENGTH_SHORT).show()

}

}

}

MainAdapter:

class MainAdapter : RecyclerView.Adapter{

var items : List? = null

var context : Context ?= null

var itemClick : ItemClick ?= null

constructor(items : List, context : Context){

this.items = items

this.context = context

}

fun setItemClickListener(itemClick : ItemClick){

this.itemClick = itemClick

}

fun updateData(items : List){

this.items = items

notifyDataSetChanged()

}

override fun onBindViewHolder(holder : ViewHolder?, position : Int) {

var name=items!!.get(position).name

if(holder != null) {

holder.textName.setText(name)

holder.textName.setOnClickListener(View.OnClickListener{

if(itemClick != null) {

itemClick!!.OnItemClick(holder.itemView, position)

}

})

}

}

override fun getItemCount():Int{

return items!!.size

}

override fun onCreateViewHolder(parent : ViewGroup?, viewType : Int) : ViewHolder{

val v = LayoutInflater.from(parent!!.context).inflate(R.layout.recycler_item, parent, false) as RelativeLayout

val holder = ViewHolder(v)

return holder

}

class ViewHolder(itemView : View?) : RecyclerView.ViewHolder(itemView) {

var textName : TextView = itemView!!.findViewById(R.id.recycle_name) as TextView

}

interface ItemClick{

fun OnItemClick(v : View, position : Int);

}

}

RecyclerItem:

class RecyclerItem{

var name : String = ""LuHngcht

get

set

}

activity_main:

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"

tools:context="com.tudo.kotlintdemo.MainActivity">

android:id="@+id/recyclerView"

android:layout_width="match_parent"

android:layhttp://out_height="match_parent"

android:background="#00000000"

android:paddingBottom="5dip">

recycler_item:

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="100dp"

android:layout_height="100dp">

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"

tools:context="com.tudo.kotlintdemo.MainActivity">

android:id="@+id/recyclerView"

android:layout_width="match_parent"

android:layhttp://out_height="match_parent"

android:background="#00000000"

android:paddingBottom="5dip">

android:id="@+id/recyclerView"

android:layout_width="match_parent"

android:layhttp://out_height="match_parent"

android:background="#00000000"

android:paddingBottom="5dip">

recycler_item:

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="100dp"

android:layout_height="100dp">

android:id="@+id/recycle_name"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:text="No.1"/>


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:详解如何优雅地在React项目中使用Redux
下一篇:ionic3实战教程之随机布局瀑布流的实现方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~