接着昨天的内容,如果想要定义更复杂的ListView怎么办,如果需要在列表中加入图片选择框以及按钮呢?我们改如何定义?修改布局文件已经不够用了,现在我们需要自定义适配器
首先新建一个·Activity
public class MainActivity extends ListActivity { String[] strs = {"aa","xx","ss","dd","ff","gg","hh","jj"}; private ListView listview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }
我们在activity_main布局中放一个listview就可以了
<ListView 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:id="@+id/list_view" tools:context=".MainActivity" > </ListView>
现在获取布局在activity显示出来
listview.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, strs)); listview.setOnItemClickListener(listener); listview.setAdapter(new MyAdapter());
如果想添加按钮,像昨天一样,我们依然可以使用昨天的方法设置权重
<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" tools:context=".MainActivity" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/lianxi_ren" android:layout_weight="0"/> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/list_view" android:layout_weight="1"></ListView> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button" android:text="button" android:layout_weight="0"/> </LinearLayout>
接下来我们开始自定义布局,丰富我们listview每一行的元素,现在我们数组适配器之能是一行textview
我们现在使用自定义适配器来改变我们的listview
首先我们把刚才的数组适配器注释掉 ,获取当前控件id
listview= (ListView) findViewById(R.id.list_view); // listview.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, strs)); // listview.setOnItemClickListener(listener); listview.setAdapter(new MyAdapter());
下面构建一个新的类MyAdpater继承BaseAdapter
public class MyAdapter extends BaseAdapter{ @Override public int getCount() { // TODO Auto-generated method stub return 0; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { return 0; } }
我们在这里定义显示的ListView行数
public int getCount() { // TODO Auto-generated method stub return 5; }
先显示5行,通过getview来显示每一行的内容
@Override public View getView(int position, View convertView, ViewGroup parent) { TODO Auto-generated method stub ImageView image = new ImageView (MainActivity.this); image .setImageResource(R.drawable.ic_launcher); System.out.println("getView"+position); return image; }
position行号
convertView当前行view
ViewGroup整体的listview
接下来我们设置更复杂的布局
先修改布局文件
<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" tools:context=".MainActivity" android:orientation="vertical"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="mjl" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/image" android:src="@drawable/ic_launcher" /> <CheckBox android:id="@+id/check" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
在getView中获取显示出来
View view =View.inflate(MainActivity.this, R.layout.activity_main, null); return view;
下面展示出来所有代码
package com.example.listview; import android.os.Bundle; import android.app.Activity; import android.app.ListActivity; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; public class MainActivity extends ListActivity { String[] strs = {"aa","xx","ss","dd","ff","gg","hh","jj"}; private ListView listview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listview= (ListView) findViewById(R.id.list_view); // listview.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, strs)); // listview.setOnItemClickListener(listener); listview.setAdapter(new MyAdapter()); } public class MyAdapter extends BaseAdapter{ @Override public int getCount() { // TODO Auto-generated method stub return 3; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub // ImageView image = new ImageView (MainActivity.this); // image .setImageResource(R.drawable.ic_launcher); // System.out.println("getView"+position); View view =View.inflate(MainActivity.this, R.layout.activity_main, null); // return image; return view; } } }
第一部分布局
<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" tools:context=".MainActivity" android:orientation="vertical"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="mjl" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/image" android:src="@drawable/ic_launcher" /> <CheckBox android:id="@+id/check" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
第二部分
<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" tools:context=".MainActivity" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/lianxi_ren" android:layout_weight="0"/> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/list_view" android:layout_weight="1"></ListView> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button" android:text="button" android:layout_weight="0"/> </LinearLayout> <!-- <ListView 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:id="@+id/list_view" tools:context=".MainActivity" > </ListView> -->