玩转listview
讲道理 现在是recyleview的时代了,但是listview还是值得学习
让我们来看看listview的继承关系
java.lang.Object
↳ android.view.View
↳ android.view.ViewGroup
↳ android.widget.AdapterView<T extends android.widget.Adapter>
↳ android.widget.AbsListView
↳ android.widget.ListView
与recycleview一样listview也是需要3个元素
.1 展示界面列表的view
.2适配器用来给view加载数据
.3数据(图片,文字,或者基本组件)
适配器的种类有多种,一般用的有ArrayAdapter,simpleAdapter和SimpleCursorAdapter。
先写一个基本的listview的案例来熟悉依稀listview的用法
public class ListViewTest extends Activity {
private ListView listview;
private String[] data = null;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listviewtest);
listview = (ListView) findViewById(R.id.listview);
data = new String[] { "1", "2", "3", "4", "5", };
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
listview.setAdapter(adapter);
}
}
以上代码很简单,完全满足3个元素的基本要求
下面再来看看其他的
然后再写一个自定义适配器布局 就是把listview的子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"
android:orientation="vertical"
tools:context="com.example.demotest.MainActivity" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
再是listview中的单个项目的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
tools:context="com.example.demotest.MainActivity" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
listview单个项目的封装类
public class ListviewItem {
private String name;
private int imageid;
public ListviewItem(String name, int imageid) {
super();
this.name = name;
this.imageid = imageid;
}
public String getName() {
return name;
}
public int getImageid() {
return imageid;
}
}
继承 ArrayAdapter的适配器ItemAdapter的代码如下:
public class ItemAdapter extends ArrayAdapter<ListviewItem> {
private int resourecId;
public ItemAdapter(Context context, int textViewResourceId, List<ListviewItem> objects) {
super(context, textViewResourceId, objects);
resourecId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ListviewItem listitem = getItem(position);
View view;
ViewHodler viewHolder;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourecId, null);
viewHolder = new ViewHodler();
viewHolder.image = (ImageView) view.findViewById(R.id.image);
viewHolder.text1 = (TextView) view.findViewById(R.id.textview1); view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHodler) view.getTag();
}
viewHolder.image.setImageResource(listitem.getImageid());
viewHolder.text1.setText(listitem.getName());
return view;
}
class ViewHodler {
ImageView image;
TextView text1;
}
}
上面的代码还是有点不熟悉,强行记下来吧,挖个坑等以后再补
最后写listviewDiy.java
代码如下:
public class ListViewDiy extends Activity {
private List<ListviewItem> listviewitems = new ArrayList<ListviewItem>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listviewtest);
initItems();
ItemAdapter adapter = new ItemAdapter(this, R.layout.listview_items, listviewitems);
ListView listview = (ListView) findViewById(R.id.listview);
listview.setAdapter(adapter);
}
private void initItems() {
ListviewItem listviewitem1=new ListviewItem("1",R.drawable.ic_launcher); listviewitems.add(listviewitem1);
ListviewItem listviewitem2=new ListviewItem("2",R.drawable.ic_launcher); listviewitems.add(listviewitem2);
ListviewItem listviewitem3=new ListviewItem("3",R.drawable.ic_launcher); listviewitems.add(listviewitem3);
}
}
也是根据3元素的结构来写的,代码很简单,主要是前面几个要认识透。不懂的闲死记下来吧。之后看懂源码后在 来补充。坑真多~~
最后在来看看listview的子项的监听事件和增删。
监听事件public void onItemClick (AdapterView
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("选择操作");
menu.add(Menu.NONE, Menu.FIRST + 1, 1, "删除");
menu.add(Menu.NONE, Menu.FIRST + 2, 2, "取消");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo acmiRef = (AdapterContextMenuInfo) item.getMenuInfo();// 用来获取item信息哎,重要
int removeIndex = acmiRef.position;
switch (item.getItemId()) {
case Menu.FIRST + 1:
listviewitems.remove(removeIndex);
adapter.notifyDataSetChanged();// 删除后刷新ListView
break;
case Menu.FIRST + 2:
break;
default:
break;
}
return false;
}