Android Fragments 在单击“查看项目”时调用片段,并在同一Activity中同时显示它们

nnsrf1az  于 2023-11-19  发布在  Android
关注(0)|答案(2)|浏览(125)

由于我只从buttons更改为ClerClerView(它在buttons上工作得很好),我知道我的java代码工作得很好,所以现在我面临一个问题,因为单击ClerClerView中的卡片会使用FragmentManager调用片段,但不会显示它。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".mainSearch"
        android:background="#f2f2f2">

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/recyclerView"
            android:orientation="horizontal" />

    </androidx.constraintlayout.widget.ConstraintLayout>
    <fragment
        android:name="com.diamcom.blue.StoneCodeFragment"
        android:id="@+id/fragment_place"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </fragment>
</LinearLayout>

字符串
我想知道我做错了什么?

bf1o4zei

bf1o4zei1#

我认为你在这里遇到的问题是,在宽度和高度上都将容器视图和片段作为match_parent。我认为你可以这样做的一种方法是将容器视图 Package 在一个框架中,当你点击容器视图中的任何一张卡片时,容器视图就会消失。

vof42yt1

vof42yt12#

Try this

<fragment
    android:name="com.diamcom.blue.StoneCodeFragment"
    android:id="@+id/fragment_place"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone">
</fragment>

public class mainSearch extends AppCompatActivity {
    private RecyclerView recyclerView;
    private StoneCodeFragment stoneCodeFragment;

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

        // Initialize your RecyclerView and adapter here
        recyclerView = findViewById(R.id.recyclerView);
        MyAdapter adapter = new MyAdapter(/* ... */);
        recyclerView.setAdapter(adapter);

        // Initialize your StoneCodeFragment
        stoneCodeFragment = (StoneCodeFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_place);

        // Set a click listener for your RecyclerView items
        recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this, recyclerView, new ClickListener() {
            @Override
            public void onClick(View view, int position) {
                // Handle the item click here
                // Show the fragment
                showFragment();
            }

            @Override
            public void onLongClick(View view, int position) {
                // Handle long click if needed
            }
        }));
    }

    // Method to show the fragment
    private void showFragment() {
        if (stoneCodeFragment != null) {
            stoneCodeFragment.getView().setVisibility(View.VISIBLE);
        }
    }
}

字符串

相关问题