最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android RecyclerView實現(xiàn)點擊條目刪除

 更新時間:2018年11月23日 11:41:16   作者:FanRQ_  
這篇文章主要為大家詳細介紹了Android RecyclerView實現(xiàn)點擊條目刪除,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了RecyclerView實現(xiàn)點擊條目刪除的具體代碼,供大家參考,具體內(nèi)容如下

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

  private Button mButton1;
  private Button mButton2;
  private Button mButton3;
  private Button mButton4;
  private Button mButton5;
  private RecyclerView mRecyclerView;
  private ArrayList<String> mList;
  private LinearLayoutManager mLinearLayoutManager;
  private RvAdapter mAdapter;


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    findViews();

    mList = new ArrayList<>();
    for (int i=0;i<20;i++){
      mList.add(i+"item");
    }

    mAdapter = new RvAdapter(mList, this);
    mRecyclerView.setAdapter(mAdapter);

    //設(shè)置分割線
    mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
    //設(shè)置默認布局
    mLinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    mRecyclerView.setLayoutManager(mLinearLayoutManager);

    mAdapter.setOnItemClickListener(new RvAdapter.OnItemClickListener() {
      @Override
      public void onItemClick(int position) {
        mAdapter.remove(position);
      }

      @Override
      public void onItemLongClick(int position) {

        mAdapter.remove(position);
      }
    });

  }

  private void findViews() {

    mRecyclerView = findViewById(R.id.rv);

    mButton1= findViewById(R.id.b1);
    mButton2= findViewById(R.id.b2);
    mButton3= findViewById(R.id.b3);
    mButton4= findViewById(R.id.b4);
    mButton5= findViewById(R.id.b5);

    mButton1.setOnClickListener(this);
    mButton2.setOnClickListener(this);
    mButton3.setOnClickListener(this);
    mButton4.setOnClickListener(this);
    mButton5.setOnClickListener(this);


  }

  @Override
  public void onClick(View view) {
    switch (view.getId()){
      case R.id.b1:

        mAdapter.addData(3);
        mRecyclerView.scrollToPosition(0);
        break;
      case R.id.b2:

        mAdapter.remove(mList.size()-1);
        break;
      case R.id.b3:

        mLinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        break;
      case R.id.b4:

        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        //mRecyclerView.addItemDecoration(new android.support.v7.widget.DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL));

        break;
      case R.id.b5:

        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        break;
    }
  }
}

activity_main.xml

<android.support.constraint.ConstraintLayout 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"
  tools:context=".MainActivity">

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal">
      <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="添加"/>
      <Button
        android:id="@+id/b2"
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="刪除"/>
      <Button
        android:id="@+id/b3"
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="List"/>
      <Button
        android:id="@+id/b4"
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Grid"/>
      <Button
        android:id="@+id/b5"
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="flow"/>
    </LinearLayout>
    <android.support.v7.widget.RecyclerView
      android:id="@+id/rv"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>
  </LinearLayout>

</android.support.constraint.ConstraintLayout>

RvAdapter.java

public class RvAdapter extends RecyclerView.Adapter<RvAdapter.ViewHolder>{

  private List<String> lists;
  private Context mContext;

  public RvAdapter(List<String> lists, Context context) {
    this.lists = lists;
    mContext = context;
  }

  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = View.inflate(mContext, R.layout.item, null);
    ViewHolder holder = new ViewHolder(view);
    return holder;
  }

  public void addData(int position) {
    lists.add(position,"ff");
    notifyItemInserted(position);
  }

  public void remove(int i) {
    lists.remove(i);
    notifyItemRemoved(i);
    notifyDataSetChanged();

  }

  public interface OnItemClickListener{  //自定義接口回調(diào)設(shè)置點擊事件
    void onItemClick(int position);
    void onItemLongClick(int position);
  }

  private OnItemClickListener mOnItemClickListener;

  public void setOnItemClickListener(OnItemClickListener onItemClickListener){
    mOnItemClickListener=onItemClickListener;
  }

  @Override
  public void onBindViewHolder(final ViewHolder holder, final int position) {
    holder.mTextView.setText(lists.get(position));

       holder.itemView.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {

           int ps = holder.getLayoutPosition();
           mOnItemClickListener.onItemClick(ps);
         }
       });

       holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
         @Override
         public boolean onLongClick(View view) {

           int ps=holder.getLayoutPosition();
           mOnItemClickListener.onItemLongClick(ps);
           return false;
         }
       });

  }


  @Override
  public int getItemCount() {
    return lists.size();
  }

  public static class ViewHolder extends RecyclerView.ViewHolder{
    public final TextView mTextView;

    public ViewHolder(View itemView) {
      super(itemView);
      mTextView = (TextView) itemView.findViewById(R.id.tv);
    }
  }
}

