如何从Activity1的RecyclerView适配器向Activity2的ViewPager碎片传递数据?
Got it, let's work through this properly. The core mistake here is trying to set arguments for a Fragment belonging to Activity2 directly from Activity1's adapter—Activity2 hasn't been launched yet, so that approach won't work. The right flow is: send data from the adapter to Activity2 first, then pass that data from Activity2 to its ViewPager's Fragment.
Here's the step-by-step solution:
Step 1: Pass Data from RecyclerView Adapter to Activity2 via Intent
In your adapter's onBindViewHolder click listener, use an Intent to send the required data to Activity2 instead of trying to create and configure the Fragment directly:
@Override public void onBindViewHolder(ViewHolder holder, final int position) { holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Grab the data you need to pass String projectName = jobCardLists.get(position).getProject(); // Create intent to launch Activity2 Intent intent = new Intent(view.getContext(), Activity2.class); // Add the data to the intent (use a unique key for the extra) intent.putExtra("EXTRA_PROJECT_NAME", projectName); // Start Activity2 view.getContext().startActivity(intent); } }); }
Step 2: Receive Data in Activity2 and Pass It to the ViewPager Fragment
First, Activity2 needs to catch the data sent from Activity1. Then, when setting up the ViewPager's adapter, pass that data to the target Fragment using setArguments().
2.1 Receive Data in Activity2
public class Activity2 extends AppCompatActivity { private String mReceivedProjectName; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_2); // Fetch the data from the incoming intent if (getIntent() != null && getIntent().hasExtra("EXTRA_PROJECT_NAME")) { mReceivedProjectName = getIntent().getStringExtra("EXTRA_PROJECT_NAME"); } // Initialize ViewPager and its adapter ViewPager viewPager = findViewById(R.id.your_view_pager_id); viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager())); } }
2.2 Create ViewPager Adapter to Pass Data to Fragment
Use a custom FragmentPagerAdapter (or FragmentStatePagerAdapter for larger datasets) to create your Fragment instance and attach the arguments:
private class ViewPagerAdapter extends FragmentPagerAdapter { public ViewPagerAdapter(FragmentManager fm) { super(fm, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); } @Override public Fragment getItem(int position) { // Initialize your DetailsFragment DetailsFragment detailsFragment = new DetailsFragment(); // Create a Bundle to hold the data Bundle args = new Bundle(); args.putString("projectName", mReceivedProjectName); // Attach the bundle to the Fragment detailsFragment.setArguments(args); return detailsFragment; } @Override public int getCount() { // Return the number of pages in your ViewPager (adjust as needed) return 1; } }
Step 3: Retrieve the Data in DetailsFragment
Finally, pull the data from the Fragment's arguments in its lifecycle methods (like onCreate or onViewCreated):
public class DetailsFragment extends Fragment { private String mProjectName; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the data from arguments if (getArguments() != null) { mProjectName = getArguments().getString("projectName"); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_details, container, false); // Use the data to update your UI (example with a TextView) TextView projectNameTv = view.findViewById(R.id.tv_project_name); if (mProjectName != null) { projectNameTv.setText(mProjectName); } return view; } }
Key Notes to Remember
- Don't create Activity2's Fragment in Activity1's adapter: The Fragment is tied to Activity2's lifecycle, which hasn't started yet—this will cause unexpected behavior.
- Use Intents for Activity-to-Activity communication: This is the standard Android pattern for passing data between activities.
- For complex objects: If you need to pass something other than a String, make sure the object implements
ParcelableorSerializable, then useintent.putExtra("EXTRA_OBJECT", yourParcelableObject)to send it.
内容的提问来源于stack exchange,提问作者Adarsh Vijayan P

