Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android recycleView的應用和點擊事件實例詳解

Android recycleView的應用和點擊事件實例詳解

編輯:關於Android編程

Android recycleView的應用和點擊事件

效果圖:


長按是刪除:

下面代碼:

MainActivity 類

public class MainActivity extends AppCompatActivity {

  RecyclerView rv;

  List<String> mList = new ArrayList<>();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.activity_main);
    rv = (RecyclerView) findViewById(R.id.rv);
    for (int i = 0; i < 1000; i++) {
      mList.add("條目" + i);
    }
    final MyAdapter adapter = new MyAdapter(this, mList);
    adapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
      @Override
      public void onClick(int position) {
        Toast.makeText(getBaseContext(), "點擊的是" + position + "的itemClick", Toast.LENGTH_SHORT).show();
      }
    });
    adapter.setOnItemLongClickListener(new MyAdapter.OnItemLongClickListener() {
      @Override
      public void onLongClick(int position) {
        //長按刪除數據
        mList.remove(position);
        adapter.notifyDataSetChanged();
      }
    });
    rv.setAdapter(adapter);

    //布局管理器
    rv.setLayoutManager(new StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.VERTICAL));
  }


  public static class VH extends RecyclerView.ViewHolder {
    TextView tv;

    public VH(View itemView) {
      super(itemView);
      tv = (TextView) itemView.findViewById(android.R.id.text1);
      tv.setBackgroundResource(R.drawable.bg);
    }
  }


}

MyAdapter.java類

public class MyAdapter extends RecyclerView.Adapter<MainActivity.VH> {

  Context mContext;
  List<String> mList;

  public MyAdapter(Context context, List<String> list) {
    this.mContext = context;
    this.mList = list;
  }

  @Override
  public MainActivity.VH onCreateViewHolder(ViewGroup parent, int viewType) {
    return new MainActivity.VH(View.inflate(mContext, android.R.layout.simple_list_item_1, null));
  }

  @Override
  public void onBindViewHolder(MainActivity.VH holder, final int position) {
    holder.tv.setText(mList.get(position));

    //自己做item點擊
    holder.itemView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (onItemClickListener != null)
          onItemClickListener.onClick(position);
      }
    });
    holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
      @Override
      public boolean onLongClick(View v) {
        if (onItemLongClickListener != null)
          onItemLongClickListener.onLongClick(position);
        //返回false會在長安結束後繼續點擊
        return true;
      }
    });
  }

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


  /**
   * 點擊
   */
  public interface OnItemClickListener {
    void onClick(int position);
  }

  OnItemClickListener onItemClickListener;


  public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
    this.onItemClickListener = onItemClickListener;
  }

  public interface OnItemLongClickListener {
    void onLongClick(int position);
  }

  OnItemLongClickListener onItemLongClickListener;

  public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
    this.onItemLongClickListener = onItemLongClickListener;
  }
}

activity_main.xml類

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.example.administrator.lesson10_recyclerview2.MainActivity">


  <android.support.v7.widget.RecyclerView
    android:id="@+id/rv"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout> 

 感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved