0
点赞
收藏
分享

微信扫一扫

一行代码搞定默认显示GridView第一个条目

分湖芝蘭 2022-06-01 阅读 44

核心代码

@Override
    public View getView(final int position, final View convertView, final ViewGroup parent) {
        v = super.getView(position, convertView, parent);
        if(position==0){
            v.setBackgroundResource(R.color.orange);
        }
        return v;
    }

全代码

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;

import com.wintec.huashang.R;

import java.util.List;
import java.util.Map;

public class MyGridAdapter  extends SimpleAdapter {

    private View v;
    /**
     * Constructor
     *
     * @param context  The context where the View associated with this SimpleAdapter is running
     * @param data     A List of Maps. Each entry in the List corresponds to one row in the list. The
     *                 Maps contain the data for each row, and should include all the entries specified in
     *                 "from"
     * @param resource Resource identifier of a view layout that defines the views for this list
     *                 item. The layout file should include at least those named views defined in "to"
     * @param from     A list of column names that will be added to the Map associated with each
     *                 item.
     * @param to       The views that should display column in the "from" parameter. These should all be
     *                 TextViews. The first N views in this list are given the values of the first N columns
     */
    public MyGridAdapter(final Context context, final List<? extends Map<String, ?>> data, final int resource, final String[] from, final int[] to) {
        super(context, data, resource, from, to);
    }


    @Override
    public View getView(final int position, final View convertView, final ViewGroup parent) {
        v = super.getView(position, convertView, parent);
        if(position==0){
            v.setBackgroundResource(R.color.orange);
        }
        return v;
    }

    //    public View getView(int position, View convertView, ViewGroup parent) {
//        v = super.getView(position, convertView, parent);
//        if (position == selectedPosition) {
//            v.setBackgroundResource(R.color.orange);
//        }
//        else{
            v.setBackgroundResource(0);
//        }
        System.out.println("_---" + x);
        System.out.println("_+++" + position);
        if (x >= 2 && x <= 6) {
            av.add(v);
        }
        System.out.println("!!!" + av.size());
//        return v;
//    }
}


举报

相关推荐

0 条评论