RecycleView基本使用
程序员文章站
2022-03-11 15:28:31
1.布局文件.xml
1.布局文件.xml
<android.support.v7.widget.RecyclerView
android:id="@+id/regionInfoRecycler"
android:layout_width="match_parent"
android:layout_height="0dp"
android:visibility="visible"
android:layout_weight="8"/>
2.子布局文件.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal"
>
<TextView
android:id="@+id/orderNum"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:gravity="center"
android:text="1"></TextView>
<TextView
android:id="@+id/regionName"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="3"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:text="高新供电中心"></TextView>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/colorLine" />
</LinearLayout>
3.适配器 Adapter
package rcd.rfidmobile.adapter;
import android.graphics.Region;
import android.support.annotation.Nullable;
import android.widget.TextView;
import com.chad.library.adapter.base.BaseItemDraggableAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import java.util.List;
import rcd.rfidmobile.R;
import rcd.rfidmobile.entity.RegionDTO;
public class RegionAdapter extends BaseItemDraggableAdapter<RegionDTO, BaseViewHolder> {
public RegionAdapter(@Nullable List<RegionDTO> data){
super(R.layout.region_item,data);
}
@Override
protected void convert(BaseViewHolder helper, RegionDTO item) {
TextView orderNumTV = helper.getView(R.id.orderNum);
orderNumTV.setText((helper.getAdapterPosition()+1)+"");
TextView regionNameTV = helper.getView(R.id.regionName);
regionNameTV.setText(item.getRegionname());
}
}
4.Activity
recyclerView = findViewById(R.id.regionInfoRecycler);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
regionAdapter = new RegionAdapter(regionDTOList);
//滑动删除
ItemDragAndSwipeCallback itemDragAndSwipeCallback = new ItemDragAndSwipeCallback(regionAdapter);
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemDragAndSwipeCallback);
itemTouchHelper.attachToRecyclerView(recyclerView);
// 开启拖拽
regionAdapter.enableDragItem(itemTouchHelper, R.id.textView, true);
regionAdapter.setOnItemDragListener(new OnItemDragListener() {
@Override
public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int pos) {
// Toast.makeText(RegionCollectionActivity.this,"onItemDragStart",Toast.LENGTH_SHORT).show();
Log.i(TAG, "onItemDragStart");
}
@Override
public void onItemDragMoving(RecyclerView.ViewHolder source, int from, RecyclerView.ViewHolder target, int to) {
// Toast.makeText(RegionCollectionActivity.this,"onItemDragMoving",Toast.LENGTH_SHORT).show();
Log.i(TAG, "onItemDragMoving");
}
@Override
public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int pos) {
// Toast.makeText(RegionCollectionActivity.this,"onItemDragEnd",Toast.LENGTH_SHORT).show();
Log.i(TAG, "onItemDragEnd");
}
});
//开启滑动删除
regionAdapter.enableSwipeItem();
regionAdapter.setOnItemSwipeListener(new OnItemSwipeListener() {
@Override
public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int pos) {
//Toast.makeText(RegionCollectionActivity.this,"onItemSwipeStart",Toast.LENGTH_SHORT).show();
Log.i(TAG, "onItemSwipeStart");
}
@Override
public void clearView(RecyclerView.ViewHolder viewHolder, int pos) {
// Toast.makeText(RegionCollectionActivity.this,"clearView",Toast.LENGTH_SHORT).show();
Log.i(TAG, "clearView");
}
@Override
public void onItemSwiped(RecyclerView.ViewHolder viewHolder, final int pos) {
//Toast.makeText(RegionCollectionActivity.this,"onItemSwiped",Toast.LENGTH_SHORT).show();
// Toast.makeText(RegionInfoActivity.this, "" + regionDTOList.get(pos).getRegionname() + "数据被删除了", Toast.LENGTH_SHORT).show();
Log.i(TAG, "onItemSwiped");
new AlertDialog.Builder(context)
.setTitle("确认删除" + regionDTOList.get(pos).getRegionname() + "吗?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
deleteRegion(pos);
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
initData();
regionAdapter.setNewData(regionDTOList);
}
}).show();
}
@Override
public void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder, float dX, float dY, boolean isCurrentlyActive) {
//Toast.makeText(RegionCollectionActivity.this,"onItemSwipeMoving",Toast.LENGTH_SHORT).show();
Log.i(TAG, "onItemSwipeMoving");
}
});
recyclerView.setAdapter(regionAdapter);
本文地址:https://blog.csdn.net/qq_39312230/article/details/107480396