安卓中为了获取context的方法和区别(getContext,getActivity,this,mainActivity.this等)

本文详细介绍了在Android开发中如何正确使用Context,包括MainActivity.this、this、getContext()等不同方法的区别和应用场景,帮助开发者更好地理解并运用Context。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在着手做项目的时候,发现在需要context的时候,不同的类或者位置中需要用不同的方法聊火气context对象。在百度谷歌查找资料以及问周围的大神之后,总结了大概如下的内容:

1.MainActivity.this:表示MainActivity对象,一般用在内部类中指示外面的this,如果在内部类直接用this,指示的是内部类本身。因为MainActivity继承Activity,而Activity继承Context,所以它也可以用来提供Activity Contex;

2.this:表示当前对象;一般而言,在哪个类中调用,就是指向该对象。

3.getContext():这个是View类中提供的方法,在继承了View的类中才可以调用,返回的是当前View运行在哪个Activity Context中。

4.getActivity和getcontext其实差不多,一般在fragment中使用的时候,用此方法获取。

4.在定义适配器等工具类的时候,很多时候都是要把从头当做方法参数传进来,在构造方法中定义。

package com.example.kucun2.ui.dingdan;//package com.example.kucun2; import static android.content.ContentValues.TAG; import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.animation.ObjectAnimator; import android.animation.ValueAnimator; import android.app.AlertDialog; import android.content.Context; import android.graphics.Color; import android.graphics.Typeface; import android.os.Bundle; import android.util.Log; import android.util.TypedValue; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewTreeObserver; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.FrameLayout; import android.widget.HorizontalScrollView; import android.widget.LinearLayout; import android.widget.PopupMenu; import android.widget.SearchView; import android.widget.Spinner; import android.widget.TableLayout; import android.widget.TableRow; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; import com.example.kucun2.MainActivity; import com.example.kucun2.R; import com.example.kucun2.View.HorizontalScrollTextView; import com.example.kucun2.entity.Bancai; import com.example.kucun2.entity.Chanpin; import com.example.kucun2.entity.Chanpin_Zujian; import com.example.kucun2.entity.Dingdan; import com.example.kucun2.entity.Dingdan_chanpin_zujian; import com.example.kucun2.entity.Dingdan_Chanpin; import com.example.kucun2.entity.data.Data; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; public class OrderDisplayFragment extends Fragment { private TableLayout table; private HorizontalScrollView horizontalScrollView; private ValueAnimator scrollIndicatorAnimator; private boolean isIndicatorVisible = false; // 添加排序相关的成员变量 private int currentSortColumn = -1; private boolean sortAscending = true; private List<Object[]> allTableRowsData = new ArrayList<>(); // 添加搜索相关成员变量 private SearchView searchView; private Spinner columnSelector; private List<Object[]> filteredTableRowsData = new ArrayList<>(); private boolean isDataLoaded = false; // 添加成员变量保存关键视图引用 private View scrollIndicator; private View rootView; // 保存根视图引用 // 添加滚动监听器的引用 private final ViewTreeObserver.OnScrollChangedListener scrollListener = new ViewTreeObserver.OnScrollChangedListener() { @Override public void onScrollChanged() { // 关键修复:添加全面的空值检查 if (horizontalScrollView == null || !isAdded() || horizontalScrollView.getChildCount() == 0) { return; } View child = horizontalScrollView.getChildAt(0); if (child == null) return; int maxScroll = child.getWidth() - horizontalScrollView.getWidth(); int currentScroll = horizontalScrollView.getScrollX(); if (currentScroll > 0 && maxScroll > 0) { if (!isIndicatorVisible) { showScrollIndicator(); } updateScrollIndicatorPosition(currentScroll, maxScroll); } else { hideScrollIndicator(); } } }; /** * 加载初始化 * * @param inflater The LayoutInflater object that can be used to inflate * any views in the fragment, * @param container If non-null, this is the parent view that the fragment's * UI should be attached to. The fragment should not add the view itself, * but this can be used to generate the LayoutParams of the view. * @param savedInstanceState If non-null, this fragment is being re-constructed * from a previous saved state as given here. * @return */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { rootView = inflater.inflate(R.layout.fragment_order_display, container, false); table = rootView.findViewById(R.id.orderTable); horizontalScrollView = rootView.findViewById(R.id.horizontalScrollContainer); scrollIndicator = rootView.findViewById(R.id.scroll_indicator); // 获取搜索控件 searchView = rootView.findViewById(R.id.search_view); columnSelector = rootView.findViewById(R.id.column_selector); table = rootView.findViewById(R.id.orderTable); horizontalScrollView = rootView.findViewById(R.id.horizontalScrollContainer); scrollIndicator = rootView.findViewById(R.id.scroll_indicator); // 初始化表头选择器 initColumnSelector(); // 设置搜索监听 searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { applySearchFilter(); return true; } @Override public boolean onQueryTextChange(String newText) { applySearchFilter(); return true; } }); LinearLayout fixedSearchBar = rootView.findViewById(R.id.fixedSearchBar); View placeholder = rootView.findViewById(R.id.search_bar_placeholder); // 添加全局布局监听器以获取正确的搜索框高度 fixedSearchBar.getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { // 获取搜索框的实际高度 int searchBarHeight = fixedSearchBar.getHeight(); // 设置占位视图的高度 ViewGroup.LayoutParams params = placeholder.getLayoutParams(); params.height = searchBarHeight; placeholder.setLayoutParams(params); // 确保仅运行一次 fixedSearchBar.getViewTreeObserver().removeOnGlobalLayoutListener(this); } } ); Log.d(TAG, "onCreateView: " + Data.dingdans.get(0).getNumber()); // 添加表头 addTableHeader(table); // 检查数据是否已加载 if (Data.dingdans.isEmpty()) { // 显示加载指示器 showLoadingIndicator(); // 设置数据加载监听器 if (getActivity() instanceof MainActivity) { ((MainActivity) getActivity()).setOnDataLoadListener(new MainActivity.OnDataLoadListener() { @Override public void onDataLoaded() { requireActivity().runOnUiThread(() -> { hideLoadingIndicator(); isDataLoaded = true; fillTableData(); // 填充数据 }); } @Override public void onDataError() { requireActivity().runOnUiThread(() -> { hideLoadingIndicator(); Toast.makeText(getContext(), "检查网络", Toast.LENGTH_SHORT).show(); //showError("数据加载失败"); }); } }); } } else { // 数据已加载,直接填充 fillTableData(); isDataLoaded = true; } // 填充表格数据 // fillTableData(); // 添加滚动监听 horizontalScrollView.getViewTreeObserver().addOnScrollChangedListener(() -> { // 添加安全检查空值防护 if (horizontalScrollView == null || horizontalScrollView.getChildCount() == 0 || getView() == null) { return; } View child = horizontalScrollView.getChildAt(0); if (child == null) return; int maxScroll = child.getWidth() - horizontalScrollView.getWidth(); int currentScroll = horizontalScrollView.getScrollX(); if (currentScroll > 0 && maxScroll > 0) { if (!isIndicatorVisible) { showScrollIndicator(); } updateScrollIndicatorPosition(currentScroll, maxScroll); } else { hideScrollIndicator(); } }); return rootView; } // 显示/隐藏加载指示器的方法 private void showLoadingIndicator() { // 实现加载动画或进度条 } private void hideLoadingIndicator() { if (scrollIndicator == null || !isAdded()) return; isIndicatorVisible = false; View indicator = this.scrollIndicator; // 使用成员变量 // 隐藏加载指示器 } @Override public void onDestroyView() { super.onDestroyView(); if (horizontalScrollView != null) { ViewTreeObserver observer = horizontalScrollView.getViewTreeObserver(); if (observer.isAlive()) { observer.removeOnScrollChangedListener(scrollChangedListener); } horizontalScrollView = null; } } @Override public void onAttach(@NonNull Context context) { super.onAttach(context); if (context instanceof MainActivity) { ((MainActivity) context).setOnDataLoadListener(new MainActivity.OnDataLoadListener() { @Override public void onDataLoaded() { // 数据加载完成后填充表格 getActivity().runOnUiThread(() -> { Log.d("DataLoad", "Data loaded, filling table"); fillTableData(); }); } @Override public void onDataError() { //showToast("数据加载失败"); } }); } } /** * 获取数据 */ private void fillTableData() { List<Dingdan> orders = Data.dingdans; List<Dingdan_Chanpin> orderProducts = Data.dingdan_chanpins; List<Dingdan_chanpin_zujian> orderMaterials = Data.Dingdan_chanpin_zujians; allTableRowsData.clear(); filteredTableRowsData.clear(); // 创建映射关系提高效率 Map<Integer, List<Dingdan_Chanpin>> orderProductMap = new HashMap<>(); Map<Integer, List<Chanpin_Zujian>> productComponentMap = new HashMap<>(); Map<Integer, List<Dingdan_chanpin_zujian>> componentMaterialMap = new HashMap<>(); // 构建映射 for (Dingdan_Chanpin op : orderProducts) { if (op != null && op.getDingdan() != null) { int orderId = op.getDingdan().getId(); orderProductMap.computeIfAbsent(orderId, k -> new ArrayList<>()).add(op); } } for (Chanpin_Zujian cz : Data.chanpin_zujians) { int productId = cz.getChanpin().getId(); productComponentMap.computeIfAbsent(productId, k -> new ArrayList<>()).add(cz); } for (Dingdan_chanpin_zujian dm : orderMaterials) { int componentId = dm.getZujian().getId(); componentMaterialMap.computeIfAbsent(componentId, k -> new ArrayList<>()).add(dm); } // 重组数据 for (Dingdan order : orders) { List<Dingdan_Chanpin> productsForOrder = orderProductMap.get(order.getId()); if (productsForOrder != null) { for (Dingdan_Chanpin op : productsForOrder) { Chanpin product = op.getChanpin(); List<Chanpin_Zujian> componentsForProduct = productComponentMap.get(product.getId()); if (componentsForProduct != null) { for (Chanpin_Zujian cz : componentsForProduct) { List<Dingdan_chanpin_zujian> materialsForComponent = componentMaterialMap.get(cz.getZujian().getId()); if (materialsForComponent != null) { for (Dingdan_chanpin_zujian dm : materialsForComponent) { Object[] rowData = createRowData(order, product, op, cz, dm); allTableRowsData.add(rowData); filteredTableRowsData.add(rowData); } } } } } } } // 日志记录添加行数 Log.d("TableFill", "Total rows created: " + allTableRowsData.size()); // 初始排序 sortTableData(-1, true); } /** * 排序表格数据并刷新显示 * * @param columnIndex 要排序的列索引 * @param ascending 是否升序排列 */ private void sortTableData(int columnIndex, boolean ascending) { // 更新排序状态 if (columnIndex >= 0) { if (currentSortColumn == columnIndex) { // 相同列点击时切换排序方向 sortAscending = !ascending; } else { currentSortColumn = columnIndex; sortAscending = true; // 新列默认升序 } } // 创建排序比较器 Comparator<Object[]> comparator = (row1, row2) -> { if (currentSortColumn < 0) { return 0; // 返回0表示相等,保持原顺序 } Object value1 = row1[currentSortColumn]; Object value2 = row2[currentSortColumn]; if (value1 == null && value2 == null) return 0; if (value1 == null) return -1; if (value2 == null) return 1; // 根据不同列数据类型定制比较规则 try { // 数值列:2(数量), 5(板材/组件), 6(订购数量) if (currentSortColumn == 2 || currentSortColumn == 5 || currentSortColumn == 6) { double d1 = Double.parseDouble(value1.toString()); double d2 = Double.parseDouble(value2.toString()); return sortAscending ? Double.compare(d1, d2) : Double.compare(d2, d1); } // 其他列按字符串排序 else { String s1 = value1.toString().toLowerCase(); String s2 = value2.toString().toLowerCase(); return sortAscending ? s1.compareTo(s2) : s2.compareTo(s1); } } catch (NumberFormatException e) { // 解析失败时按字符串比较 String s1 = value1.toString().toLowerCase(); String s2 = value2.toString().toLowerCase(); return sortAscending ? s1.compareTo(s2) : s2.compareTo(s1); } }; // 特殊处理初始未排序状态 if (columnIndex == -1) { // 直接复制数据而不排序 filteredTableRowsData.clear(); filteredTableRowsData.addAll(allTableRowsData); } else { Collections.sort(filteredTableRowsData, comparator); } // 刷新显示 refreshTableWithData(filteredTableRowsData); } /** * 表格数据动态添加 * * @param rowData */ private void addTableRow(Object[] rowData) { TableRow row = new TableRow(requireContext()); TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams( TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT ); row.setLayoutParams(rowParams); row.setMinimumHeight(dpToPx(36)); for (int i = 0; i < rowData.length; i++) { final Object data = rowData[i]; // 判断是否为操作列(最后一列) if (i == rowData.length - 1) { // 创建操作按钮 Button actionButton = new Button(requireContext()); actionButton.setText("操作"); actionButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14); actionButton.setBackgroundResource(R.drawable.btn_selector); // 自定义按钮样式 // 设置按钮点击监听器 actionButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { handleRowAction(rowData, v); } }); // 设置按钮布局参数 TableRow.LayoutParams btnParams = new TableRow.LayoutParams( 0, // 宽度由权重控制 TableRow.LayoutParams.WRAP_CONTENT, 0.5f ); btnParams.weight = 0.5f; int margin = dpToPx(1); btnParams.setMargins(margin, margin, margin, margin); actionButton.setLayoutParams(btnParams); actionButton.setHeight(11); row.addView(actionButton); } else { // 正常文本列的代码(保持原逻辑) HorizontalScrollTextView textView = new HorizontalScrollTextView(requireContext()); textView.setText(String.valueOf(data)); textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14); int padding = dpToPx(8); textView.setPadding(padding, padding / 2, padding, padding); textView.setMinWidth(dpToPx(50)); TableRow.LayoutParams colParams = null; // 设置背景边框 textView.setBackgroundResource(R.drawable.cell_border); if (data.toString().length() > 10) { colParams = new TableRow.LayoutParams( 0, // 宽度将由权重控制 TableRow.LayoutParams.MATCH_PARENT, 2.0f ); colParams.weight = 2; } else { colParams = new TableRow.LayoutParams( 0, // 宽度将由权重控制 TableRow.LayoutParams.MATCH_PARENT, 1.0f ); colParams.weight = 1; } textView.setLayoutParams(colParams); row.addView(textView); } } table.addView(row); } // 动态添加表头 (使用自定义TextView) private void addTableHeader(TableLayout table) { TableRow headerRow = new TableRow(requireContext()); headerRow.setLayoutParams(new TableLayout.LayoutParams( TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT )); // 设置行背景颜色 headerRow.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.purple_500)); // 定义表头 // 更新表头数组(添加操作列) String[] headers = getResources().getStringArray(R.array.table_headers); List<String> headerList = new ArrayList<>(Arrays.asList(headers)); headerList.add("操作"); // 添加操作列标题 headers = headerList.toArray(new String[0]); // 更新权重数组(添加操作列权重) float[] weights = {1.0f, 1.0f, 1.0f, 1.0f, 2.0f, 1.0f, 1.0f, 0.5f}; // 新增操作列权重0.5 // 更新优先级数组(添加操作列优先级) boolean[] priority = {false, false, false, false, true, false, false, false}; for (int i = 0; i < headers.length; i++) { HorizontalScrollTextView headerView = new HorizontalScrollTextView(requireContext()); headerView.setText(headers[i]); headerView.setTextColor(Color.WHITE); headerView.setTypeface(null, Typeface.BOLD); headerView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); headerView.setPadding(dpToPx(8), dpToPx(8), dpToPx(8), dpToPx(8)); // 为优先级高的列设置最小宽度 if (priority[i]) { headerView.setMinWidth(dpToPx(220)); } // 设置布局参数 TableRow.LayoutParams colParams = new TableRow.LayoutParams( priority[i] ? TableRow.LayoutParams.WRAP_CONTENT : 0, TableRow.LayoutParams.MATCH_PARENT, priority[i] ? 0 : weights[i] // 优先级列不使用权重 ); headerView.setLayoutParams(colParams); final int columnIndex = i; headerView.setOnClickListener(v -> { // 排序并刷新表格 sortTableData(columnIndex, sortAscending); // 更新排序指示器(可选) showSortIndicator(headerView); }); headerRow.addView(headerView); } table.addView(headerRow); } // 添加排序指示器(可选) private void showSortIndicator(View header) { // 实现:在表头右侧添加↑或↓指示符 // 实现逻辑根据设计需求 // header.setTooltipText(new ); } /** * */ private void showScrollIndicator() { if (scrollIndicator == null || !isAdded()) return; // 使用成员变量而不是findViewById View indicator = this.scrollIndicator; isIndicatorVisible = true; // View indicator = getView().findViewById(R.id.scroll_indicator); if (scrollIndicatorAnimator != null && scrollIndicatorAnimator.isRunning()) { scrollIndicatorAnimator.cancel(); } indicator.setVisibility(View.VISIBLE); indicator.setAlpha(0f); scrollIndicatorAnimator = ObjectAnimator.ofFloat(indicator, "alpha", 0f, 0.8f); scrollIndicatorAnimator.setDuration(300); scrollIndicatorAnimator.start(); } /** * + */ private void hideScrollIndicator() { isIndicatorVisible = false; View indicator = getView().findViewById(R.id.scroll_indicator); if (scrollIndicatorAnimator != null && scrollIndicatorAnimator.isRunning()) { scrollIndicatorAnimator.cancel(); } scrollIndicatorAnimator = ObjectAnimator.ofFloat(indicator, "alpha", indicator.getAlpha(), 0f); scrollIndicatorAnimator.setDuration(300); scrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { indicator.setVisibility(View.INVISIBLE); } }); scrollIndicatorAnimator.start(); } /** * @param currentScroll * @param maxScroll */ private void updateScrollIndicatorPosition(int currentScroll, int maxScroll) { if (scrollIndicator == null || !isAdded()) return; View indicator = this.scrollIndicator; // 使用成员变量 FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) indicator.getLayoutParams(); // 计算指示器位置(0-100%) float percentage = (float) currentScroll / maxScroll; int maxMargin = getResources().getDisplayMetrics().widthPixels - indicator.getWidth(); // 设置右边距(控制位置) params.rightMargin = (int) (maxMargin * percentage); indicator.setLayoutParams(params); } // 处理行操作的方法 private void handleRowAction(Object[] rowData, View anchorButton) { // 安全地从行数据中提取关键信息 String orderNumber = safeGetString(rowData[0]); // 订单号 String productId = safeGetString(rowData[1]); // 产品ID String componentName = safeGetString(rowData[3]); // 组件名称 // 安全地获取订购数量 double materialQuantity = 0.0; try { if (rowData[6] != null) { if (rowData[6] instanceof Number) { materialQuantity = ((Number) rowData[6]).doubleValue(); } else { materialQuantity = Double.parseDouble(rowData[6].toString()); } } } catch (Exception e) { Log.e("OrderFragment", "Failed to parse material quantity", e); } Context context = getContext(); if (context == null || anchorButton == null) { Log.w("PopupMenu", "Context or anchorButton is null"); return; } PopupMenu popupMenu = new PopupMenu(context, anchorButton); // 强制设置菜单在锚点视图下方显示(关键设置) popupMenu.setGravity(Gravity.BOTTOM); // 如果使用支持库,设置弹出方向 // 设置在锚点视图下方显示 // popupMenu.setOverlapAnchor(true); // 填充菜单项 popupMenu.getMenuInflater().inflate(R.menu.row_actions_menu, popupMenu.getMenu()); // 设置菜单项点击监听器 popupMenu.setOnMenuItemClickListener(item -> { int itemId = item.getItemId(); if (itemId == R.id.action_view_details) { showDetailDialog(orderNumber, productId); return true; } else if (itemId == R.id.action_edit) { editRowData(rowData); return true; } else if (itemId == R.id.action_delete) { deleteRowWithConfirm(rowData); return true; } return false; }); popupMenu.show(); } // 安全获取字符串值的方法 private String safeGetString(Object value) { if (value == null) return ""; if (value instanceof String) return (String) value; return value.toString(); } // 查看详情对话框 private void showDetailDialog(String orderNumber, String productId) { AlertDialog.Builder builder = new AlertDialog.Builder(requireContext()); builder.setTitle("订单详情") .setMessage("订单号: " + orderNumber + "\n产品ID: " + productId) .setPositiveButton("确定", null) .show(); } // 编辑行数据 private void editRowData(Object[] rowData) { // 实现编辑逻辑 // 这里创建包含表单的对话框 Toast.makeText(requireContext(), "编辑操作: " + rowData[0], Toast.LENGTH_SHORT).show(); } // 带确认的删除操作 private void deleteRowWithConfirm(Object[] rowData) { new AlertDialog.Builder(requireContext()) .setTitle("确认删除") .setMessage("确定要删除订单 " + rowData[0] + " 吗?") .setPositiveButton("删除", (dialog, which) -> { // 实际删除逻辑 deleteRow(rowData); }) .setNegativeButton("取消", null) .show(); } // 实际删除行数据 private void deleteRow(Object[] rowData) { // 1. 从allTableRowsData中移除对应行 for (Iterator<Object[]> iterator = allTableRowsData.iterator(); iterator.hasNext(); ) { Object[] row = iterator.next(); if (Arrays.equals(row, rowData)) { iterator.remove(); break; } } // 2. 从filteredTableRowsData中移除 filteredTableRowsData.removeIf(row -> Arrays.equals(row, rowData)); // 3. 刷新表格 refreshTableWithData(filteredTableRowsData); Toast.makeText(requireContext(), "已删除订单", Toast.LENGTH_SHORT).show(); } // DP转PX工具方法 private int dpToPx(int dp) { return (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics() ); } /** * 数据组合 * * @param order * @param product * @param component * @param material * @return */ private Object[] createRowData(Dingdan order, Chanpin product, Dingdan_Chanpin dingdan_chanpin, Chanpin_Zujian component, Dingdan_chanpin_zujian material) { Bancai board = material.getBancai(); String boardInfo = board.TableText(); ; return new Object[]{ order.getNumber(), // 订单号 product.getBianhao(), // 产品编号 dingdan_chanpin.getShuliang(), // 产品数量 (根据需求调整) component.getZujian().getName(), // 组件名 boardInfo, // 板材信息 Math.round(component.getOne_howmany()), // 板材/组件 material.getShuliang(), // 订购数量 "操作" }; } // 初始化列选择器 private void initColumnSelector() { ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( requireContext(), R.array.table_headers, android.R.layout.simple_spinner_item ); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); columnSelector.setAdapter(adapter); // 添加"所有列"选项 columnSelector.setSelection(0); // 默认选择第一个选项(所有列) // 列选择变化监听 columnSelector.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { applySearchFilter(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } // 应用搜索过滤 private void applySearchFilter() { String query = searchView.getQuery().toString().trim().toLowerCase(); int selectedColumn = columnSelector.getSelectedItemPosition(); filteredTableRowsData.clear(); if (query.isEmpty()) { // 没有搜索词,显示所有数据 filteredTableRowsData.addAll(allTableRowsData); } else { // 根据选择的列进行过滤 for (Object[] row : allTableRowsData) { // 如果选择"所有列"(位置0),检查所有列 if (selectedColumn == 0) { for (Object cell : row) { if (cell != null && cell.toString().toLowerCase().contains(query)) { filteredTableRowsData.add(row); break; } } } // 检查特定列 else if (selectedColumn >= 1 && selectedColumn <= row.length) { int columnIndex = selectedColumn - 1; // 调整索引(0=所有列,1=第一列) if (row[columnIndex] != null && row[columnIndex].toString().toLowerCase().contains(query)) { filteredTableRowsData.add(row); } } } } // 刷新表格显示 refreshTableWithData(filteredTableRowsData); } /** * 刷新表格显示 */ private void refreshTableWithData(Iterable<? extends Object[]> dataToShow) { // Log.d("TableRefresh", "Refreshing table with " + currentSortColumn + " rows"); // 添加调试信息 Log.d(“TableRefresh”, “Refreshing table with " + currentSortColumn + " rows”); removeAllRowsSafely(); int addedRows = 0; for (Object[] rowData : dataToShow) { addTableRow(rowData); addedRows++; } // 添加空数据提示 if (addedRows == 0) { addEmptyTableRow(); } } private void addEmptyTableRow() { TableRow row = new TableRow(requireContext()); TextView emptyView = new TextView(requireContext()); emptyView.setText("暂无数据"); emptyView.setGravity(Gravity.CENTER); emptyView.setLayoutParams(new TableRow.LayoutParams( TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT )); row.addView(emptyView); table.addView(row); } private void removeAllRowsSafely() { // 移除除表头外的所有行(如果有表头) if (table.getChildCount() > 0) { // 保留表头(索引0) for (int i = table.getChildCount() - 1; i >= 1; i--) { View child = table.getChildAt(i); table.removeView(child); // 清理视图引用(非常重要!) cleanupRowViews((TableRow) child); } } } private void cleanupRowViews(TableRow row) { int childCount = row.getChildCount(); for (int i = 0; i < childCount; i++) { View view = row.getChildAt(i); // 解除视图的所有监听器 view.setOnClickListener(null); // 特别是操作按钮,需要取消所有监听器 if (view instanceof Button) { Button button = (Button) view; button.setOnClickListener(null); // 清空按钮的数据引用 button.setTag(null); } } // 从父视图中移除行 if (row.getParent() != null) { ((ViewGroup) row.getParent()).removeView(row); } } @Override public void onDestroyView() { super.onDestroyView(); // 清理视图引用 // 移除滚动监听器 if (horizontalScrollView != null) { ViewTreeObserver observer = horizontalScrollView.getViewTreeObserver(); if (observer.isAlive()) { observer.removeOnScrollChangedListener(scrollListener); } } scrollIndicator = null; horizontalScrollView = null; table = null; rootView = null; } }当跳转到另一个Fragment时,当前Fragment销毁,需要再次返回时控件被重新加载能数据视图重新加载。但现在是在其他fragment中 没有返回这个fragment还是有报错并崩溃
06-14
package com.example.suyzeko.fragment; import android.app.AlertDialog; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.ServiceConnection; import android.content.pm.ActivityInfo; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.viewpager2.widget.ViewPager2; import android.os.Handler; import android.os.IBinder; import android.util.TypedValue; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewParent; import android.widget.Button; import android.widget.ImageButton; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.Toast; import com.example.suyzeko.MainActivity; import com.example.suyzeko.activitys.ledPanel.sixRoads.DeviceDetailsActivity; import com.example.suyzeko.activitys.ledPanel.twoRoads.PhototherapyBoardTwoActivity; import com.example.suyzeko.R; import com.example.suyzeko.SelectDeviceActivity; import com.example.suyzeko.activitys.therapyHelmet.fourZones.TherapyHelmetFourActivity; import com.example.suyzeko.activitys.therapyHelmet.nineteenZones.TherapyHelmetNineteenActivity; import com.example.suyzeko.entity.Device; import com.example.suyzeko.entity.DeviceClass; import com.example.suyzeko.service.BluetoothService; import com.example.suyzeko.utils.BannerAdapter; import com.example.suyzeko.utils.DeviceUtils; import com.example.suyzeko.utils.RetrofitAPI; import com.example.suyzeko.utils.RetrofitClient; import com.google.android.flexbox.FlexboxLayout; import java.util.Arrays; import java.util.List; import java.util.Objects; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; public class DashboardFragment extends Fragment{ private ViewPager2 viewPager; private FlexboxLayout flexboxContainer; private int buttonCount = 0; private View maskView; // 遮罩层视图 private PopupWindow popupWindow; private final Handler autoScrollHandler = new Handler(); private ImageButton btnAdd; private TextView textView; private BluetoothService bluetoothService; // 蓝牙后台服务实例 private final Runnable autoScrollRunnable = new Runnable() { @Override public void run() { if (viewPager != null) { int currentItem = viewPager.getCurrentItem(); viewPager.setCurrentItem(currentItem + 1, true); autoScrollHandler.postDelayed(this, 3000); // 3秒切换一次 } } }; private boolean isBound = false; private float flexBasisPercent = 0.4f; private int buttonHeight = 90; /** * 服务连接回调 */ private final ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { try { BluetoothService.LocalBinder binder = (BluetoothService.LocalBinder) service; bluetoothService = binder.getService(); isBound = true; registerReceivers(); // 注册广播接收器 } catch (SecurityException e) { throw new RuntimeException(e); } } @Override public void onServiceDisconnected(ComponentName name) { isBound = false; } }; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_dashboard, container, false); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); if (DeviceUtils.isTablet(requireActivity())) { flexBasisPercent = 0.22f; buttonHeight = 110; } else { // 手机设备 } viewPager = view.findViewById(R.id.viewPager); textView = view.findViewById(R.id.device_num); // 示例图片列表 List<Integer> images = Arrays.asList( R.drawable.ontc, R.drawable.offtc ); // 绑定服务 if (bluetoothService != null){ Intent intentBlue = new Intent(getActivity(), BluetoothService.class); bluetoothService.bindService(intentBlue, connection, Context.BIND_AUTO_CREATE); } // 初始化适配器并绑定 BannerAdapter adapter = new BannerAdapter(images); viewPager.setAdapter(adapter); // 设置初始位置(从中间开始,支持向左滑动) viewPager.setCurrentItem(Integer.MAX_VALUE / 2, false); // 自动轮播 autoScrollHandler.postDelayed(autoScrollRunnable, 3000); // 页面切换监听(可选) viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() { @Override public void onPageSelected(int position) { // 可以在此处理指示器逻辑 } }); flexboxContainer = view.findViewById(R.id.flexbox_container); // 初始化检查:如果没有按钮,显示添加按钮 checkAndShowAddButton(); // 获取加号按钮 btnAdd = view.findViewById(R.id.add_image_button); // 设置点击事件 btnAdd.setOnClickListener(this::showPopupMenu); } @Override public void onAttach(@NonNull Context context) { super.onAttach(context); if (context instanceof OnJumpToActivityListener) { onJumpToActivityListener = (OnJumpToActivityListener) context; } else { throw new RuntimeException("宿主 Activity 需实现 OnJumpToActivityListener"); } } @Override public void onResume(){ super.onResume(); loadActivity(); updateConnectionStatus(); } @Override public void onDestroyView() { super.onDestroyView(); autoScrollHandler.removeCallbacks(autoScrollRunnable); // 防止内存泄漏 // 确保 Fragment 销毁时移除遮罩 removeMask(); super.onDestroy(); } /** * 显示/隐藏"添加按钮"的逻辑 */ private void checkAndShowAddButton() { if (buttonCount == 0) { // 创建"添加按钮" Button addButton = new Button(requireContext()); addButton.setTextColor(Color.parseColor("#2A2A2A")); addButton.setBackgroundResource(R.drawable.button_style); // 可自定义样式 addButton.setText(getString(R.string.add)); // 布局参数 FlexboxLayout.LayoutParams params = new FlexboxLayout.LayoutParams( FlexboxLayout.LayoutParams.WRAP_CONTENT, FlexboxLayout.LayoutParams.WRAP_CONTENT ); params.setFlexBasisPercent(flexBasisPercent); params.setHeight(dpToPx(buttonHeight)); params.setMargins(32, 16, 32, 16); // 点击事件:开始添加普通按钮 addButton.setOnClickListener(v -> { onJumpToActivityListener.jumpToActivityAndClose();// 跳转到搜索设备页面 // navigateToNewActivity(); }); buttonCount++; flexboxContainer.addView(addButton, params); } } private List<Device> deviceLists; /** * 添加动态按钮的方法 * @param deviceList */ public void addNewButton(List<Device> deviceList) { for (Device device:deviceList){ int flag = 0; if(deviceLists != null){ for (Device device1:deviceLists){ if (device1.getDeviceMac().equals(device.getDeviceMac())) { flag = 1; break; } } } if (flag == 1){ continue; } buttonCount++; DeviceClass deviceTypeResources = getDeviceTypeResources(device.getDeviceClassCode()); Button newButton = new Button(requireContext()); newButton.setTag(device); newButton.post(() -> { // 获取按钮实际宽高(单位:像素) int buttonWidth = newButton.getWidth(); int buttonHeight = newButton.getHeight(); // 动态计算文字大小(示例:按钮宽度的 1/6) float textSizePx = buttonWidth / 20f; // 转换为 sp 单位(更符合用户习惯) float textSizeSp = pxToSp(textSizePx); // 设置文字大小 newButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSizeSp); }); // 创建状态图标层叠Drawable Drawable statusIcon = createStatusIcon(device); newButton.setText(device.getDeviceName()); // 获取设备类型对应的图标资源 Drawable icon = ContextCompat.getDrawable(requireContext(), deviceTypeResources.getIconRes()); // 计算按钮实际高度(从布局参数获取) int buttonHeightPx = dpToPx(buttonHeight); // 需与布局中params.setHeight(dpToPx(90))一致 // 设置图标尺寸为按钮高度的60%(按需调整比例) int iconSize = (int) (buttonHeightPx * 0.6); if (icon != null) { // 保持图标宽高比 float aspectRatio = (float) icon.getIntrinsicWidth() / icon.getIntrinsicHeight(); int scaledWidth = (int) (iconSize * aspectRatio); icon.setBounds(0, 0, scaledWidth, iconSize); // 设置绘制区域 } // 设置图标到按钮左侧 newButton.setCompoundDrawables(statusIcon, icon, null, null); newButton.setCompoundDrawablePadding(8); newButton.setBackgroundResource(R.drawable.button_style); newButton.setTextColor(Color.parseColor("#2A2A2A")); // 布局参数(与之前相同) FlexboxLayout.LayoutParams params = new FlexboxLayout.LayoutParams( FlexboxLayout.LayoutParams.WRAP_CONTENT, FlexboxLayout.LayoutParams.WRAP_CONTENT ); params.setFlexBasisPercent(flexBasisPercent); params.setHeight(dpToPx(buttonHeight)); params.setMargins(32, 16, 32, 16); // 点击事件 newButton.setOnClickListener(v -> { if (bluetoothService != null && bluetoothService.getConnectedDevice() != null){ if(device.getDeviceMac().equals(bluetoothService.getMACAddress())){ navigateToNewDeviceDetailsActivity(device); }else { new AlertDialog.Builder(getContext()) .setTitle("Device not connected") .setMessage("Do you want to redirect to the device connection page") // 确认按钮 .setPositiveButton(getString(R.string.OK), (dialog, which) -> { onJumpToActivityListener.jumpToActivityAndClose(); // 跳转到搜索设备页面 }) // 取消按钮 .setNegativeButton(getString(R.string.Cancel), (dialog, which) -> { dialog.dismiss(); }) .show(); } }else { new AlertDialog.Builder(getContext()) .setTitle("Device not connected") .setMessage("Do you want to redirect to the device connection page") // 确认按钮 .setPositiveButton(getString(R.string.OK), (dialog, which) -> { onJumpToActivityListener.jumpToActivityAndClose(); // 跳转到搜索设备页面 }) // 取消按钮 .setNegativeButton(getString(R.string.Cancel), (dialog, which) -> { dialog.dismiss(); }) .show(); } }); // // 长按删除(增加删除后的检查) // newButton.setOnLongClickListener(v -> { // flexboxContainer.removeView(v); // buttonCount--; // checkAndShowAddButton(); // 删除后检查是否需要恢复"添加按钮" // return true; // }); // 将新按钮添加到第一个 flexboxContainer.addView(newButton, 0, params); // --- 关键修改:强制刷新父容器滚动视图 --- flexboxContainer.post(new Runnable() { @Override public void run() { flexboxContainer.requestLayout(); // 触发 NestedScrollView 重新计算滚动范围 ViewParent parent = flexboxContainer.getParent(); if (parent instanceof ViewGroup) { ((ViewGroup) parent).requestLayout(); } } }); } textView.setText(String.valueOf(buttonCount-1)); } public void updateConnectionStatus() { for (int i = 0; i < flexboxContainer.getChildCount() - 1; i++) { View view = flexboxContainer.getChildAt(i); if (view instanceof Button) { Button btn = (Button) view; Device device = (Device) btn.getTag(); Drawable newStatus = createStatusIcon(device); DeviceClass deviceTypeResources = getDeviceTypeResources(device.getDeviceClassCode()); // 获取设备类型对应的图标资源 Drawable icon = ContextCompat.getDrawable(requireContext(), deviceTypeResources.getIconRes()); // 计算按钮实际高度(从布局参数获取) int buttonHeightPx = dpToPx(buttonHeight); // 设置图标尺寸为按钮高度的60%(按需调整比例) int iconSize = (int) (buttonHeightPx * 0.6); if (icon != null) { // 保持图标宽高比 float aspectRatio = (float) icon.getIntrinsicWidth() / icon.getIntrinsicHeight(); int scaledWidth = (int) (iconSize * aspectRatio); icon.setBounds(0, 0, scaledWidth, iconSize); // 设置绘制区域 } // 设置图标到按钮左侧 btn.setCompoundDrawables(newStatus, icon, null, null); btn.postInvalidate(); // 线程安全刷新 } } } public void updateUnConnectionStatus() { for (int i = 0; i < flexboxContainer.getChildCount() - 1; i++) { View view = flexboxContainer.getChildAt(i); if (view instanceof Button) { Button btn = (Button) view; Device device = (Device) btn.getTag(); if(bluetoothService!= null && device.getDeviceMac().equals(bluetoothService.getMACAddress())){ bluetoothService.disconnect(); Drawable newStatus = createStatusIcon(device); DeviceClass deviceTypeResources = getDeviceTypeResources(device.getDeviceClassCode()); // 获取设备类型对应的图标资源 Drawable icon = ContextCompat.getDrawable(requireContext(), deviceTypeResources.getIconRes()); // 计算按钮实际高度(从布局参数获取) int buttonHeightPx = dpToPx(buttonHeight); // 设置图标尺寸为按钮高度的60%(按需调整比例) int iconSize = (int) (buttonHeightPx * 0.6); if (icon != null) { // 保持图标宽高比 float aspectRatio = (float) icon.getIntrinsicWidth() / icon.getIntrinsicHeight(); int scaledWidth = (int) (iconSize * aspectRatio); icon.setBounds(0, 0, scaledWidth, iconSize); // 设置绘制区域 } // 设置图标到按钮左侧 btn.setCompoundDrawables(newStatus, icon, null, null); btn.postInvalidate(); // 线程安全刷新 } } } } private Drawable createStatusIcon(Device device) { // 获取状态图标资源 int statusRes = isDeviceConnected(device) ? R.drawable.ic_connected_green_round : R.drawable.ic_disconnected_hui_round; // 创建缩放后的状态图标 Drawable statusIcon = ContextCompat.getDrawable(requireContext(), statusRes); int statusSize = dpToPx(12); // 状态图标尺寸 if (statusIcon != null) { statusIcon.setBounds(0, 0, statusSize, statusSize); } return statusIcon; } private boolean isDeviceConnected(Device device) { return bluetoothService != null && bluetoothService.getConnectedDevice() != null && device.getDeviceMac().equals(bluetoothService.getMACAddress()); } /** * 定义类型映射关系 * @param * @return */ private DeviceClass getDeviceTypeResources(String deviceClassCode) { DeviceClass deviceClass; if(deviceClassCode.equals("5A A5 01 01")){ deviceClass = new DeviceClass(0,R.drawable.up_down_black, "ELEVATING WHIRLING LED PANEL"); }else if(deviceClassCode.equals("5A A5 02 02")){ deviceClass = new DeviceClass(1,R.drawable.ontc, "红光床"); }else { deviceClass = new DeviceClass(2,R.drawable.up_down_black, "ELEVATING WHIRLING LED PANEL"); } return deviceClass; } /** * 弹出菜单 * @param anchorView */ public void showPopupMenu(View anchorView) { // 初始化布局 View popupView = LayoutInflater.from(requireContext()).inflate(R.layout.menu_popup_layout, null); // 创建 PopupWindow popupWindow = new PopupWindow( popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true ); // 设置背景动画 popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); popupWindow.setElevation(8); // 阴影效果 popupWindow.setAnimationStyle(R.style.PopupAnimation); // 自定义动画 // 处理菜单项点击 popupView.findViewById(R.id.menu_item_add).setOnClickListener(v -> { // 处理“添加设备”逻辑 popupWindow.dismiss(); navigateToNewActivity(); }); popupView.findViewById(R.id.menu_item_scan).setOnClickListener(v -> { // 处理“扫一扫”逻辑 popupWindow.dismiss(); }); // 添加遮罩层 addMask(); // 设置 PopupWindow 关闭时移除遮罩 popupWindow.setOnDismissListener(() -> removeMask()); // 显示菜单(相对于锚点视图) popupWindow.showAsDropDown(anchorView, dpToPx(-130), dpToPx(15), Gravity.END); } /** * 将遮罩层添加到 Activity 的根布局 */ private void addMask() { if (getActivity() == null) return; ViewGroup rootView = (ViewGroup) getActivity().getWindow().getDecorView(); maskView = LayoutInflater.from(getContext()).inflate(R.layout.layout_background_mask, null); rootView.addView(maskView); } /** * 移除遮罩层 */ private void removeMask() { if (getActivity() == null || maskView == null) return; ViewGroup rootView = (ViewGroup) getActivity().getWindow().getDecorView(); rootView.removeView(maskView); maskView = null; } /** * 跳转查找设备Activity */ private void navigateToNewActivity() { if (getActivity() == null) return; // 避免 Fragment 未附加到 Activity 时崩溃 Intent intent = new Intent(getActivity(), SelectDeviceActivity.class); startActivity(intent); } /** * 跳转到新 Activity * @param device */ private void navigateToNewDeviceDetailsActivity(Device device) { if (getActivity() == null) return; // 避免 Fragment 未附加到 Activity 时崩溃 if(device.getDeviceClassCode().equals("5A A5 01 02")){ Intent intent = new Intent(getActivity(), PhototherapyBoardTwoActivity.class); intent.putExtra("devices",device); startActivity(intent); }else if(device.getDeviceClassCode().equals("5A A5 01 01")){ Intent intent = new Intent(getActivity(), DeviceDetailsActivity.class); intent.putExtra("devices",device); startActivity(intent); }else if(device.getDeviceClassCode().equals("5A A5 02 01")){ Intent intent = new Intent(getActivity(), TherapyHelmetFourActivity.class); intent.putExtra("devices",device); startActivity(intent); }else if(device.getDeviceClassCode().equals("5A A5 02 05")){ Intent intent = new Intent(getActivity(), TherapyHelmetNineteenActivity.class); intent.putExtra("devices",device); startActivity(intent); } } /** * 像素转 sp * @param px * @return */ private float pxToSp(float px) { return px / getResources().getDisplayMetrics().scaledDensity; } /** * dp 转像素工具方法 */ private int dpToPx(int dp) { float density = getResources().getDisplayMetrics().density; return Math.round(dp * density); } /** * 获取用户设备列表 */ private void loadActivity() { // 创建Retrofit服务实例 RetrofitAPI api = RetrofitClient.getClient(getActivity()).create(RetrofitAPI.class); // 发起异步请求 api.deviceFindAll().enqueue(new Callback<List<Device>>() { @Override public void onResponse(Call<List<Device>> call, Response<List<Device>> response) { if (response.isSuccessful() && response.body() != null) { addNewButton(response.body()); deviceLists = response.body(); } } @Override public void onFailure(Call<List<Device>> call, Throwable t) { if (getActivity() != null) { Toast.makeText(getContext(), "网络错误: " + t.getMessage(), Toast.LENGTH_SHORT).show(); // // todo 离线 // DevicesRepository devicesRepository = new DevicesRepository(getActivity()); // List<Devices> deviceByUsername = devicesRepository.getDevicesByUserId(1L); // List<Device> list = new ArrayList<>(); // for (Devices devices:deviceByUsername){ // Device device = new Device(); // device.setDeviceMac(devices.getDeviceMac()); // device.setDeviceName(devices.getDeviceName()); // device.setDeviceClassCode(String.valueOf(devices.getDeviceClassId())); // device.setUserId(devices.getUserId()); // list.add(device); // } // addNewButton(list); // deviceLists = list; // // / } } }); } /** * 注册本地广播接收器 * 用于接收来自BluetoothService的事件 */ private void registerReceivers() { IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothService.ACTION_CONNECTION_STATUS); // 连接状态变化 LocalBroadcastManager.getInstance(getActivity()) .registerReceiver(btReceiver, filter); // 注册广播接收器 } /** * 蓝牙事件广播接收器 * 处理三种事件: * 1. 发现新设备 - 更新设备列表 * 2. 连接状态变化 - 更新UI状态 * 3. 接收到数据 - 显示在消息框 */ private final BroadcastReceiver btReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { try { String action = intent.getAction(); if (BluetoothService.ACTION_CONNECTION_STATUS.equals(action)) { // 更新连接状态显示 String status = intent.getStringExtra(BluetoothService.EXTRA_STATUS); if(status.equals("DISCONNECTED")){ updateUnConnectionStatus(); } } } catch (SecurityException e) { throw new RuntimeException(e); } } }; @Override public void onStart() { super.onStart(); Intent intent = new Intent(getActivity(), BluetoothService.class); getActivity().bindService(intent, connection, Context.BIND_AUTO_CREATE); // 绑定服务 } @Override public void onStop() { super.onStop(); if (isBound) { getActivity().unbindService(connection); // 解绑服务‌ isBound = false; } LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(btReceiver); } private OnJumpToActivityListener onJumpToActivityListener; // 在 Fragment 中定义接口 public interface OnJumpToActivityListener { void jumpToActivityAndClose(); } }但设备按钮为4个时 手机版本显示顺序没问题 但平板版本的+add按钮会在第六个按钮的位置 而不是第五个
最新发布
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值