Android recyclerview新增刪除條目點選事件
阿新 • • 發佈:2018-11-28
1.首先匯入recyclerview依賴
implementation 'com.android.support:recyclerview-v7:28.0.0'
2.activity_main.xml
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="新增"/> <Button android:id="@+id/btn2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="刪除"/> <Button android:id="@+id/btn3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="list"/> <Button android:id="@+id/btn4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="grid"/> <Button android:id="@+id/btn5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="flow"/> </LinearLayout> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/recycleview" android:scrollbars="vertical"></android.support.v7.widget.RecyclerView>
3.iten.xml佈局(介面卡)
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_horizontal"> <ImageView android:layout_width="100dp" android:layout_height="100dp" android:src="@mipmap/ic_launcher" android:id="@+id/img"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv" android:text="別來無恙" android:textSize="25sp" android:paddingTop="30dp"/> </LinearLayout> </RelativeLayout>
4.MainActivity主頁面
package com.bwie.a20181114; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.view.View; import android.widget.Button; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button btn1; private Button btn2; private Button btn3; private Button btn4; private Button btn5; private RecyclerView recycleview; private List<String> mList; private RecycleViewAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控制元件 initView(); //初始化資料 initData(); //介面卡 mAdapter = new RecycleViewAdapter(mList, this); recycleview.setAdapter(mAdapter); mAdapter.setmkkListener(new RecycleViewAdapter.mkkListener() { @Override public void success(String data) { Toast.makeText(MainActivity.this, data, Toast.LENGTH_SHORT).show(); } }); //線性管理器,支援橫向縱向 LinearLayoutManager manager = new LinearLayoutManager(this); recycleview.setLayoutManager(manager); } private void initData() { mList = new ArrayList<>(); for (int i = 0; i < 100; i++) { mList.add("別來無恙"+i); } } private void initView() { btn1 = (Button) findViewById(R.id.btn1); btn2 = (Button) findViewById(R.id.btn2); btn3 = (Button) findViewById(R.id.btn3); btn4 = (Button) findViewById(R.id.btn4); btn5 = (Button) findViewById(R.id.btn5); recycleview = (RecyclerView) findViewById(R.id.recycleview); btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); btn4.setOnClickListener(this); btn5.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn1://新增 mAdapter.addData(1); break; case R.id.btn2://刪除 mAdapter.delData(1); break; case R.id.btn3://list //線性管理器,支援橫向縱向 LinearLayoutManager manager = new LinearLayoutManager(this); recycleview.setLayoutManager(manager); break; case R.id.btn4://grid //網格佈局管理器 GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3); recycleview.setLayoutManager(gridLayoutManager); break; case R.id.btn5://flow //瀑布式佈局管理 StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); recycleview.setLayoutManager(staggeredGridLayoutManager); break; } } }
5.介面卡
package com.bwie.a20181114;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.PointerIcon;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* date:2018/11/14
* author:別來無恙(別來無恙)
* function:
*/
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder> {
private List<String> mList;
private Context mContext;
public RecycleViewAdapter(List<String> list, Context context) {
mList = list;
mContext = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(mContext,R.layout.iten,null);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder,final int i) {
viewHolder.mImg.setImageResource(R.drawable.n);
viewHolder.mTv.setText(mList.get(i));
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mkkListener.success(mList.get(i));
}
});
}
@Override
public int getItemCount() {
return mList.size();
}
//新增資料
public void addData(int i) {
mList.add(i,"1");
notifyItemInserted(i);
}
//刪除資料
public void delData(int i) {
mList.remove(i);
//更新
notifyItemRemoved(i);
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final ImageView mImg;
private final TextView mTv;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mImg = itemView.findViewById(R.id.img);
mTv = itemView.findViewById(R.id.tv);
}
}
public interface mkkListener{
void success(String data);
}
private mkkListener mkkListener;
public void setmkkListener(mkkListener mkkListeners){
mkkListener = mkkListeners;
}
}