网格视图 - 第一个和最后一个位置的第一个项目

时间:2014-03-07 17:15:34

标签: android gridview android-adapter

我有一个使用gridview的片段:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    context = container.getContext();

    View window = inflater
            .inflate(R.layout.shop_fragment, container, false);

    gridView = (GridView) window.findViewById(R.id.shop_root_view);
    gridView.setAdapter(new ShopAdapter(context, GRID_DATA));

    gridView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            Toast.makeText(
                    context,
                    ((TextView) v.findViewById(R.id.grid_item_label))
                            .getText(), Toast.LENGTH_SHORT).show();
        }
    });
    return window;
}

适配器:

public class ShopAdapter extends BaseAdapter {

    private Context context;
    private final String[] gridValues;

    // Constructor to initialize values
    public ShopAdapter(Context context, String[] gridValues) {

        this.context = context;
        this.gridValues = gridValues;
    }

    @Override
    public int getCount() {
        return gridValues.length;
    }

    @Override
    public Object getItem(int position) {

        return null;
    }

    @Override
    public long getItemId(int position) {

        return 0;
    }

    // Number of times getView method call depends upon gridValues.length
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // LayoutInflator to call external grid_item.xml file

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;

        if (convertView == null) {

            gridView = new View(context);

            // get layout from grid_item.xml ( Defined Below )

            gridView = inflater.inflate(R.layout.item_grid_shop, null);

            // set value into textview

            TextView textView = (TextView) gridView
                    .findViewById(R.id.grid_item_label);

            Log.d("Shop", "pozycja: " + position);
            textView.setText(gridValues[position]);

            // set image based on selected text

            ImageView imageView = (ImageView) gridView
                    .findViewById(R.id.grid_item_image);

            imageView.setImageResource(R.drawable.ic_launcher);

        } else {

            gridView = (View) convertView;
        }

        return gridView;
    }
}

每次打开该片段时,GRID_DATA中的项目都会出现在第一个和最后一个位置。 GRID_VIEW的最后一项不会出现。怎么解决这个?这只是我在互联网上找到的示例代码。

1 个答案:

答案 0 :(得分:3)

此处convertView使用错误。

你应该以不同的方式使用它: - 如果为null,则膨胀视图 - 如果没有,什么都不做(并将使用它) 然后关闭该括号并设置数据。

试试这个:

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.item_grid_shop, null);
    }
    TextView textView = (TextView) convertView.findViewById(R.id.grid_item_label);
    Log.d("Shop", "pozycja: " + position);
    textView.setText(gridValues[position]);
    ImageView imageView = (ImageView) convertView.findViewById(R.id.grid_item_image);
    imageView.setImageResource(R.drawable.ic_launcher);
    return convertView;
}