You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android Java项目中为evrencoskun TableView启用行侧滑功能?

如何在evrencoskun TableView中实现行侧滑功能

evrencoskun的TableView库本身并未提供内置的行侧滑操作API,但可以通过自定义Cell结合侧滑布局来实现你需要的效果(类似附图中的侧滑菜单),以下是具体实现步骤:

1. 准备侧滑布局依赖

可以使用成熟的第三方侧滑库简化开发,比如daimajia/SwipeLayout,在项目的Module级别build.gradle中添加依赖:

dependencies {
    implementation 'com.daimajia.swipe:library:1.2.7'
}

如果不想依赖第三方库,也可以自行通过自定义ViewGroup监听触摸事件实现侧滑逻辑,不过第三方库能节省大量开发时间。

2. 创建侧滑行Item布局

编写包含侧滑菜单和内容区域的布局文件(示例命名为swipeable_row_item.xml):

<?xml version="1.0" encoding="utf-8"?>
<com.daimajia.swipe.SwipeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <!-- 侧滑显示的操作菜单(示例为删除按钮) -->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:background="#ff4444">

        <Button
            android:id="@+id/btn_delete"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:text="删除"
            android:textColor="#ffffff"
            android:background="@null"/>
    </LinearLayout>

    <!-- 行的正常内容区域 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:background="#ffffff">

        <!-- 替换为你实际的行内容控件 -->
        <TextView
            android:id="@+id/tv_row_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"/>
    </LinearLayout>

</com.daimajia.swipe.SwipeLayout>

3. 自定义可侧滑的Cell类

继承TableView的Cell类,绑定侧滑布局并提供操作接口:

public class SwipeableRowCell extends Cell {
    private SwipeLayout mSwipeLayout;
    private TextView mContentTv;
    private Button mDeleteBtn;

    public SwipeableRowCell(Context context) {
        super(context);
        initView();
    }

    public SwipeableRowCell(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.swipeable_row_item, this);
        mSwipeLayout = findViewById(R.id.swipe_layout);
        mContentTv = findViewById(R.id.tv_row_content);
        mDeleteBtn = findViewById(R.id.btn_delete);
    }

    // 设置行内容
    public void setRowContent(String content) {
        mContentTv.setText(content);
    }

    // 获取侧滑布局,用于控制侧滑状态
    public SwipeLayout getSwipeLayout() {
        return mSwipeLayout;
    }

    // 设置删除按钮点击监听
    public void setDeleteAction(OnClickListener listener) {
        mDeleteBtn.setOnClickListener(listener);
    }
}

4. 实现自定义CellAdapter

继承TableView的CellAdapter,使用自定义的侧滑Cell:

public class CustomSwipeCellAdapter extends CellAdapter {
    private List<String> mRowData;
    private SwipeLayout mOpenedSwipeLayout; // 记录当前打开的侧滑布局,避免同时打开多个

    public CustomSwipeCellAdapter(List<String> rowData) {
        mRowData = rowData;
    }

    @Override
    public Cell onCreateCellViewHolder(ViewGroup parent, int viewType) {
        return new SwipeableRowCell(parent.getContext());
    }

    @Override
    public void onBindCellViewHolder(Cell cell, int position) {
        SwipeableRowCell swipeCell = (SwipeableRowCell) cell;
        swipeCell.setRowContent(mRowData.get(position));

        // 处理删除逻辑
        swipeCell.setDeleteAction(v -> {
            mRowData.remove(position);
            notifyDataSetChanged();
            if (mOpenedSwipeLayout != null) {
                mOpenedSwipeLayout.close();
                mOpenedSwipeLayout = null;
            }
        });

        // 点击内容区域关闭侧滑,同时管理多侧滑状态
        swipeCell.setOnClickListener(v -> {
            if (swipeCell.getSwipeLayout().isOpen()) {
                swipeCell.getSwipeLayout().close();
                mOpenedSwipeLayout = null;
            } else {
                // 关闭其他已打开的侧滑
                if (mOpenedSwipeLayout != null && mOpenedSwipeLayout != swipeCell.getSwipeLayout()) {
                    mOpenedSwipeLayout.close();
                }
                mOpenedSwipeLayout = swipeCell.getSwipeLayout();
            }
        });
    }

    @Override
    public int getCellItemCount() {
        return mRowData.size();
    }
}

5. 绑定Adapter到TableView

在你的Activity或Fragment中完成Adapter的初始化与绑定:

TableView tableView = findViewById(R.id.your_table_view_id);
List<String> sampleData = new ArrayList<>();
// 填充测试数据
sampleData.add("第一行数据");
sampleData.add("第二行数据");
sampleData.add("第三行数据");

CustomSwipeCellAdapter cellAdapter = new CustomSwipeCellAdapter(sampleData);
tableView.setAdapter(cellAdapter);

注意事项

  • 若使用第三方侧滑库,需注意版本兼容性,避免与项目中其他依赖冲突。
  • 多侧滑状态管理:通过记录当前打开的侧滑布局,确保同一时间只有一行处于侧滑打开状态,提升用户体验。
  • 自定义侧滑逻辑:如果不依赖第三方库,可通过重写ViewGroup的onTouchEvent方法,计算滑动距离来动态调整子布局的位置,实现侧滑效果。

内容的提问来源于stack exchange,提问作者Сергей Соколов

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 19:33:25