亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

詳解分別用Kotlin和java寫RecyclerView的示例

發布時間:2020-09-16 17:00:09 來源:腳本之家 閱讀:159 作者:kel_love 欄目:編程語言

本文介紹了分別用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:

<android.support.v7.widget.RecyclerView
  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.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00000000"
    android:paddingBottom="5dip">
  </android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>

recycler_item:

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="100dp"
  android:layout_height="100dp">
  <Button
    android:id="@+id/recycle_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="No.1"/>
</RelativeLayout>

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 = ""
    get
    set
}

activity_main:

<android.support.constraint.ConstraintLayout
  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.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00000000"
    android:paddingBottom="5dip">
  </android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>

recycler_item:

<RelativeLayout>
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="100dp"
  android:layout_height="100dp">
  <Button
    android:id="@+id/recycle_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="No.1"/>
</RelativeLayout>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

东乌珠穆沁旗| 抚顺县| 东阳市| 大港区| 宜城市| 仁怀市| 渭源县| 黔西县| 康定县| 淮南市| 南部县| 玛多县| 台中市| 三门峡市| 宝丰县| 永顺县| 赤峰市| 蒲城县| 宜春市| 镇平县| 五寨县| 吉隆县| 绍兴县| 冀州市| 军事| 莆田市| 巧家县| 古丈县| 葵青区| 台中市| 额济纳旗| 新密市| 五大连池市| 化德县| 宣武区| 三穗县| 桐城市| 遵化市| 常宁市| 简阳市| 遵义市|