问题
I called the fragements as
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(context, Fragment1.class.getName()));
fragments.add(Fragment.instantiate(context, Fragment2.class.getName()));
mPageAdapter = new PageAdapter(activity.getSupportFragmentManager(),fragments);
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(mPageAdapter);`
,but now in fragment class ,when trying to access the child button as
View v = inflater.inflate(R.layout.fragment1, container, false);
btn = (ImageButton) v.findViewById(R.id.btn);
btn.setOnTouchListener(new OnTouchListener() {}
,but the button is not generating click event,not able to understand the problem. Secondly, how can I open a small window with viewpager and fragments ,above the present window,so user touches the back window front one goes off,and when front window is visible,onclick of one of fragment button ,user can swipe its elements,as here new in use of viewpager,kindly guide
回答1:
It looks like you are not completely understand the meaning of inflater.inflate
, it was the same problem in your previous question.
When calling inflater.inflate
it creat new View object from the xml you give it.
In this code:
View v = inflater.inflate(R.layout.fragment1, container, false);
btn = (ImageButton) v.findViewById(R.id.btn);
btn.setOnTouchListener(new OnTouchListener() {}
You created new view object, than you found it's button and add a touch listener event to it. Unless you are going to add this view to the fragment, the event will never fire.
I think what you meant to do is finding your view using findViewById()
inside your fragment class.
来源:https://stackoverflow.com/questions/20754854/accessing-viewpager-child-elements-for-clicking-and-diplaying-another-view-pager