java—如何从显示的活动中设置片段textview

8e2ybdfx  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(280)

我有 TestsFragment 那个 extendsFragment . 在这个城市的布局中 Fragment 有一个 TextView (id:textview)有一个 Activity 打电话 MainActivity :这个 Activity 包含 FrameLayout 这表明 Fragment 我想设置文本视图文本
onCreate MainActivity 为此,我想我需要这个观点 Fragment 我尝试了以下代码(oncreate-mainactivity):

Fragment fragment = new TestsFragment();
View v = getLayoutInflater().inflate(R.layout.fragment_tests, null);
TextView tv = v.findViewById(R.id.textview);
tv.setText("text changed!");

但它不起作用(没有错误文本只是没有改变)
你能帮我吗?如果您需要任何代码,请告诉我!
主要活动:

public class MainActivity extends AppCompatActivity {
BottomNavigationView bottomNavigationView;
Fragment testsFragment, signsFragment, practiceFragment, accountFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bottomNavigationView = findViewById(R.id.bottom_navigation);
    bottomNavigationView.setOnNavigationItemSelectedListener(navListener);

    if(savedInstanceState == null){
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, new TestsFragment(), "TestsFragment").commit();
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, new SignsFragment()).commit();
        getSupportFragmentManager().executePendingTransactions();
    }

}

@Override
protected void onStart() {
    super.onStart();
    testsFragment = getSupportFragmentManager().findFragmentByTag("TestsFragment");
    if(testsFragment != null){
        TextView textView = testsFragment.getView().findViewById(R.id.textview);
        textView.setText("Text Changed!");
    }
}

private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment selectedFragment = null;

        switch (item.getItemId()){
            case R.id.nav_signs:
                selectedFragment = new SignsFragment();
                break;

            case R.id.nav_tests:
                selectedFragment = testsFragment;
                break;

            case R.id.nav_practice:
                selectedFragment = new PracticeFragment();
                break;

            case R.id.nav_account:
                selectedFragment = new AccountFragment();
                break;
        }

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();
        return true;
    }
};
}

xml(布局):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#007AFF" android:orientation="vertical">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerview_tests"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    android:paddingHorizontal="5dp">

</androidx.recyclerview.widget.RecyclerView>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="text not changed"
    android:id="@+id/textview"/>

</LinearLayout>
guz6ccqo

guz6ccqo1#

解决方案
当用户单击每个菜单项时,总是创建一个新的片段,这不是一个好的用户体验,因为当用户在菜单项之间切换时,他们总是看到一个新的屏幕而不是离开之前的屏幕。例如,在 SignsFragment ,有一个 EditText 当用户切换到 AccountFragment 然后切换回 SignsFragment ,的 EditText 将为空(但用户希望看到“bob”)。
为了防止这种行为,我们将使用 addToBackStack() 方法,所以 FragmentManager 将保留对所有创建片段的引用。如果片段不存在于 FragmentManager ,它将创建一个新的,否则,它将重用现有的。
主活动.java

public class MainActivity extends AppCompatActivity {

    private static final String TAG_SIGNS_FRAGMENT = "TAG_SIGNS_FRAGMENT";
    private static final String TAG_TESTS_FRAGMENT = "TAG_TESTS_FRAGMENT";
    private static final String TAG_PRACTICE_FRAGMENT = "TAG_PRACTICE_FRAGMENT";
    private static final String TAG_ACCOUNT_FRAGMENT = "TAG_ACCOUNT_FRAGMENT";

    BottomNavigationView bottomNavigationView;

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

        bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(navListener);
        bottomNavigationView.setSelectedItemId(R.id.nav_signs);
    }

    private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            String tag = null;

            // Find the tag of fragment based on menu item position.
            switch (item.getItemId()) {
                case R.id.nav_signs:
                    tag = TAG_SIGNS_FRAGMENT;
                    break;
                case R.id.nav_tests:
                    tag = TAG_TESTS_FRAGMENT;
                    break;
                case R.id.nav_practice:
                    tag = TAG_PRACTICE_FRAGMENT;
                    break;
                case R.id.nav_account:
                    tag = TAG_ACCOUNT_FRAGMENT;
                    break;
            }

            // Find the fragment in FragmentManager.
            Fragment fragment = getSupportFragmentManager().findFragmentByTag(tag);

            // If the fragment is not existed, create a new instance of it, otherwise
            // use the existing one.
            if (fragment == null) {
                switch (tag) {
                    case TAG_SIGNS_FRAGMENT:
                        fragment = new SignsFragment();
                        break;
                    case TAG_TESTS_FRAGMENT:
                        fragment = new TestsFragment("text changed!");
                        break;
                    case TAG_PRACTICE_FRAGMENT:
                        fragment = new PracticeFragment();
                        break;
                    case TAG_ACCOUNT_FRAGMENT:
                        fragment = new AccountFragment();
                        break;
                }
            }
            // Show the fragment on screen.
            showFragment(fragment, tag);
            return true;
        }
    };

    private void showFragment(Fragment fragment, String tag) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, fragment, tag)
                .addToBackStack(tag)
                .commit();
        getSupportFragmentManager().executePendingTransactions();
    }
}

相关问题