0
我正在使用此https://github.com/ISchwarz23/SortableTableView庫來顯示自定義TableView中的數據。我想要做的是當用戶點擊表格的一行來彈出2個新按鈕(位於行的位置),供用戶選擇編輯或刪除該行。如何在用戶點擊自定義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>
我不想要任何佈局消失。我想在用戶點擊一行TableView時顯示另一個。 –