因为对于activities1到adpter再到activities2再回传数据不是很清楚,现在只能做到activitiy1到activity2数据,activity2返回一个requestcode,但是不知道怎么调用adpter中的position,进而对原界面进行修改,所以只能获取到activities2中的不带位置的信息,或者可以设置activities2的数量与列表数量对应,进而获取具体的位置,但这显然冗余度太高了,研究许久,最终无法解决,暂时无法实现回调函数的功能,现附已完成的代码,等解决后再进行修改
本问题已解决,解决部分在安卓开发学习日记三中,作业部分为安卓开发学习日记一二三中
myadpter
public class Myadapter extends RecyclerView.Adapter<Myadapter.Myholder> {
private View view;
private Context context;
public List<String> list;
private OnItemClickListener mListener;
public Myadapter(Context context,OnItemClickListener listener,List<String> list) {
this.mListener = listener;
this.list=list;
this.context = context;
}
public interface OnItemClickListener {
void onItemClick();
}
@NonNull
@Override
public Myholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
view= LayoutInflater.from(context).inflate(R.layout.item,null,false);
Myholder holder;
holder = new Myholder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull Myholder holder,int position) {
holder.textView.setText(list.get(position));
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onItemClick();
}
})
;
}
@Override
public int getItemCount() {
return list.size();
}
public class Myholder extends RecyclerView.ViewHolder{
private final TextView textView;
public Myholder(@NonNull View itemView) {
super(itemView);
textView=itemView.findViewById(R.id.textView_item);
}
}
Tab_activity
public class Tab_Activity extends AppCompatActivity {
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
Intent intent =getIntent();
String fangke_string=intent.getStringExtra("访客");
TextView textView=findViewById(R.id.tap_textview);
Button button=findViewById(R.id.tab_button);
Context context=this;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1=getIntent();
intent1.putExtra("like",1);
setResult(2,intent1);
textView.setText("收藏成功");
textView.setTextColor(Color.parseColor("#26A65B"));
}
});
textView.setText("欢迎"+fangke_string+"进入tab");
}
}
跳转面
public class lianxiren_Fragment extends Fragment implements Myadapter.OnItemClickListener {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private RecyclerView recyclerView;
public List<String> list = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view;
view = inflater.inflate(R.layout.fragment_lianxiren_, container, false);
recyclerView = view.findViewById(R.id.rec);
for (int i = 0; i < 9; i++) {
list.add("这是第" + i + " 个列表");
}
Context context = getContext();
Myadapter adapter = new Myadapter(context,this,list);
LinearLayoutManager layoutManager = new LinearLayoutManager(context);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Myadapter myadapter=new Myadapter(getContext(),this,list);
if(resultCode==2&&requestCode==666){
};
}
@Override
public void onItemClick() {
Myadapter adapter = new Myadapter(getContext(),this,list);
adapter.setOnItemClickListener(new Myadapter.ItemListenter() {
@Override
public void onItemClick(int position) {
}
});
Intent send_intent=new Intent(getContext(),Tab_Activity.class);
send_intent.putExtra("访客","wrs");
startActivityForResult(send_intent,666);
}