Android 如何从片段中获取数据并将其传输到另一个片段?
我有一节新课。我必须以片段的形式显示一些信息。例如:recyclerView包含:id、姓名、姓氏。我该怎么做?id列表将显示在第一个片段中,然后我单击条目id,在第二个片段中我得到这个id的名称和姓氏 我的recyclerView类:Android 如何从片段中获取数据并将其传输到另一个片段?,android,android-recyclerview,fragment,Android,Android Recyclerview,Fragment,我有一节新课。我必须以片段的形式显示一些信息。例如:recyclerView包含:id、姓名、姓氏。我该怎么做?id列表将显示在第一个片段中,然后我单击条目id,在第二个片段中我得到这个id的名称和姓氏 我的recyclerView类: public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.NewsViewHolder> { private LayoutInflater inflater;
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.NewsViewHolder> {
private LayoutInflater inflater;
private List<Source> sources;
public NewsAdapter(Context context, List<Source> sources) {
this.sources = sources;
this.inflater = LayoutInflater.from(context);
}
@NonNull
@Override
public NewsAdapter.NewsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.news_item, parent, false);
return new NewsViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final NewsViewHolder holder, int position) {
final Source source = sources.get(position);
holder.sourceId.setText(source.getId());
holder.sourceName.setText(source.getName);
holder.sourceSurname.setText(source.getSurname);
}
@Override
public int getItemCount() {
return sources.size();
}
public class NewsViewHolder extends RecyclerView.ViewHolder {
TextView sourceName, sourceId, sourceSurname;
public NewsViewHolder(View view) {
super(view);
sourceName = view.findViewById(R.id.sourceName);
sourceId = view.findViewById(R.id.sourceId);
sourceDescription = view.findViewById(R.id.sourceSurname);
sourceId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putString("sourceName", sourceName.getText().toString());
bundle.putString("sourceSurname", sourceSurname.getText().toString());
NewsFragment newsFragment = new NewsFragment();
newsFragment.setArguments(bundle);
AppCompatActivity activity = (AppCompatActivity) v.getContext();
activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment2, newsFragment).addToBackStack(null).commit();
Toast.makeText(itemView.getContext(), "Item clicked. ", Toast.LENGTH_SHORT).show();
}
});
你可以很容易地处理它 我在这件事上使用的是如下所示的片段构造函数
public class NewsFragment extends Fragment {
String param1,param2;
public NewsFragment(String param1, String param2)
{
this.param1=param1;this.param2=param2;
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
.....
return view;
}
}
当你为你的片段设置构造器时,当你想使用它时,它会强迫你输入参数
还有其他方法,但我发现最好的方法是使用片段构造函数。我必须转移到这一行:
NewsFragment NewsFragment=new NewsFragment()
?@MonicaNewsFragment NewsFragment=新的新闻片段(param1,param2)代码>也许我做错了什么,它不工作。在片段类中:公共新闻片段(String sourceNameItem){this.sourceNameItem=sourceNameItem;}
字符串sourceNameItem=bundle.getString(“sourceName”)适配器类中的code>:NewsFragment NewsFragment=new NewsFragment(sourceNameItem)这是我的错误。我对片段做了错误的xml布局。现在没事了。谢谢你的建议。
public class NewsFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details_fragment, container, false);
TextView textView1 = view.findViewById(R.id.sourceName);
TextView textView2 = view.findViewById(R.id.sourceSurname);
Bundle bundle = this.getArguments();
if (bundle != null) {
String sourceName = bundle.getString("sourceName");
textView1.setText(sourceName);
String sourceSurname = bundle.getString("sourceSurname");
textView2.setText(sourceSurname);
}
return view;
}
}
public class NewsFragment extends Fragment {
String param1,param2;
public NewsFragment(String param1, String param2)
{
this.param1=param1;this.param2=param2;
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
.....
return view;
}
}