简体   繁体   中英

Same views shared in two adapters

I would like to know is this possible in Android. I am using support library v4. Problem I am having is that I have to show same custom views in two activities using two adapters, one adapter is supporting the view pager and the other is supporting grid. Basically there is a grid with images that you can view and then when you click on element it should open another Activity where you can view same images in a view pager. I tried just returning same views in fragment onCreateView method but get child already have a parent which I would expect, after removing view first using

((ViewGroup) myImageView.getParent()).removeView(myImageView);

I get java.lang.UnsupportedOperationException: removeView(View) is not supported in AdapterView exception which I didn't expected to happen. Know I am stuck because I don't want to create new views but use existing in both adapters. My question is: Could this showing of same view be done or am I on the totally wrong track and should implement this "Adapter link" some other way?

Could this showing of same view be done or am I on the totally wrong track and should implement this "Adapter link" some other way?

There is no way to share views between two activities and you wouldn't want to do that anyway. Even if you used a single activity with fragments you shouldn't share views of an adapter as it could lead you to more problems.

The proper way to do what you want is to recreate the data in the two places you want the same data and this should be easy if you use an adapter. You'll basically need to use the same layout for the ViewPager 's pages in the normal adapter used for the GridView .

I get java.lang.UnsupportedOperationException: removeView(View) is not supported in AdapterView exception which I didn't expected to happen.

A child of AdapterView manages its children through the adapter and the methods like addView() or removeView() are not implemented for them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM