1.创建一个activity,命名为:ListViewActivity
public class ListViewActivity extends AppCompatActivity {
private static final String TAG = "ListViewActivity";
private ListView lv_planet;
private Drawable drawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
ArrayList<Planet> planetList = Planet.getDefaultList();
PlanetListAdapter adapter = new PlanetListAdapter(this, planetList);
lv_planet = findViewById(R.id.lv_planet);
lv_planet.setAdapter(adapter);
lv_planet.setOnItemClickListener(adapter);
lv_planet.setOnItemLongClickListener(adapter);
drawable = getResources().getDrawable(R.drawable.divider_red2);
initDividerSpinner();
}
private void initDividerSpinner() {
ArrayAdapter<String> dividerAdapter = new ArrayAdapter<String>(this, R.layout.item_select, dividerArray);
Spinner sp = findViewById(R.id.sp_list);
sp.setPrompt("请选择分割线显示样式");
sp.setAdapter(dividerAdapter);
sp.setOnItemSelectedListener(new DividerSelectedListener());
}
private String[] dividerArray = {
"不显示分隔线(分隔线高度为0)",
"不显示分隔线(分隔线为null)",
"只显示内部分隔线(先设置分隔线高度)",
"只显示内部分隔线(后设置分隔线高度)",
"显示底部分隔线(高度是wrap_content)",
"显示底部分隔线(高度是match_parent)",
"显示顶部分隔线(别瞎折腾了,显示不了)",
"显示全部分隔线(看我用padding大法)"
};
class DividerSelectedListener implements AdapterView.OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int dividerHeight = 5;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lv_planet.setDivider(drawable);
lv_planet.setDividerHeight(dividerHeight);
lv_planet.setPadding(0, 0, 0, 0);
lv_planet.setBackgroundColor(Color.TRANSPARENT);
switch (position) {
case 0:
lv_planet.setDividerHeight(0);
break;
case 1: {
lv_planet.setDivider(null);
lv_planet.setDividerHeight(dividerHeight);
}
break;
case 2: {
lv_planet.setDividerHeight(dividerHeight);
lv_planet.setDivider(drawable);
}
break;
case 3: {
// 代码中要设置分割线,务必先调用 setDivider 再调用 setDividerHeight 方法
lv_planet.setDivider(drawable);
lv_planet.setDividerHeight(dividerHeight);
}
break;
case 4: {
lv_planet.setFooterDividersEnabled(true);
}
break;
case 5: {
params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1);
lv_planet.setFooterDividersEnabled(true);
}
break;
case 6: {
params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1);
lv_planet.setFooterDividersEnabled(true);
lv_planet.setHeaderDividersEnabled(true); // 顶部分割线显示不了,Android的bug,用padding代替
}
break;
case 7: {
lv_planet.setDivider(null);
lv_planet.setDividerHeight(dividerHeight);
lv_planet.setPadding(0, dividerHeight, 0, dividerHeight);
lv_planet.setBackground(drawable);
}
break;
}
lv_planet.setLayoutParams(params);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
}
2. ListViewActivity 对应的布局文件 activity_list_view 如下:
<?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:padding="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_height="40dp">
<TextView
android:id="@+id/tv_type"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:layout_alignParentLeft="true"
android:text="分割线显示"
android:textColor="@color/black"
android:textSize="17sp" />
<Spinner
android:id="@+id/sp_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@id/tv_type"
android:spinnerMode="dialog" />
</RelativeLayout>
<ListView
android:id="@+id/lv_planet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerHeight="5dp"
android:divider="@drawable/divider_red2"
android:headerDividersEnabled="true"
android:footerDividersEnabled="true" />
</LinearLayout>
3. ListView对应的适配器为PlanetListAdapter 如下:
public class PlanetListAdapter extends BaseAdapter implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
private Context mContext;
private ArrayList<Planet> mPlanetList;
public PlanetListAdapter(Context context, ArrayList<Planet> arrayList) {
mContext = context;
mPlanetList = arrayList;
}
@Override
public int getCount() {
return mPlanetList.size();
}
@Override
public Object getItem(int position) {
return mPlanetList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list, null);
holder.iv_icon = convertView.findViewById(R.id.iv_icon);
holder.tv_name = convertView.findViewById(R.id.tv_name);
holder.tv_desc = convertView.findViewById(R.id.tv_desc);
convertView.setTag(holder); // 将视图持有者保存到转换视图中
} else {
holder = (ViewHolder)convertView.getTag();
}
Planet info = mPlanetList.get(position);
holder.iv_icon.setImageResource(info.image);
holder.tv_name.setText(info.name);
holder.tv_desc.setText(info.desc);
return convertView;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String desc = String.format("您点击了第%d个行星,它的名字是%s", position + 1, mPlanetList.get(position).name);
ToastUtil.showToast(mContext, desc);
}
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
String desc = String.format("您长按了第%d个行星,它的名字是%s", position + 1, mPlanetList.get(position).name);
ToastUtil.showToast(mContext, desc);
return true;
}
public final class ViewHolder {
public ImageView iv_icon;
public TextView tv_name;
public TextView tv_desc;
}
}
4. PlanetListAdapter 适配器对应的布局文件为item_list 如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:background="@color/white"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_height="100dp">
<ImageView
android:id="@+id/iv_icon"
android:layout_marginLeft="5dp"
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_weight="1"
android:scaleType="fitCenter" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_marginLeft="5dp"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="20sp"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_desc"
android:layout_width="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_height="0dp"
android:layout_weight="2"
android:textColor="@color/gray"
android:textSize="13sp" />
</LinearLayout>
</LinearLayout>
5. PlanetListAdapter适配器中用到的模型为 Planet 如下:
public class Planet {
public int image;
public String name;
public String desc;
public Planet(int image, String name, String desc) {
this.image = image;
this.name = name;
this.desc = desc;
}
private static int[] iconArray = {R.drawable.shuixing, R.drawable.jinxing, R.drawable.diqiu,
R.drawable.huoxing, R.drawable.muxing, R.drawable.tuxing};
private static String[] nameArray = {"水星", "金星", "地球", "火星", "木星", "土星"};
private static String[] descArray = {
"水星是太阳系八大行星最内侧也是最小的一颗行星,也是离太阳最近的行星",
"金星是太阳系八大行星之一,排行第二,距离太阳0.725天文单位",
"地球是太阳系八大行星之一,排行第三,也是太阳系中直径、质量和密度最大的类地行星,距离太阳1.5亿公里",
"火星是太阳系八大行星之一,排行第四,属于类地行星,直径约为地球的53%",
"木星是太阳系八大行星中体积最大、自转最快的行星,排行第五。它的质量为太阳的千分之一,但为太阳系中其它七大行星质量总和的2.5倍",
"土星为太阳系八大行星之一,排行第六,体积仅次于木星"
};
public static ArrayList<Planet> getDefaultList() {
ArrayList<Planet> planetList = new ArrayList<Planet>();
for (int i = 0; i < nameArray.length; i++) {
planetList.add(new Planet(iconArray[i], nameArray[i], descArray[i]));
}
return planetList;
}
}
6. 效果图如下:
- 第一张图是:不显示分隔线(分隔线高度为0)
- 第二张图是:只显示内部分隔线(先设置分隔线高度)
- 第三张图是:只显示内部分隔线(后设置分隔线高度)
- 第四站图是:显示全部分隔线(看我用padding大法)
7. ListView 的注意点
- 注意1:代码中要设置分割线,务必先调用 setDivider 再调用 setDividerHeight 方法,否则分割线的高度是分割图片的高度而不是你设置的高度
- 注意2:setHeaderDividersEnabled 顶部分割线显示不了,Android的bug,用padding代替