- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在使用 setTag
方法存储的 ViewHolder 中保留 Activity 的句柄是否安全?
我发现这个问题声称存储对 Activity 的引用可能会导致内存泄漏,但它已在 Android 4.0 中修复:https://code.google.com/p/android/issues/detail?id=18273
具体来说,我想知道拥有一个看起来像这样的 ViewHolder 是否安全:
class MyHolder {
private Context context; // <<-- is this safe to keep here??
private TextView textView;
public MyHolder(Context context) {
this.context = context;
}
public void populate(Doc doc) {
textView.setText(context.getString(doc.getTextId()));
}
public View inflate(ViewGroup parent) {
View view = LayoutInflater.from(parent.getContext()).inflate(
R.layout.doc_item, parent, false);
textView = (TextView)view.findViewById(R.id.doc_item_text);
return view;
}
}
像这样在我的 ArrayAdapter 中使用 getView 方法:
@Override
public View getView(int position, View row, ViewGroup parent) {
Doc doc = getItem(position);
MyHolder holder;
if (row != null) {
holder = (MyHolder) row.getTag();
} else {
holder = new MyHolder(getContext());
row = holder.inflate(parent);
row.setTag(holder);
}
holder.populate(doc);
return row;
}
(该代码是实际代码库的简化版本,只是为了说明这一点。)
我见过的所有示例代码都没有存储对持有者中 View 以外的任何内容的引用。我想知道这是巧合还是故意为之。
最佳答案
无论在这种情况下是否安全,始终最好将上下文引用保持在最低限度。您在 MyHolder
中使用 context
所做的一切都可以转换为在 getView()
中执行的操作,并使用 Adapter 中保存的 Context 引用。这是设计使然,因为肯定不需要您的设计所需的多个上下文引用。
关于android - 在 ViewHolder 中引用 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25615489/
最近我使用 RecyclerView 并添加了一个自定义标题 View (另一种类型的项目 View )并尝试在数据发生更改时对其进行更新。奇怪的事情发生了。适配器创建一个新的 HeaderViewH
我的项目应该基于用户输入(用户发送文本、拍照或录制视频)创建多个 View ,类似于 WhatsApp 聊天 Activity ,结构几乎相同。适配器应该能够通过使用 getItemViewType(
在 ViewHolder pattern 中将 ViewHolder 设为静态对性能至关重要吗? ? A ViewHolder object stores each of the component
我只是想更好地理解我经常用来优化 ListView 的以下模式 我的阅读只指出静态内部类被视为顶级类的事实。与成员类(非静态)相比,这样的事情有什么好处? @Override public View
我有一个 RecyclerView 和一些 view/card(我们暂时称它为 View ),它们都包含相同的东西,包括我用作分隔栏的 View。 我希望能够在当前 View 上方的 view 中更改
我需要从 View Holder 访问 RecyclerView Adapter 的方法。我找不到任何解决方案。 或者是否可以从 ViewHolder 的 ViewModel 类(我已经在 MVVM
在我的 RecyclerView 适配器中只有一个 ViewHolder 类: public static class PlayerItemViewHolder extends RecyclerVie
我已经采用了一个示例代码来实现 RecyclerView,但试图将其转换为在我的应用程序的子 fragment 中工作。 代码在“创建列表 - 示例”下 Creating Lists and Card
我试图在我的主要 Activity 中而不是在我的自定义适配器中保存复选框的状态。我已检查共享首选项中是否存储了正确的数据,并且可以成功检索信息,但是当我尝试在打开应用程序时标记复选框时, View
我是 Kotlin 的新手,我正在制作一款货币兑换应用。在适配器中,我想将一些项目传递到新 Activity 中。 class AdapterC (val countryList: ArrayLis
我在我的代码中遇到了这个问题,当在 Viewholder 中的一个按钮上进行转换时,这是在 Onclicklistener 中完成的,多个转换发生在不同的行上即,如果我单击第 1 行中的按钮,则按钮向
public class ListViewAdapter extends BaseAdapter { private Context context; private LocationDetails[
我刚刚生成了一个 Master/Detail Flow 项目,我发现了一些奇怪的东西:在 DriverListActivity.java 中,名为 ViewHolder 的子类具有 final 属性。
我正在尝试配置抽屉导航以显示 3 种不同类型的“行”,但我在使用 Holder 时遇到了问题,被告知该消息: FATAL EXCEPTION: main java.lang.ClassCastExce
我理解Viewholder pattern的思路和用法,但我仍然有一个问题: 假设我们在 viewholder 中有一个 TextView,并显示 10 个项目(“item0,item1 ....”)
我有一个包含 3 种不同类型行的 ListView 。我在网上收到 ClassCastException holder = (RowViewHolder) row.getTag(); 我注意到 row
我正在尝试学习 Android 编程。我找不到这个算法的解释: public View getView(int r, View convertView, ViewGroup parent) { V
我正在创建一个 Android 应用程序,其中包含一个带有嵌套 CardView 的 RecyclerView。我需要将所有其他卡片替换为不同的颜色。我正在使用 @Override 来覆盖 onBin
我在 ListView 中使用分段时遇到问题。我需要使用 viewholder 使 listview 滚动平滑,但我不知道如何实现两个 viewholders,因为有两个单独的 View ,一个是部分
我在 ListView 上遇到了 View 持有者的充气问题。实现 Viewholder 后,充气机会出错,因此消息显示错误。 (在图 10 中,消息不是来 self ,而且每当我滚动时,错误的消息都
我是一名优秀的程序员,十分优秀!