build.gradle

implementation 'com.android.support:recyclerview-v7:27.1.1'

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android編程獲取圖片和視頻縮略圖的方法

    Android編程獲取圖片和視頻縮略圖的方法

    這篇文章主要介紹了Android編程獲取圖片和視頻縮略圖的方法,結(jié)合實例形式分析了Android圖形圖像處理所涉及的常用函數(shù)與使用技巧,需要的朋友可以參考下
    2016-04-04
  • Android離線Doc文檔訪問速度慢的有效解決方法

    Android離線Doc文檔訪問速度慢的有效解決方法

    今天小編就為大家分享一篇關(guān)于Android離線Doc文檔訪問速度慢的有效解決方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Android開發(fā)之文本內(nèi)容自動朗讀功能實現(xiàn)方法

    Android開發(fā)之文本內(nèi)容自動朗讀功能實現(xiàn)方法

    這篇文章主要介紹了Android開發(fā)之文本內(nèi)容自動朗讀功能實現(xiàn)方法,結(jié)合實例形式分析了Android自動朗讀TTS功能的操作步驟、相關(guān)函數(shù)使用方法與注意事項,需要的朋友可以參考下
    2017-09-09
  • 上傳Android項目至github的解析

    上傳Android項目至github的解析

    本文主要講解了如何將自己的android項目上傳至github,相信大家平時在開發(fā)過程中為了避免重復(fù)造輪子會經(jīng)常逛一下github查看有沒有與需求類似的開源項目,那么github上面的開源項目是如何上傳至github上的呢?
    2018-05-05
  • 直接應(yīng)用項目中的Android圖片緩存技術(shù)

    直接應(yīng)用項目中的Android圖片緩存技術(shù)

    這篇文章主要為大家詳細介紹了直接應(yīng)用項目中的Android圖片緩存技術(shù),簡單、方便、高效,感興趣的小伙伴們可以參考一下
    2016-04-04
  • 去除arraylist容器中的相同的對象元素的方法

    去除arraylist容器中的相同的對象元素的方法

    下面小編就為大家?guī)硪黄コ齛rraylist容器中的相同的對象元素的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-09-09
  • 詳解Android中Intent傳遞對象給Activity的方法

    詳解Android中Intent傳遞對象給Activity的方法

    這篇文章主要介紹了Android中Intent傳遞對象給Activity的方法,文章中對Activity的生命周期等知識先作了簡要的介紹,需要的朋友可以參考下
    2016-04-04
  • Android實現(xiàn)圖文垂直跑馬燈效果

    Android實現(xiàn)圖文垂直跑馬燈效果

    這篇文章主要為大家詳細介紹了Android實現(xiàn)圖文垂直跑馬燈效果,圖文結(jié)合的跑馬燈效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android打開圖庫選擇照片功能代碼

    Android打開圖庫選擇照片功能代碼

    這篇文章主要介紹了Android打開圖庫選擇照片功能代碼以及實現(xiàn)流程分析,對此有需要的朋友參考學(xué)習(xí)下吧。
    2018-02-02
  • Android開發(fā)之OkHttpUtils的具體使用方法

    Android開發(fā)之OkHttpUtils的具體使用方法

    這篇文章主要介紹了Android開發(fā)之OkHttpUtils的具體使用方法,非常具有實用價值,需要的朋友可以參考下
    2017-08-08

最新評論

旬阳县| 澄迈县| 长治县| 乐昌市| 沁阳市| 上杭县| 关岭| 驻马店市| 师宗县| 津市市| 馆陶县| 通山县| 澄江县| 合阳县| 高碑店市| 西安市| 嫩江县| 体育| 类乌齐县| 雅安市| 安吉县| 滦平县| 博湖县| 临湘市| 辽中县| 隆尧县| 徐水县| 新巴尔虎右旗| 富阳市| SHOW| 黄石市| 昌吉市| 新绛县| 大田县| 扎鲁特旗| 锦屏县| 沁水县| 荆州市| 吴堡县| 营山县| 西贡区|