本文介紹了分別用Kotlin和java寫(xiě)RecyclerView的示例,分享給大家,具體如下:
札達(dá)網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、響應(yīng)式網(wǎng)站設(shè)計(jì)等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)公司自2013年起到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專(zhuān)注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。
java:跟一般的寫(xiě)法一樣,增加了按鈕響應(yīng)
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); // 豎直方向的網(wǎng)格樣式,每行一個(gè)Item mLayoutManager=new GridLayoutManager(this,3,OrientationHelper.VERTICAL,false); // 設(shè)置布局管理器 mRecyclerView.setLayoutManager(mLayoutManager); // 設(shè)置adapter mRecyclerView.setAdapter(mRecyclerAdapter); // 設(shè)置Item添加和移除的動(dòng)畫(huà) mRecyclerView.setItemAnimator(newDefaultItemAnimator()); } //adapter的按鈕點(diǎn)擊事件 @Override public void onItemContentClick(Viewview,intposition) { //點(diǎn)擊最后一個(gè)按鈕增加一個(gè)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; } //刷新整個(gè)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:
recycler_item:
Kotlin:語(yǔ)法略有不同,recyclerview調(diào)用類(lèi)似
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) //添加一個(gè)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的按鈕點(diǎn)擊事件 override fun OnItemClick(v : View, position : Int) { if(position == items.size - 1) { val item = RecyclerItem() item.name = "No." + (position + 1) items.add(0, item) //更新數(shù)據(jù) 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 = "" get set }
activity_main:
recycler_item:
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="100dp" android:layout_height="100dp">
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。