Получилось два фрагмента (layout_a.xml и layout_b.xml). И я просто хочу сделать это: нажмите кнопку (btn1), а затем измените текст textview (tva) в layout_a.xml. Вот мои коды:

activity_main.xml


    
        
    
    

custom_tab.xml



layout_a.xml


    

    

layout_b.xml


    

A_activity.java

public class A_activity extends Fragment{

    public A_activity() {
        // Required empty public constructor

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.layout_a, container, false);
    }
}

B_activity.java

public class B_activity extends Fragment{

        public B_activity() {
            // Required empty public constructor

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

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.layout_b, container, false);
        }
    }

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private int[] tabIcons = {
            R.drawable.a,
            R.drawable.b,
    };

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

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        setupTabIcons();
        Button btn1 = (Button) findViewById(R.id.btn1);
        TextView tva = (TextView) findViewById((R.id.tva));
//        btn1.setOnClickListener(new View.OnClickListener() {
//            public void onClick(View v) {
//                tva.setText("Clicked");
//            }
//        };

    }

    private void setupTabIcons() {
        TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabOne.setText("a activity");
        tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.a, 0, 0);
        tabLayout.getTabAt(0).setCustomView(tabOne);

        TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabTwo.setText("b activity");
        tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.b, 0, 0);
        tabLayout.getTabAt(1).setCustomView(tabTwo);

    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new A_activity(), "A activity");
        adapter.addFrag(new B_activity(), "B activity");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List mFragmentList = new ArrayList<>();
        private final List mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}

Ответы (1)

Измените A_activity onCreateView () на следующий

View view = inflater.inflate(R.layout.layout_a, container, false);

Button btn1 = (Button) view.findViewById(R.id.btn1);
final TextView tva = (TextView) view.findViewById((R.id.tva));
btn1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        tva.setText("Clicked");
    }
};

return view;

2022 WebDevInsider