开始使用gridView

时间:2016-10-04 15:15:33

标签: java android gridview

目前还有GridViewActivity,工具栏发布了" +"按钮(添加元素gridView列表)。

public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.addPerson) {
    addPersonToAdapter();
}
return super.onOptionsItemSelected(item);
}

问题是,图像可能是" +" (或任何xml文件)放在我的列表gridView的顶部,通过点击这个项目添加我的元素,使图片是" +"总是向右移动?

现在我在代码中添加一些更改

public int getCount() {
    return persons.size() + 1;
}

并希望在我的方法getView

中添加类似的内容
public View getView(final int position, View convertView, ViewGroup parent) {
if(position == getCount - 1) {
return new "my image or what??"
} else {
    return View;
}

完整代码getView:

public View getView(final int position, View convertView, ViewGroup parent) {
    person = persons.get(position);
    final ViewHolder viewHolder;
    if (convertView == null) {
        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(activity);
        convertView = inflater.inflate(R.layout.list_item, parent, false);
        viewHolder.name = (TextView) convertView.findViewById(R.id.firstLastNameTextView);
        viewHolder.photo = (ImageView) convertView.findViewById(R.id.imageViewPhoto);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    viewHolder.name.setText(person.name);
    viewHolder.photo.setImageResource(setImage());

    Button buttonDelete = (Button) convertView.findViewById(R.id.deleteButton);
    buttonDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            persons.remove(position);
            notifyDataSetChanged();
        }
    });

    return convertView;
}

1 个答案:

答案 0 :(得分:-1)

定义一个枚举器并将其作为参数传递给方法..

public enum EVideo{
    YOUTUBE,
    VIMEO

}

private void useLinkInput(String input,EVideo video ) {
    Uri uri = Uri.parse(input);
    String videoID = uri.getQueryParameter("v");
    if(TextUtils.isEmpty(videoID)) {
        String[] str = input.split("/");
        videoID = str[str.length-1];
    }
    if(video == EVideo.YOUTUBE){
        source= "http://img.youtube.com/vi/";
    }else{
        source= "http://VIMEO.CN";
    }
    url = source + videoID +"/0.jpg";
    Log.d("url",url);

}