RemoveView Function On ViewPager Not Working - android

I'm trying to remove a view from a Container of type ViewGroup like below, and I cast it into a ViewPager, but I noticed that the removeView() function is not working, as at now, I'm not sure of what I'm doing wrong.
I need help.
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
ViewPager viewPager = (ViewPager)container;
View view = (View)object;
ViewPager.removeView(view); //removeView is not working
layoutInflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view=layoutInflater.inflate(R.layout.custom_layout,null);
ImageView imageView = (ImageView) view.findViewById(R.id.myImageView);
imageView.setImageResource(images[position]);
ViewPager viewPager = (ViewPager)container;
ViewPager.addView(view);
return view;

Yeah. It is works as expected. Instead of doing above, you need to remove the item for the ViewPager adapter. Something like below.
// Delete a page at a `position`
public void deletePage(int position) {
// Remove the corresponding item in the data set
pageIndexes.remove(position);
// Notify the adapter that the data set is changed
notifyDataSetChanged();
}

Related

Android: call adapter directly in viewpager or call fragment per page?

this question is related with this one that I asked before.
I create a viewpager in MainActivity.java like this:
final ViewPager viewPager = (ViewPager) findViewById(R.id.vp_horizontal_ntb);
viewPager.setAdapter(new PagerAdapter() {
#Override
public int getCount() {
return 5;
}
#Override
public boolean isViewFromObject(final View view, final Object object) {
return view.equals(object);
}
#Override
public void destroyItem(final ViewGroup container, final int position, final Object object) {
((ViewPager) container).removeView((View) object);
}
#Override
public Object instantiateItem(final ViewGroup container, final int position) {
if(position==0) {
// here is important!
} else if(position == 1) {
}
...
}
});
Now I want fill each page with some json RecyclerView data list.(get json from network).
each page has independent data list.
For first time, I create a fragment for each page like this:
if (position == 0) {
final View view = LayoutInflater.from(getActivity().getBaseContext()).inflate(R.layout.fragment_tab0, null, false);
tabFragment.MyAdapter adapter = new tabFragment.MyAdapter(getActivity().getSupportFragmentManager());
adapter.addFragment(new tab0Fragment(), getResources().getString(R.string.tab0));
container.addView(view);
return view;
}
(so for 5 page, I have 5 fragment.
DEMO
But My application run slow (laggy) when I swipe pages.(with tap buttom is normal)
So I tried write an Adapter class directly for each page like this:
if (position == 0) {
final View view = LayoutInflater.from(getBaseContext()).inflate(R.layout.item_vp_list, null, false));
final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.rv);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getBaseContext(), LinearLayoutManager.VERTICAL, false));
recyclerView.setAdapter(new Tab0RecycleAdapter());
container.addView(view);
return view;
}
with top code,my application run fast again with swap pages!
Is it important to create fragment per each page?
why I must use fragment?(because some programmer recommended it in viewpager)
my method (second method without fragment) is true or false for a real application?
(I am noob and this is my first app)
Now I want fill each page with some json RecyclerView data list.(get json from network).
If you perform this network task on the UI thread, it will block and could cause laggy performance. This could be the reason your pages load slowly. You should perform network tasks on a separate thread.
So I tried write an Adapter class directly for each page like this
You only need one adapter per recycler view. If you want to support multiple views within the same adapter, override getItemViewType(). Example here: https://stackoverflow.com/a/26245463/7395923
Is it important to create fragment per each page?
Why I must use fragment? (because some programmer recommended it in view pager)
It is possible to use a view pager without fragments. Base on your previous question (linked at the top), it does seem overkill to load an entire fragment just to inflate a view. Here is a link to an example of a view pager without fragments: https://stackoverflow.com/a/18710626/7395923
I hope this helps.

Viewpager no longer swipeable after onClick is set on inflated View

