You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:点击搜索按钮在新Activity加载Fragment搜索结果

How to Move Search Results to a New Activity

Got it, let's walk through how to adjust your existing Fragment to launch a new Activity for search results. Here's a step-by-step breakdown with code examples:

Step 1: Create the Search Results Activity & Layout

First, we need a new Activity to display the results. Let's call it SearchResultsActivity.

1.1 Create the Layout File (activity_search_results.xml)

This layout will hold the RecyclerView for your results:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- Add a RecyclerView to display results -->
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/results_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

1.2 Create the Search Results Activity (SearchResultsActivity.java)

This Activity will retrieve the search query, fetch results, and populate the RecyclerView:

public class SearchResultsActivity extends AppCompatActivity {
    private RecyclerView resultsRecyclerView;
    private YourResultAdapter resultAdapter; // Replace with your existing adapter
    private List<YourResultModel> resultsList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_results);

        // Initialize RecyclerView
        resultsRecyclerView = findViewById(R.id.results_recycler_view);
        resultsRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        resultAdapter = new YourResultAdapter(resultsList); // Reuse your existing adapter
        resultsRecyclerView.setAdapter(resultAdapter);

        // Get the search query from the Intent
        String searchQuery = getIntent().getStringExtra("SEARCH_QUERY");
        
        // Perform your search logic here (reuse the code from your Fragment)
        if (searchQuery != null && !searchQuery.isEmpty()) {
            fetchSearchResults(searchQuery);
        }
    }

    // Reuse your existing search method here
    private void fetchSearchResults(String query) {
        // Replace this with your actual search logic (API call, database query, etc.)
        resultsList.clear();
        resultsList.addAll(yourSearchDataSource.getResults(query));
        resultAdapter.notifyDataSetChanged();
    }
}

Step 2: Update Your Fragment's Search Button Action

In your existing Search.java Fragment, modify the search button's click listener to launch the new Activity instead of loading results locally:

public class Search extends Fragment {
    private EditText searchEditText;
    private Button searchButton;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout, container, false);
        
        searchEditText = view.findViewById(R.id.search_edit_text);
        searchButton = view.findViewById(R.id.search_button);

        searchButton.setOnClickListener(v -> {
            String query = searchEditText.getText().toString().trim();
            if (!query.isEmpty()) {
                // Launch SearchResultsActivity with the query
                Intent intent = new Intent(getActivity(), SearchResultsActivity.class);
                intent.putExtra("SEARCH_QUERY", query);
                startActivity(intent);
            } else {
                // Show error if query is empty
                Toast.makeText(getActivity(), "Please enter a search query", Toast.LENGTH_SHORT).show();
            }
        });

        return view;
    }
}

Step 3: Add the Activity to AndroidManifest.xml

Don't forget to declare the new Activity in your manifest:

<manifest ...>
    <application ...>
        <!-- Your existing activities -->
        <activity android:name=".SearchResultsActivity" />
    </application>
</manifest>

Important Notes

  • Reuse Existing Components: You can reuse your existing RecyclerView adapter and model class in the new Activity—no need to rewrite those.
  • Passing Complex Data: If you need to pass pre-fetched results instead of just the query, make sure your result model implements Parcelable or Serializable, then add it to the Intent with putExtra("RESULTS", resultsList).
  • Configuration Changes: Handle screen rotations or other config changes as needed (using ViewModel or saving instance state) to retain results.

That's it! Now when users click the search button, they'll be taken to a new Activity where the results are displayed.

内容的提问来源于stack exchange,提问作者user7810601

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:38:49