用户单击自定义TableView行时如何弹出2个新按钮?

时间:2017-12-02 19:36:10

标签: java android tableview

我正在使用此https://github.com/ISchwarz23/SortableTableView库在自定义TableView中显示数据。我想要做的是当用户点击表格的一行时弹出2个新按钮(在行的位置),供用户选择编辑或删除该行。

FragmentTableUsers.class

public class FragmentTableUsers extends Fragment {
View v;
ArrayList<User> allUsers;
private static final String[] TABLE_HEADERS = { "Ime", "Prezime"};

public FragmentTableUsers() {
    // Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    DataBaseHelper helper = new DataBaseHelper(getActivity());
    allUsers = helper.getAllUsers();

    v =  inflater.inflate(R.layout.fragment_fragment_add_user, container, false);

    TableView tableView = (TableView<String[]>) v.findViewById(R.id.tableView);
    tableView.setColumnCount(2);
    tableView.setDataAdapter(new UserTableDataAdapter(getActivity(), allUsers));
    SimpleTableHeaderAdapter simpleHeader = new SimpleTableHeaderAdapter(getActivity(), TABLE_HEADERS);
    simpleHeader.setTextColor(Color.WHITE);
    simpleHeader.setPaddingTop(8);
    simpleHeader.setPaddingBottom(8);

    tableView.setHeaderAdapter(simpleHeader);
    tableView.setHeaderElevation(5);
    int colorEvenRows = getResources().getColor(R.color.white);
    int colorOddRows = getResources().getColor(R.color.et_grey);
    tableView.setDataRowBackgroundProvider(TableDataRowBackgroundProviders.alternatingRowColors(colorEvenRows, colorOddRows));
    tableView.addDataClickListener(new UserClickListener());
    return  v;
}
private class UserClickListener implements TableDataClickListener<User> {
    @Override
    public void onDataClicked(int rowIndex, User clickedData) {
        String clickedCarString = clickedData.getName() + " " + clickedData.getSurname(); // HERE IS CODE WHEN USER CLICK ON ROW
    }
}

TableAdapter.class:

class UserTableDataAdapter extends TableDataAdapter<User> {

public UserTableDataAdapter(Context context, List<User> data) {
    super(context, data);
}

@Override
public View getCellView(int rowIndex, int columnIndex, ViewGroup parentView) {
    User user = getRowData(rowIndex);
    View renderedView = null;

    switch (columnIndex) {
        case 0:
            renderedView = renderUserName(user);
            break;
        case 1:
            renderedView = renderSurName(user);
    }
    return renderedView;
}

private View renderUserName(final User user) {
    return renderString(user.getName());
}

private View renderSurName(final User user) {
    return renderString(user.getSurname());
}

private View renderString(final String value) {
    final TextView textView = new TextView(getContext());
    textView.setText(value);
    textView.setTextColor(Color.BLACK);
    textView.setPadding(20, 10, 20, 10);
    textView.setTextSize(14);
    return textView;
}

FragmentTable用户xml:

<FrameLayout 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="com.group.digit.razvoj.appointment.user.FragmentTableUsers">

<!-- TODO: Update blank fragment layout -->

<LinearLayout
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/between_margin"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/tableViewLaayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <de.codecrafters.tableview.TableView xmlns:table="http://schemas.android.com/apk/res-auto"
            android:id="@+id/tableView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            table:tableView_columnCount="2"
            table:tableView_headerColor="@color/cyan" />

    </LinearLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

最简单的解决方案可能是为元素添加一个onclick监听器,该元素设置按钮&#39;财产可见。

以下是我过去如何做到这一点的代码示例。

fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            enrolling=true;
            attendance.setVisibility(View.GONE);
            lin.setVisibility(View.VISIBLE);
            fname.setText("");
            lname.setText("");
            id_view.setText("Scan Card");
            Snackbar.make(view, "Scan To Enroll New User", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

.setVisibility(View.*some visibility property*)是您想要的线。