I have a viewpager, which contain an image. If I create the image view using an xml layout as below
#Override
public Object instantiateItem(final ViewGroup container, final int position) {
final int realPosition = getItemPosition(position);
View view = LayoutInflater.from(mContext).inflate(R.layout.item_adsong, container, false);
ImageView imageView = (ImageView)view.findViewById(R.id.adsong_img);
/... set to the image.../
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onClickPage(realPosition);
}
});
container.addView(view);
return view;
}
After adding the onClickListener, I can no longer swipe my view pager from page to page. Without the onClickListener, I could do so.
However, if I create my ImageView from the code (instead of using xml layout) as below
#Override
public Object instantiateItem(final ViewGroup container, final int position) {
final int realPosition = getItemPosition(position);
ImageView view = new ImageView(mContext);
/... set to the image.../
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onClickPage(realPosition);
}
});
container.addView(view);
return view;
}
I could now swipe the view pager, and I could do the click as I like. Why my first example above is intercepting the swipe, while the second is not? How could I fix my first example above to allow both swipe and onClick still work (I prefers using the layout, as I have several more e.g. some TextViews than just ImageView to show in my view pager). Thanks!
After further thorough debug and investigation, found the issue as per https://groups.google.com/forum/#!topic/android-developers/wIqNP4fzJQc. The cause of the issue is because the existence of TextView in my .xml layout. The TextView is scrollable by default.
Given the TextView is scrollable, the ViewPager then would allow the text to scroll by not intercepting the scrolling touch, and passing to the child. Hence the scrolling touch of the ViewPager is not working.
To fix the problem I override the canScrollHorizontal function of the TextView to return false. This solve the problem
public class MyTextView extends TextView {
//...
#Override
public boolean canScrollHorizontally(int direction) {
return false;
}
//....
Note: using setHorizontallyScrolling(false) or setting the android:scrollHorizontally="false" attribute in the XML of the TextView doesn't solve the problem somehow.
Hope this helps some who face the ViewPager no longer scrollable issue.

dynamic layouts in navigation drawer ruins the view

I'm facing some trouble with the navigation drawer adapter.
It's supposed to display items as follows: Favorito, Categorias, and small sub categories underneath categorias.
I programmed the navigation drawer adapter to use a big_layout.xml file by default, but if its position is greater than a certain value, then it uses a small_layout.xml file.
It works fine for the first few items, but the problem is when I scroll down to see the rest of the items, they use the big_layout.xml, and then when I scroll back up, the original big items change their view and use the small layout!
below is the code, and this is a screen shot of the bad results: http://i.stack.imgur.com/QWwts.jpg
#Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
LayoutInflater laoutInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (getItemId(position)>3)
view = laoutInflater.inflate(R.layout.drawer_list_item_small, null);
if (getItemId(position)<=3)
view = laoutInflater.inflate(R.layout.drawer_list_item, null);
}
ImageView icon = (ImageView) view.findViewById(R.id.icon);
icon.setImageResource(drawerItems.get(position).getIcon());
TextView title = (TextView) view.findViewById(R.id.title);
title.setText(drawerItems.get(position).getTitle());
return view;}
Is there anything wrong I'm doing ? , Is there something missing that might be responsible of making the view stable?
How can i fix this ?
Your issue is with recycling. When you scroll down and back up, the views using the small layout are no longer needed, and so are eligible for recycling - now, the view is not null, so the layout will not be reinitialised based on its position, but merely updated with the new content.
You can fix this by using ViewTypes in your list adapter class, overriding the following methods.
#Override
public int getItemViewType(int position) {
return (position > 3) ? 0 : 1;
}
#Override
public int getViewTypeCount() {
return 2;
}
Then, in your getView() you will not be given a view (for recycling) if it is of the wrong view type.
#Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
int layout = getLayoutForViewType(position);
view = LayoutInflater.from(parent.getContext()).inflate(layout, null);
}
...
return view;
}
private int getLayoutForViewType(int position) {
if (getItemViewType(position) == 0) {
return R.layout.blahblahblah;
}
return R.layout.bloobloobloo;
}

How to change the image resource of a item clicked in a list view

