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();
                }
            }
        });
    }
}
时间: 2024-10-10 04:07:24

GridView动态添加View的相关文章

Android在布局中动态添加view的两种方法

一.说明 添加视图文件的时候有两种方式:1.通过在xml文件定义layout:2.java代码编写 二.前言说明 1.构造xml文件 2.LayoutInflater 提到addview,首先要了解一下LayoutInflater类.这个类最主要的功能就是实现将xml表述的layout转化为View的功能.为了便于理解,我们可以将它与findViewById()作一比较,二者都是实例化某一对象,不同的是findViewById()是找xml布局文件下的具体widget控件实例化,而LayoutI

微信小程序之一:动态添加view(view包含picker,input)

<view wx:for="{{array}}" wx:key="this" class="borderContainer"> <view class="borderContainer1"> <view class="firstBorder"> <view class="firstBorderInput"> <input maxlen

为节省内存,动态添加view布局和控件

1如果一个自定义view要在短时间被多次调用,会造成多次读取xml和findViewById,所以动态添加控件.属性 RelativeLayout: private void initView() { Button btn1 = new Button(this.getContext()); btn1.setId(1); RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.Layo

android动态添加view

动态添加一个view. 首先,搞清楚是哪个view(子view),要添加到哪个view(父view)中:其次,子view的属性怎么设置:这两个问题明白了,就容易了. 举个例子:比如要在一个LinearLayout中添加一个Button,  子view是Button,父view是LinearLayout. 子view的属性就是通过LayoutParams来设置的,注意是LinearLayout.LayoutParams,因为子view的高度,宽度这些都是针对父view的,要告诉父view自己要占用

GridView动态添加新行

gridview动态添加行的原理就是用datatable增加新列然后重新绑定: 设计源码 设计gridview代码: <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:TextBox ID="TextBox2&qu

Android中动态添加View的两种方法

一.使用xml的方式: 1.LayoutInflater: 这个类可以把xml表述的Layout解析为View,从而可以使addView()方法添加View. 2.LayoutInflater与findViewById的区别: 两者都是实例化某一个对象,不同的是findViewById是通过找xml布局文件下的一个具体的widget控件进行实例化,而LayoutInflater是找res/layout 下的xml布局文件来实例化的. 3.使用方法: LayoutInflater inflater

GridView动态添加列并判断绑定数据DataTable的列类型控制展示内容

此篇随笔是2013年根据项目需求开发记录的,不一定符合大众口味,只需了解开发思路,毕竟解决方案多种多样. 下面简单说说需求点吧: (1)通过下拉列表可以选择一个DataSet(数据集),一个DataSet存在可以互相关联的多个DataTable(数据表格),DataTable数据来源于数据库视图:SQL语句关联比较复杂 (2)一个DataTable(数据表格)存在多个可供选择查询显示的Column(列),支持动态组合 (3)Column(列)同时支持作为查询条件进行并运算 先看效果吧,免得待会看

Android 利用addView 动态给Activity添加View组件

本文主要讲述如何动态给UI界面添加布局和控件,在编程的时候很多时候需要动态显示一些内容,在动态添加View的时候,主要使用addView方法. 1. addView方法简介 在Android 中,可以利用排版View的 addView 函数,将动态产生的View 物件加入到排版View 中. 例子如下: 界面代码: <LinearLayout android:id="@+id/viewObj" android:layout_width="wrap_content&quo

动态添加布局时,addView易引发的错误

今天在做课程设计的时候,因为Adapter里面动态添加View的时候,写了下面的代码 TextView start = new TextView(context,null); holder.start = start; LinearLayout.LayoutParams startl = new LinearLayout.LayoutParams(80,40); start.setLayoutParams(startl); start.setText("("+bean.getStart