我有RecyclerView.位置的元素是0是标题EditText,然后所有其他元素都是图像.长按EditText它会给出选项粘贴.这很好用.但是,当我将回收器视图滚动到底部并再次到达顶部并长按时,它将不会显示粘贴选项并给出错误.在滚动到底部并再次到达顶部传递呼叫onBindViewHolder.
TextView:TextView不支持文本选择.选择取消.
public class Someclass extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
private static final int TYPE_EDIT = 0;
private static final int TYPE_IMAGE = 1;
List<String> msomelist = new ArrayList<String>();
public void someMethod(List<String> somelist) {
msomelist.clear();
msomelist.addAll(somelist);
notifyDataSetChanged();
}
public Someclass(Activity activity, List<String> somelist) {
this.activity = activity;
this.msomelist.clear();
this.msomelist.addAll(somelist);
mContext = activity;
}
@Override
public int getItemViewType(int position) {
if (position == 0)
return TYPE_EDIT;
return TYPE_IMAGE;
}
@Override
public void onClick(View view) {
int postition = (int) view.getTag();
msomelist.remove(postition);
notifyDataSetChanged();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
ImageView img;
public ViewHolder(View itemView) {
super(itemView);
img = (ImageView) itemView.findViewById(R.id.image);
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_IMAGE) {
View view = LayoutInflater.from(mContext)
.inflate(somelayout, false);
ViewHolder holder = new ViewHolder(view);
return holder;
} else {
View view = LayoutInflater.from(mContext)
.inflate(someotherlayout, false);
return new OtherHolder(view);
}
}
class OtherHolder extends RecyclerView.ViewHolder {
EditText editText;
public OtherHolder(View itemView) {
super(itemView);
editText = (EditText) itemView.findViewById(R.id.ediItext);
editText.requestFocus();
}
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ViewHolder) {
some function.....
} else if (holder instanceof OtherHolder) {
some function
}
}
}
Run Code Online (Sandbox Code Playgroud)
Lis*_*ray 18
这是Android平台中的已知错误.我一开始并不相信,但发布的代码@ user2246055是一个有效的解决方法!
我更喜欢在我的适配器中添加它而不是子类化TextView:
@Override
public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
super.onViewAttachedToWindow(holder);
// Bug workaround for losing text selection ability, see:
// https://code.google.com/p/android/issues/detail?id=208169
holder.textView.setEnabled(false);
holder.textView.setEnabled(true);
}
Run Code Online (Sandbox Code Playgroud)
小智 5
使用以下命令MyEditText.java可以解决您的问题:
public class MyEditText extends EditText {
private boolean mEnabled; // is this edittext enabled
public MyEditText(Context context) {
super(context);
}
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
try {
if (!mEnabled) return;
super.setEnabled(false);
super.setEnabled(mEnabled);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void setEnabled(boolean enabled) {
this.mEnabled = enabled;
super.setEnabled(enabled);
}}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2869 次 |
| 最近记录: |