Android Gridview - changing element position Randomly when scrolling

I have a GridView that has 10 rows and 7 columns. Elements in a GridView not in a unique form, which is a single row containing 7 elements, one row has only 4 or 5 elements, etc. Now I have created a GridView with full elements that comprise 70 elements. Now I want to hide some elements in the GridView . I tried setting the visibility of ImageView and TextView to View.INVISIBLE . It works, but when we scroll through the GridView , it randomly changes.

What should I do? I am new to Android.

code:

 @Override public int getCount() { // TODO Auto-generated method stub return listFlag.size(); } @Override public String getItem(int position) { // TODO Auto-generated method stub return listCountry.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } public static class ViewHolder { public ImageView imgViewFlag; public TextView txtViewTitle; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder view; LayoutInflater inflator = activity.getLayoutInflater(); int childSize = parent.getChildCount(); if(convertView==null) { view = new ViewHolder(); convertView = inflator.inflate(R.layout.gridview_row, null); view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1); view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1); convertView.setTag(view); } else { view = (ViewHolder) convertView.getTag(); } if(position==64||position==57||position==58||position==59||position==50||position==23||position==16||position==17||position==9||position==10||position==12||position==1||position==2||position==8) { view.txtViewTitle.setVisibility(View.GONE); view.imgViewFlag.setVisibility(View.GONE); } view.txtViewTitle.setText(listCountry.get(position)); view.imgViewFlag.setImageResource(listFlag.get(position)); return convertView; } } 
+4
source share
1 answer

The problem is with setVisibility calls in code.

 if(position==64||position==57||position==58||position==59||position==50||position==23||position==16||position==17||position==9||position==10||position==12||position==1||position==2||position==8) { view.txtViewTitle.setVisibility(View.GONE); view.imgViewFlag.setVisibility(View.GONE); } 

You install them in GONE when you do not need them, but you do not install them in VISIBLE when you need them. Once you set the value to GONE, the same cells will be reused when scrolling. At this point, if you do not set them to VISIBLE, they will never appear.

 if(position==64||position==57||position==58||position==59||position==50||position==23||position==16||position==17||position==9||position==10||position==12||position==1||position==2||position==8) { view.txtViewTitle.setVisibility(View.GONE); view.imgViewFlag.setVisibility(View.GONE); } else { view.txtViewTitle.setVisibility(View.VISIBLE); view.imgViewFlag.setVisibility(View.VISIBLE); } 

To address the second part of the title, you can add a TextView for the title to each cell, but just set its visibility to VISIBLE if this is the first element in the row. This way you do not have to do too much custom code.

Hope this helps.

+3
source

Source: https://habr.com/ru/post/1479687/


All Articles