GridView动态添加View

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".activity.MainActivity">
    <GridView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/gridView"
        android:numColumns="4"></GridView>
</LinearLayout>

Article.java

package com.pingyijinren.helloworld.model;

/**
 * Created by Administrator on 2016/6/3 0003.
 */
public class Article {
    private int imageId;
    private String name;

    public Article(int imageId,String name){
        this.imageId=imageId;
        this.name=name;
    }

    public int getImageId() {
        return imageId;
    }

    public String getName() {
        return name;
    }
}

grid_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/image"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:id="@+id/name"
        android:gravity="center"/>
</LinearLayout>

ArticleAdapter.java

package com.pingyijinren.helloworld.adapter;

import android.content.Context;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.pingyijinren.helloworld.R;
import com.pingyijinren.helloworld.model.Article;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/3 0003.
 */
public class ArticleAdapter extends ArrayAdapter<Article>{
    private ArrayList<Article> arrayListArticle;
    public ArticleAdapter(Context context, int resource, List<Article> objects) {
        super(context, resource, objects);
        arrayListArticle=(ArrayList<Article>) objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null){
            convertView= LayoutInflater.from(getContext()).inflate(R.layout.grid_item,null);
            viewHolder=new ViewHolder();
            viewHolder.image=(ImageView) convertView.findViewById(R.id.image);
            viewHolder.name=(TextView)convertView.findViewById(R.id.name);
            convertView.setTag(viewHolder);
        }
        else{
            viewHolder=(ViewHolder) convertView.getTag();
        }

        if(position<arrayListArticle.size()){
            Article article=getItem(position);
            viewHolder.image.setImageResource(article.getImageId());
            viewHolder.name.setText(article.getName());
        }
        else{
            viewHolder.image.setImageResource(R.drawable.plus);
            viewHolder.name.setText("");
        }

        return convertView;
    }

    private class ViewHolder{
        public ImageView image;
        public TextView name;
    }

    @Override
    public int getCount() {
        return arrayListArticle.size()+1;
    }
}
package com.pingyijinren.helloworld.adapter;

import android.content.Context;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.pingyijinren.helloworld.R;
import com.pingyijinren.helloworld.model.Article;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/3 0003.
 */
public class ArticleAdapter extends ArrayAdapter<Article>{
    private ArrayList<Article> arrayListArticle;
    public ArticleAdapter(Context context, int resource, List<Article> objects) {
        super(context, resource, objects);
        arrayListArticle=(ArrayList<Article>) objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null){
            convertView= LayoutInflater.from(getContext()).inflate(R.layout.grid_item,null);
            viewHolder=new ViewHolder();
            viewHolder.image=(ImageView) convertView.findViewById(R.id.image);
            viewHolder.name=(TextView)convertView.findViewById(R.id.name);
            convertView.setTag(viewHolder);
        }
        else{
            viewHolder=(ViewHolder) convertView.getTag();
        }

        if(position<arrayListArticle.size()){
            Article article=getItem(position);
            viewHolder.image.setImageResource(article.getImageId());
            viewHolder.name.setText(article.getName());
        }
        else{
            viewHolder.image.setImageResource(R.drawable.plus);
            viewHolder.name.setText("");
        }

        return convertView;
    }

    private class ViewHolder{
        public ImageView image;
        public TextView name;
    }

    @Override
    public int getCount() {
        return arrayListArticle.size()+1;
    }
}

MainActivity.java

package com.pingyijinren.helloworld.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.pingyijinren.helloworld.R;
import com.pingyijinren.helloworld.adapter.ArticleAdapter;
import com.pingyijinren.helloworld.model.Article;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private GridView gridView;
    private ArrayList<Article> arrayListArticle;
    private ArticleAdapter articleAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gridView=(GridView)findViewById(R.id.gridView);
        arrayListArticle=new ArrayList<Article>();
        arrayListArticle.add(new Article(R.drawable.first_image,"frist"));
        arrayListArticle.add(new Article(R.drawable.second_image,"second"));
        articleAdapter=new ArticleAdapter(MainActivity.this,R.layout.grid_item,arrayListArticle);
        gridView.setAdapter(articleAdapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position==arrayListArticle.size()){
                    arrayListArticle.add(new Article(R.drawable.third_image,"third"));
                    articleAdapter.notifyDataSetChanged();
                }
            }
        });
    }
}

 

posted @ 2016-06-03 12:40  zqxLonely  阅读(711)  评论(0编辑  收藏  举报