i would like to ask about ViewPager i had created 2 layouts in viewPager where you can slide between them. but i totaly do not know how to gain access to buttons inside of theese 2 views.
Button deleteButton; TableLayout orderTable; TableRow order; TableLayout preparedTable; TableRow prepared; TextView plus; JSONObject rows = new JSONObject( ); int last = 0; private ViewPager tablePager; private PagerAdapter pagerAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sliding_panel); getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); getSupportActionBar().hide(); //orderTable.addView(order); List<Fragment> list = new ArrayList<>(); list.add(new PageFragment1()); list.add(new PageFragment2()); tablePager = findViewById(R.id.main_view_pager); pagerAdapter = new SlidePageAdapter(getSupportFragmentManager(),list); tablePager.setAdapter(pagerAdapter); tablePager.setCurrentItem(0); setMain(); /*LayoutInflater getView = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = getView.inflate(R.layout.main, null); plus = v.findViewById(R.id.plus); plus.setOnClickListener(this);*/ } private void setMain() { View main = getLayoutInflater().inflate(R.layout.main, tablePager, false); orderTable = (TableLayout) main.findViewById(R.id.main_orders_table); order = (TableRow) main.findViewById(R.id.main_orders_rows); preparedTable = (TableLayout) main.findViewById(R.id.main_prepared_table); prepared = (TableRow) main.findViewById(R.id.main_prepared_rows); plus = (TextView) main.findViewById(R.id.plus); plus.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.plus: addOrder(); break; } }
so how could i call something inside of one of theese views? this does not trigered the onclicklistener.
please help thanks.
Advertisement
Answer
you should access the button from within the fragment by finding the button id
Button mybtn=getActivity().findViewById(yourbtnid);