更新标签视图文本

时间:2017-12-06 08:20:51

标签: java android android-fragments

我从Android开始,我有一个NavigationView以及Tabular布局。我想根据导航视图中的选择更改选项卡片段中的值。这是我的代码 -

MainActivity.java

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    Context mainActivityContext;
    Intent serviceIntent;

    SharedPreferences sharedPref;
    SharedPreferences.Editor editor;

    private SectionsPagerAdapter mSectionsPagerAdapter;
    private String valSelected = "";
    private ViewPager mViewPager;

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


        editor  = sharedPref.edit();

        mainActivityContext = this;

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);


        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());


        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.sel1) {

            valSelected = "First";
            NotificationFragment fragment_obj = (NotificationFragment)getSupportFragmentManager().
                    findFragmentById(R.id.title_notification);

            fragment_obj.updateView();
        }


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }





    public static class PlaceholderFragment extends Fragment {

        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }


        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main2, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);

             textView.setText("HELLO");
            return rootView;
        }
    }




    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            if(position == 0) {
                return new NotificationFragment();

            }
            return PlaceholderFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {

            return 4;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Tab1";
                case 1:
                    return "Tab2";
                case 2:
                    return "Tab3";
                case 3:
                    return "Tab4";


            }
            return null;
        }

        @Override
        public int getItemPosition(Object object) {
            if (object instanceof NotificationFragment) {
                ((NotificationFragment)object).updateView();
            }
            return super.getItemPosition(object);
        }
    }
}

这是我的NotificationFragment 的 NotificationFragment.java

public class NotificationFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        View rootView = inflater.inflate(R.layout.notification_layout, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.title_notification);

        textView.setText(MainActivity.coinSelected.toUpperCase());
        return rootView;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    public void updateView(){
        TextView text = (TextView) this.getView().findViewById(R.id.title_notification);
        text.setText(MainActivity.valSelected);
    }

}

但是当我尝试使用

更新MainActivity中的值时
NotificationFragment fragment_obj = (NotificationFragment)getSupportFragmentManager().
                    findFragmentById(R.id.title_notification);

它给了我null

修改 这是通知布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >


    <TextView
        android:id="@+id/title_notification"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BTC/USD"
        android:textSize="26dp"/>

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

我在MainActivity.java中导入android.support.v4.app.Fragment时遇到过这个问题;在我的其他课程中我导入了android.app.Fragment。我把它改成了同样的东西,findFragmentById(R.id)现在返回了正确的片段。

答案 1 :(得分:0)

你的代码中的

NotificationFragment fragment_obj =(NotificationFragment)getSupportFragmentManager()。                     findFragmentById(R.id.title_notification); R.id.title_notification必须是片段的id,但在你的xml中,它是一个textview