之前一直想总结一下这样知识,后面各种忙,就不想写。还是自已总结一下比较好。listview需要注意事项有很多吧,也是最常用的控件之一。我们可以自动计算listview的高度,当与滚动条一起使用的时候。listview分隔线也可以去掉,或者更改。还有与各种控件一起使用的监听。接下来看看我所介绍的以及效果图。
本文项目demo地址:点击此处下载
一、看项目主要类
package com.example.customlistviewdemo; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.LinearLayout; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.Toast; public class CustomListviewActivity extends Activity implements OnClickListener, OnItemLongClickListener { private ListView myListView; private MyCustomListViewAdapter mCustomListViewAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_custom_listview); myListView = (ListView) findViewById(R.id.listView1); mCustomListViewAdapter = new MyCustomListViewAdapter( getApplicationContext()); myListView.setOnItemLongClickListener(this); // 长按item监听 myListView.setAdapter(mCustomListViewAdapter); setListViewHeightBasedOnChildren(myListView); // listview自动计算高度 Button button = (Button) findViewById(R.id.button1); button.setOnClickListener(this); } // listview自动计算高度 private void setListViewHeightBasedOnChildren(ListView listView) { // 获取ListView对应的Adapter ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; for (int i = 0, len = listAdapter.getCount(); i < len; i++) { View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); // 计算子项View 的宽高 totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度 } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); } public List<String> getDataSource() { ArrayList<String> list = new ArrayList<String>(); list.add("北京"); list.add("上海"); list.add("广州"); list.add("南京"); list.add("苏州"); list.add("江苏"); list.add("杭州"); list.add("乌镇"); list.add("丽江"); list.add("大理"); list.add("昆明"); list.add("哈尔滨"); return list; } public class MyCustomListViewAdapter extends BaseAdapter implements OnCheckedChangeListener, OnClickListener { private Context mContext; private LayoutInflater mLayoutInflater; public MyCustomListViewAdapter(Context context) { mContext = context; mLayoutInflater = (LayoutInflater) mContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return getDataSource().size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int arg0) { return 0; } @Override public View getView(int position, View arg1, ViewGroup arg2) { LinearLayout linearLayout = (LinearLayout) mLayoutInflater.inflate( R.layout.layout_custom_checkbox_item, null); linearLayout.setOnClickListener(this); linearLayout.setTag(position); TextView titleTextView = (TextView) linearLayout .findViewById(R.id.textView1); if (titleTextView != null) titleTextView.setText(getDataSource().get(position)); // 控件 是否选中 CheckBox selectedCheckBox = (CheckBox) linearLayout .findViewById(R.id.checkBox1); selectedCheckBox.setOnCheckedChangeListener(this); return linearLayout; } @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { Integer nPosition = (Integer) (arg0.getTag()); Toast.makeText(getApplication(), "CheckBox选中事件:", Toast.LENGTH_SHORT).show(); } @Override public void onClick(View arg0) { // 单击事件响应 int position = ((Integer) arg0.getTag()).intValue(); Toast.makeText(getApplication(), "listview单击事件:" + getDataSource().get(position), Toast.LENGTH_SHORT).show(); } } @Override public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(getApplicationContext(), "listview长按事件:", Toast.LENGTH_SHORT).show(); return true; } @Override public void onClick(View arg0) { if (arg0.getId() == R.id.button1) { Toast.makeText(getApplicationContext(), "button事件:", Toast.LENGTH_SHORT).show(); } } }大家都知道,listview是可以自已滚动的,但也可以禁止滚动。可能你不需要它滚动。只需要滚动条滚动。我就是遇到过这种需求,listview与滚动条一起使用情况。代码上也有所说明了。
二、下面看看xml
<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" > <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" > <ScrollView android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="0.96" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="自定义listview样式" android:textSize="18sp" /> </LinearLayout> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="400dp"> </ListView> </LinearLayout> </ScrollView> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:layout_gravity="bottom" android:gravity="center" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:textSize="18sp" /> </LinearLayout> </FrameLayout> </LinearLayout>这里的xml还用到浮动的FrameLayout,用来显示button键。
三、下面来看看listview的item的xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:longClickable="true" android:clickable="true" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_weight="1.02" android:gravity="center_vertical|left" android:paddingLeft="10dp" android:textColor="#000" android:text="TextView" android:textSize="18sp" /> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="match_parent" android:paddingRight="10dp" android:textColor="#000" android:layout_gravity="right" /> </LinearLayout> </LinearLayout>
这里说明一下,LinearLayout里需要这两句。分别是listview单击和长按的设置。
android:longClickable="true" android:clickable="true"
四、接下来看看效果图:
五、最后补充一下,去掉listview分隔线或者设置。
去掉分隔线有三种方法:
1、myListView.setDividerHeight(0);
2、myListView.setDivider(null);
设置或者换一个好看的自动的分隔线也行
android:divider="@drawable/skin_line"
到这里就结束了,本文中讲的都是监听以及布局,关于listview以及滚动条,checkbox,buuton一起使用。
本文项目demo地址:点击此处下载