I have a custom listview which has an ImageView on the left, followed by a EditText and on the right I have another ImageView which, at first, has not any source.
Then I have an onItemClickListener on this View. I would like to change the source of the ImageView on the right (the one that initially hasn't any source) on the item click.
For this I have implemented this code:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ImageView icone2 = (ImageView) view.findViewById(R.id.icone2_lugar);
icone2.setImageResource(R.drawable.flag_origem);
}
The ImageView is succefully inserted on the item clicked. However, when I scroll down the list, I realized that all the other elements which were at the same position of the one clicked before I scroll the list, also changed the image resource.
Why is it happening? I guess Android loads new views as scroll the list instead of loading all elements views at the same moment, so I guess I can't change the resource of a element in a list by taking the View parameter of onItemClick.
So how could I change the resource of the clicked element in a list view?
EDIT:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View linha = convertView;
ArmazenadorLugar armazenador = null;
if (linha == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
linha = inflater.inflate(R.layout.linha_lugar, parent, false);
armazenador = new ArmazenadorLugar(linha);
linha.setTag(armazenador);
} else {
armazenador = (ArmazenadorLugar) linha.getTag();
}
armazenador.popularFormulario(mListaLugares.get(position), mContext);
return linha;
}
public LugarAndroid getItem(int position) {
return mListaLugares.get(position);
}
I ended up doing it in a different way. Don't know if it is the better but it works.
I created a variable (imgPosition) in my adapter which will hold the element position which has to set the image resource. Then, whenever I click onto an element, the listener calls
adapter.setImgPosition(position);
adapter.getView(position, view, parent);
I called the getView method in the listener to refresh the list, because getView calls a method (I'll explain it next) to update the list.
In the getView method of the adapter class I have the following logic (the position element comes from getView parameter):
holder.setValues(mListaLugares.get(position), mContext, imgPosition == position);
In the holder:
static class Holder{
private TextView txt1= null;
private ImageView img1 = null;
private ImageView img2 = null;
Holder(View linha) {
txt1= (TextView) linha.findViewById(R.id.txt1);
img1 = (ImageView) linha.findViewById(R.id.img1 );
img2 = (ImageView) linha.findViewById(R.id.img2);
}
void setValues(LugarAndroid lugar, Context mContext, boolean setImg) {
img2.setImageResource(0);
if(setImg) img2.setImageResource(R.drawable.flag);
// Logic to fill up the others views
}
This way I will only set the image resource of the element clicked when the boolean passed is true
If you are using an adapter with a List of Object, you can use :
getItem(position) and then change the image of the imageview of this specific object

View pager does not inflate the good page

I'm trying to use a view pager to make a gallery. Nevertheless it's always the view at the position 1 which is updated whereas i'm trying to update the current page (0 by default).
First of all i have created a view which gonna be inflated by the pager. This view is composed of 4 ImageView and 4 textview.
Then i created a adapter for the pager :
public class LivePagerAdapter extends PagerAdapter {
private CameraList cameras = null;
public LivePagerAdapter(CameraList cams)
{
this.cameras = cams;
}
#Override
public int getCount() {
return this.cameras.size() /4; // 4 imageview in a page
}
public void setCameras(CameraList cams)
{
this.cameras = cams;
}
#Override
public Object instantiateItem(View container, int position) {
LayoutInflater inflater = LayoutInflater.from(container.getContext());
View view = inflater.inflate(R.layout.pager,null);
((ViewPager) container).addView(view,0);
return view;
}
}
Finally in my activity i'm instanciate the pager like this :
adapter = new LivePagerAdapter(this.listeCamera);
pager = (ViewPager) findViewById(R.id.camspager);
pager.setAdapter(adapter);
pager.setCurrentItem(0);
After that i'm initialising a array with my 4 imageview :
tabImageView.add((ImageView) findViewById(R.id.cam1)); //Imageview located in the view loaded by the pager
tabImageView.add((ImageView) findViewById(R.id.cam2));
tabImageView.add((ImageView) findViewById(R.id.cam3));
tabImageView.add((ImageView) findViewById(R.id.cam4));
And when needed i add a image in the imageview.
It seems to works except that i'm asking the pager to start at position 0 and this is the view from position 1 which is updated with my images.
When i use findViewById(somethingInflatedByThePager) i need it to be the current page , how can i do that ?
What happens if you replace ((ViewPager) container).addView(view,0); by ((ViewPager) container).addView(view);

Categories

Resources