首页 > 代码库 > ListVIew中插入view

ListVIew中插入view

public class MainActivity extends Activity {    private ListView listview;    private List<String> dataList = new ArrayList<String>();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);         listview = (ListView) findViewById(R.id.listview);         initData();         MyAdapter myAdapter = new MyAdapter();         listview.setAdapter(myAdapter);         //为listview设置点击监听         listview.setOnItemClickListener(new OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view,                    int position, long id) {                //标题                if(position == 1|| position == 7){                    return;                }                //数据                Toast.makeText(MainActivity.this, "点击了第"+position+"条", 0).show();            }        });            }     private void initData() {         for (int i = 0; i < 10; i++) {            dataList.add("第"+i+"条数据");        }            }    class MyAdapter extends BaseAdapter{        @Override        public int getCount() {            return dataList.size();        }        @Override        public Object getItem(int position) {            return dataList.get(position);        }        @Override        public long getItemId(int position) {            return position;        }        /*         * 总共显示10条数据   第2条和第8条显示文本  其他显示图片加文本(position)         */        @Override        public View getView(int position, View convertView, ViewGroup parent) {
//注意:1.加载的是布局文件,返回的也是布局文件,
//   2.只有textview特殊能直接返回,imageView需要从布局中加载文件
//屏幕滑出去会自动复用 if(position == 1 || position == 7){ TextView textView = new TextView(MainActivity.this); textView.setText(""+position); textView.setTextSize(30); textView.setTextColor(Color.BLUE); return textView; } ViewHolder viewHolder = null ;
//如果复用的view不为空,并且能转化为子类布局文件,获取viewHolder对象,否则加载布局文件,重新进行设置复用的view
if(convertView != null&&convertView instanceof LinearLayout){                viewHolder = (ViewHolder) convertView.getTag();                viewHolder.tv_text.setText("+"+dataList.get(position));            }else{                //加载布局                viewHolder= new ViewHolder();                convertView = View.inflate(MainActivity.this, R.layout.listview_item01, null);                viewHolder.iv_image = (ImageView) convertView.findViewById(R.id.iv_image01);                viewHolder.tv_text = (TextView) convertView.findViewById(R.id.tv_text01);                viewHolder.tv_text.setText("+"+dataList.get(position));                convertView.setTag(viewHolder);            }                        return convertView;        }        class ViewHolder{            private TextView tv_text;            private ImageView iv_image;        }              }}

Instanceof对象与类比较运算符

语法: 对象 instanceof 类    向下转型(大转小)父转子,必须强行转

    返回类型是boolean类型,非true即false

表示判断某个对象是否属于某个类

 

注意:这个比较运算符不能随便使用,必须用在类与类存在继承关系的时候使用

效果图:

技术分享

技术分享

 

ListVIew中插入view