Android GridView – randomly change element position while scrolling

I have a GridView with 10 rows and 7 columns. The elements in the GridView are not a unique form. A row contains 7 elements, a row has only 4 or 5 elements, etc. now I have created a GridView with 70 complete elements. Now I want to hide some elements in the GridView. I have tried to set the visibility of ImageView and textview to view.invisible. It works, But when we scroll the GridView, it changes randomly

>Another problem is that each line contains a title that does not belong to each element

What should I do? I'm 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;
}

}

resolvent:

The problem is the setvisibility call in the 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);
}

Set them to go when you don't need them, but don't set them to visible when you need them. Once set to go, the same cells will be used repeatedly when scrolling. At this time, if you don't 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 solve the second part about the title, you can add a textview of the title for each cell, but if it is the first item in the row, only set its visibility to visible. In this way, you don't have to make too much custom code

I hope this will help

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>