我正在尝试从我的适配器获取一些数据但没有成功。我在我的主要活动功能中获得了一些项目位置,需要从该项目中获取项目名称。我可以弄清楚如何实现这一目标。如果有人可以帮我修理它,我会很高兴。
我的主要活动的功能 -
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition();
if (direction == ItemTouchHelper.LEFT) {
Log.e(TAG, RecyclerAdapter.getIdFromPosition(position)); // Here what I try to do but to achieve that the getIdFromPosition
function need to be static and I can do this function to be static
} else {
Log.e(TAG,"Right");
}
}
适配器功能 -
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
List<Task> data= Collections.emptyList();
public RecyclerAdapter(MainActivity mainActivity, List<Task> data) {
this.data = data;
}
@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_list, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Task current = data.get(position);
//holder.tv1.setText(name[position]);
ViewHolder.tv2.setText(current.data);
if(current.sync != "null"){
holder.tv1.setText("A");
}
holder.imageView.setTag(holder);
}
@Override
public int getItemCount() {
return data.size();
}
public void getIdFromPosition(int position){
Log.e(TAG, data.get(position).getId());
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public TextView tv1;
public static TextView tv2;
public ImageView imageView;
public ViewHolder(View view) {
super(view);
tv1 = (TextView) view.findViewById(R.id.list_title);
tv2 = (TextView)view.findViewById(R.id.list_desc);
imageView = (ImageView) view.findViewById(R.id.list_avatar);
view.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
//Log.e("test","test");
}
});
}
}
答案 0 :(得分:0)
public interface RecyclerClick {
void onClick(Object o);
}
现在将接口添加为适配器构造函数中的参数:
public RecyclerAdapter(MainActivity mainActivity, List<Task> data, RecyclerClick click) {
this.data = data;
this.click = click;
}
在视图内,调用onClick:
view.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
//Log.e("test","test");
//Pass any object u want
click.onClick(current);
}
});
在主活动中实现此接口并传递this
作为适配器构造函数的参数:
adapter = new RecyclerAdapter(this, data, this);
现在,主要活动中的onClick方法将在每次点击时调用所需的数据/对象。
答案 1 :(得分:0)
要实现RecyclerView
的滑动,您需要在托管ItemTouchHelper.SimpleCallback
适配器的活动或片段中实施RecyclerView
:
这样的事情:
ItemTouchHelper touchHelper = new ItemTouchHelper(
new ItemTouchHelper.SimpleCallback(ItemTouchHelper.LEFT) {
@Override
public abstract boolean onMove(RecyclerView recyclerView,
ViewHolder viewHolder, ViewHolder target) {
return true;// true if moved, false otherwise
}
@Override
public void onSwiped(ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition();
MyData dataObj = myDataList.get(position);
// execute whatever you choose to here.
}
});
然后,当您设置RecyclerView
时,请附上ItemTouchHelper
:
touchHelper.attachToRecyclerView(yourRecyclerView);