[英]How can I send text value from ViewHolder to ItemTouchHelper class?
I've been learning Java and Android Development for about a month. 我已经学习Java和Android开发大约一个月了。 Maybe, I don't know some features... But I can't find any advise for a week.
也许我不知道某些功能...但是我一周都找不到任何建议。 I need to send value from one of my fields in ViewHolder (created by onBindViewHolder) to overrided method onChildDraw (in class, that extends ItemTouchHelper).
我需要将值从ViewHolder(由onBindViewHolder创建)中的一个字段发送到重写的方法onChildDraw(在类中,该类扩展了ItemTouchHelper)。 But I can't understand, how can I do this.
但是我不明白,我该怎么做。 I want to call new activity after swipe action and send value of field "name" to that activity.
我想在滑动动作后调用新活动,并将字段“名称”的值发送给该活动。 Can I do this with Intent?
我可以用Intent做到这一点吗? Or maybe have other way?
也许还有其他方法?
Creating ViewHolder (public class ModelsDataAdapter extends RecyclerView.Adapter): 创建ViewHolder(公共类ModelsDataAdapter扩展了RecyclerView.Adapter):
@Override
public void onBindViewHolder(ModelsViewHolder holder, int position) {
ModelsData model = models.get(position);
holder.name.setText(model.getName()); // <-- I NEED THIS VALUE
holder.tags.setText(model.getTags());
holder.keywords.setText(model.getKeywords());
holder.cost.setText(model.getCost().toString());
//-- some code --//
}
My onChildDraw (public class SwipeControl extends ItemTouchHelper.Callback): 我的onChildDraw(公共类SwipeControl扩展了ItemTouchHelper.Callback):
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
//-- some code --//
int x = 0;
if (dX < -200){ //
viewHolder.itemView.findViewById(R.id.card_foreground).setTranslationX(dX / 3);
} else if (dX > 200){
viewHolder.itemView.findViewById(R.id.card_foreground).setTranslationX(dX / 3);
openStatCard();
} else if (dX == 0){
checkActivity = false;
}
}
Opening new activity (inside SwipeControl.class): 打开新活动(在SwipeControl.class内部):
private void openStatCard(){
if (!checkActivity) {
checkActivity = true;
Intent intent = new Intent(context, StatCards.class);
intent.putExtra("choosingModel", model); // <-- PUT "name" IN "model"
context.startActivity(intent);
}
}
Inside onChildDraw
method use viewHolder.getAdapterPositon() method to get the position. 在
onChildDraw
方法内部,使用viewHolder.getAdapterPositon()方法获取位置。 And when you need the value just use that position to find Name
from the models
list 当您需要该值时,只需使用该位置从
models
列表中查找Name
EDIT 编辑
Inside onChildDraw
get the TextView using viewHolder.itemView.findViewById(R.id.name);
在
onChildDraw
内,使用viewHolder.itemView.findViewById(R.id.name);
获取TextView viewHolder.itemView.findViewById(R.id.name);
and then use getText()
method to get the Name. 然后使用
getText()
方法获取名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.