我有适配器和片段。
考虑这种情况:
适配器具有编辑文本
默认情况下,编辑文字不可编辑
这是片段代码:
editIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Adapter.makeEditTextEditable();
}
});
这里是适配器代码:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.layout, null);
holder = new ViewHolder();
holder.EditText = (EditText) convertView.findViewById(R.id.edit_text);
} else {
holder = (ViewHolder) convertView.getTag();
}
}
// This is method i am calling from fragment to make editText editable
public void makeEditTextEditable() {
// How do i get holder object and view object here? OR
// How do i make edit text editable from this method?
}
答案 0 :(得分:3)
boolean mEditTextEditable = false;
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.layout, null);
holder = new ViewHolder();
holder.EditText = (EditText) convertView.findViewById(R.id.edit_text);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.EditText.setEnabled(mEditTextEditable);
}
public void setEditTextEditable(boolean editable) {
if(editable == mEditTextEditable) return;
mEditTextEditable = editable;
notifyDatasetChange();
}
你要求在getView()方法之外获取视图,但我不建议,让ListView / RecyclerView不必单独处理这些视图的重点