【问题标题】:My Grid View Items poition change in scrolling down and up我的网格视图项目位置在向下和向上滚动时发生变化
【发布时间】:2016-02-10 17:26:04
【问题描述】:

我在活动开始时的GridView 看起来正确

但在向下和向上滚动后项目位置发生变化

我第一行的最后一项有一个大文本,我认为这是我的问题 这是我的getView() 代码

public View getView(int position, View convertView, ViewGroup parent) {
    View v;
    if (convertView == null) {
        LayoutInflater li = getLayoutInflater();
        v= li.inflate(R.layout.item, null);
    } else {
    v= convertView;
    }
    ImageView imageView = (ImageView) v.findViewById(R.id.grid_item_image);
    imageView.setImageResource(R.drawable.logo);
    TextView textView = (TextView) v.findViewById(R.id.grid_item_label);
    textView.setText(PersianReshape.reshape( Values[position].getName()));
    Typeface face = Typeface.createFromAsset(context.getAssets(),"font/BNazanin.ttf");
    textView.setTypeface(face);
    textView.setTextSize(farin.code.rahnamee.attrib.attribute.content_font_size);
    return gridView;
}

【问题讨论】:

    标签: android android-layout android-gridview


    【解决方案1】:

    您的网格项目都应具有统一的高度。考虑设置最小/最大文本行数以确保高度一致,并确保ImageView 也具有一致的大小。一种方法是为ImageView 使用已知的固定大小,而不是简单的wrap_content

    【讨论】:

    • 回复坦克我认为我的问题在我的大文本框中,当我向上滚动从底部对齐的其他页面中的所有第一个项目行时,我有统一的项目我没有这个问题我应该削减我的文本视图?没有统一项目就没有办法解决这个问题吗?
    【解决方案2】:

    你应该像这样改变你的 getView 方法。

    public View getView(int position, View convertView, ViewGroup parent){
        // TODO Auto-generated method stub
        View v;
        if(convertView==null)
        {
            LayoutInflater li = getLayoutInflater();
            v = li.inflate(R.layout.icontext, null);
        }else{
            v = convertView;
        }
        TextView tv = (TextView)v.findViewById(R.id.icon_text);
        tv.setText(providers[position]);
        ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
        iv.setImageResource(R.drawable.icon);
    
        return v;
    }
    

    您的问题是,当您使用 convertView 时,它会从第一条记录中存储旧数据。转换视图用于避免资源的布局膨胀,这会花费您的时间和内存。您应该使用旧的膨胀视图,但设置新数据。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多