我需要一些帮助在MainActivity中设置一个监听器,等待一个recyclelerview项目被刷掉。
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
Log.d(TAG, "whats swiped " + viewHolder.getAdapterPosition() + " direction: " + direction + " arrayListCountyNames.get(viewHolder.getAdapterPosition())" + arrayListCountyNames.get(viewHolder.getAdapterPosition()));
final String s = arrayListCountyNames.get(viewHolder.getAdapterPosition());
final String s2 = arrayListStateName.get(viewHolder.getAdapterPosition());
final String s3 = arrayListSourceId.get(viewHolder.getAdapterPosition());
final String s4 = arrayListID.get(viewHolder.getAdapterPosition());
我需要回到MainActivity,因为那是一切都在执行的地方。
答案 0 :(得分:0)
有两种方法可以将数据发送到其他类。
首先创建一个模型类来存储您需要发送的数据示例:
public class Details {
public String s ;
public String s2 ;
public String s3;
public String s4;
}
然后在片段中创建一个Listener(接口),如下所示:
public class BlankFragment extends Fragment {
private OnSwipeMyRecyclerViewListener mListener;
public BlankFragment() {
// Required empty public constructor
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnSwipeMyRecyclerViewListener) {
mListener = (OnSwipeMyRecyclerViewListener) context;
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnSwipeMyRecyclerViewListener{
void onSwipeMyRecyclerView(Details details, int position);
}
}
在onSwiped方法之后,创建详细信息类的对象,并分配您需要传递的所有值,并按如下方式调用监听器:
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
//your code
Details details = new Details();
details.s = s;
details.s2 = s2;
details.s3 = s3;
details.s4 = s4;
mListener.onSwipeMyRecyclerView(details,direction);
}
最后在您的Activity中实现您的监听器,如下所示。
public class MainActivity extends AppCompatActivity implements BlankFragment.OnSwipeMyRecyclerViewListener {
@Override
public void onSwipeMyRecyclerView(Details details, int position) {
//Do whatever want to do with your data here
}
}
现在试着让我们知道......:)