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();
}
}
});
}
}
GridView动态添加View的更多相关文章
- 动态添加布局、动态添加View、LinearLayout动态添加View;
LinearLayout提供了几个方法,用作动态添加View特别好用: 可以添加View.删除View.删除指定位置View.删除全部View: 看代码: public class MainActiv ...
- 微信小程序之一:动态添加view(view包含picker,input)
<view wx:for="{{array}}" wx:key="this" class="borderContainer"> ...
- WPF GridView动态添加项并读取数据
假设数据库有如下表, 首先我们创建一个WPF工程,界面如下 <Window x:Class="WpfApplication2.MainWindow" xmlns=" ...
- asp.net gridview动态添加列,并获取其数据;
1,绑定数据前先动态添加列,见方法CreateGridColumn(只在第一次加载动态添加): 2,gvlist_RowDataBound为对应列添加控件: 前台代码: <%@ Page Lan ...
- GridView动态添加列之后,导致PostBack(回发)页面数据丢失问题解决
直入主题,首先声明,这个问题是无法解决的,特此在这说明 一.如何动态添加列,如下: 在页面重写OnInit事件,至于为什么要在这个事件写,根据页面的声明周期和经验可知(不用去别的地方找了,这个我找了之 ...
- Android 在程序中动态添加 View 布局或控件
有时我们需要在程序中动态添加布局或控件等,下面用程序来展示一下相应的方法: 1.addView 添加View到布局容器 2.removeView 在布局容器中删掉已有的View 3.LayoutPar ...
- GridView 动态添加绑定列和模板列
动态添加绑定列很简单:例如: GridView1.DataSourceID = "SqlDataSource1"; BoundField bf1 = new BoundField( ...
- Mono for android 如何动态添加View,线程内部如何更新UI.
貌似所有设计到UI的程序原理都是一样的,子线程是不能够更新UI状态的,所以就必须使用UI自身或者第三方来更新UI. 如 在WinForm 中 就可以使用Control.Invoke(Action ac ...
- 往Layout中动态添加View
需要注意几个方法:基本上所有的方法参数单位是px 1.设置View的宽高: LinearLayout.LayoutParams params = new LinearLayout().LayoutPa ...
随机推荐
- softmax_loss的归一化问题
cnn网络中,网络更新一次参数是根据loss反向传播来,这个loss是一个batch_size的图像前向传播得到的loss和除以batch_size大小得到的平均loss. softmax_loss前 ...
- gson对象的相互转换
参见 http://www.javacreed.com/gson-deserialiser-example/
- EMVS: Event-based Multi-View Stereo 阅读笔记
0. 摘要 EMVS目的:从已知轨迹的event相机,估计半稠密的3D结构 传统的MVS算法目的:从已知视点的图片集,去估计场景的稠密3D结构. EMVS2个固有属性: (1) 当传感器发生相对运 ...
- java中regex参考
在Sun的Java JDK 1.40版本中,Java自带了支持正则表达式的包,本文就抛砖引玉地介绍了如何使用java.util.regex包. 可粗略估计一下,除了偶尔用Linux的外,其他Linu ...
- awk日志分割
awk日志分割 1. awk实现日志按照日期分割 #!/bin/bash DATE=$(date -d yesterday +%Y-%m-%d) awk 'BEGIN{RS="'$DATE ...
- 四. python网络编程
第八章.网络基础知识 1. TCP/IP协议介绍 1.TCP/IP概念 TCP/IP: Transmission Control Protocol/Internet Protocol的简写,中译名为传 ...
- ios之UITextView
我们计划创建UITextView,实现UITextViewDelegate协议方法,使用NSLog检查该方法何时被调用.我们还会接触到如何在TextView中限制字符的数量,以及如何使用return键 ...
- sqlite查看所有表名、判断表是否存在,字段名及字段信息
sqlite查看所有表名.判断表是否存在,字段名及字段信息 sqlite查看所有表名及字段名查询table,type 段是'table',name段是table的名字, select name f ...
- Ruby设计模式-观察者模式学习笔记
observer.rb #!/bin/env ruby # encoding: utf-8 require 'observer' class CriminalMovement include Obse ...
- javascript基础知识 (八) BOM学习笔记
一.什么是BOM BOM(Browser Object Model)即浏览器对象模型. BOM提供了独立于内容 而与浏览器窗口进行交互的对象: 由于BOM主要用于管理窗口 ...