自定义ArrayAdapter实现listview分割线的多种颜色

   前不久在做一道安卓实验练习题时发现listview的item下方分割线的颜色有所不同,如下图两箭头所示,通过对比即可分辨。当时第一时间想到的是listview有没有相关的方法可以实现这一效果,但经过一番查找,没有。那它是如何达到这种效果的呢,又经过一番思索,发现是自定义ArrayAdater,更改子项view的布局而来的。




   废话少说,贴上代码如下:

  1.activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<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.yekong.homework4.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="ArrayList Demo"
        android:textSize="30dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:background="@color/tool_bg"
        />
  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      >
    <ListView
        android:id="@+id/listview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="#f00"
        android:dividerHeight="2dp"

        >
    </ListView>
  </LinearLayout>

    <ListView
        android:id="@+id/listview2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@drawable/bg1"
        android:dividerHeight="2dp"
        >
    </ListView>
</LinearLayout>

  2.array_item.xml

 <?xml version="1.0" encoding="utf-8"?>
<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"
    >

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/show_text"
    android:paddingStart="?android:attr/expandableListPreferredItemPaddingLeft"
    android:textAppearance="?android:attr/textAppearanceListItem"
    android:textAlignment="viewStart"
    >
</TextView>
 <!--注意这里定义了一个TextView控件,作为分割线-->
 <TextView
     android:id="@+id/line_bg"
     android:layout_width="match_parent"
     android:layout_height="2dp" />
</LinearLayout>

  3.list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/list_item_textview"
    android:paddingStart="?android:attr/expandableListPreferredItemPaddingLeft"
    android:textAppearance="?android:attr/textAppearanceListItem"
    android:textAlignment="viewStart"
    android:checked="true"
    android:checkMark="?android:attr/textCheckMark"
    >
</CheckedTextView>

  4.MainActivit.java

package com.example.yekong.homework4;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
    private ListView listView1=null;
    private ListView listView2=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();

    }

    public void init(){
        listView1=(ListView)findViewById(R.id.listview1);
        listView2=(ListView)findViewById(R.id.listview2);
        MyAdapter adapter=new MyAdapter(this,R.layout.array_item,getResources().getStringArray(R.array.list_content1));
        listView1.setAdapter(adapter);
        listView2.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item,getResources().getStringArray(R.array.list_content2)));

        listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
              getToast("you  click item "+i);
            }
        });

    }

    public void getToast(String content){
        Toast.makeText(this,content,Toast.LENGTH_SHORT).show();
    }
}

  5.MyAdapter.java

package com.example.yekong.homework4;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by YeKong on 2017/5/31.
 */
//自定义ArrayAdapter,;重载子view布局
public class MyAdapter extends ArrayAdapter<String> {
    Context context;
    LayoutInflater inflater;
    List<String> list=null;
    String[] str=null;
    public MyAdapter(Context context, int resource, String[] objects) {
        super(context, resource, objects);
        this.context=context;
        this.inflater=LayoutInflater.from(context);
        this.str=objects;
    }

    public MyAdapter(Context context, int resource, List<String> objects) {
        super(context, resource, objects);
        this.context=context;
        this.list=objects;
    }

    @Override
    public String getItem(int position) {
        return super.getItem(position);
    }

    @Override
    public int getPosition(String item) {
        return super.getPosition(item);
    }

    @Override
    public int getCount() {
        return super.getCount();
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Holder holder;
        if(convertView==null){
            holder = new Holder();
            convertView=inflater.inflate(R.layout.array_item, null);//加载子项item的布局文件
            holder.textView1=(TextView) convertView.findViewById(R.id.show_text);
            holder.textView2=(TextView) convertView.findViewById(R.id.line_bg);
            convertView.setTag(holder);
        }else{
            holder=(Holder)convertView.getTag();
        }
        holder.textView1.setText(str[position]);
        switch (position){
            case 0:
                holder.textView2.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
            break;
            case 1:
                holder.textView2.setBackgroundColor(context.getResources().getColor(R.color.divider1));
                break;
            case 2:
                holder.textView2.setBackgroundColor(context.getResources().getColor(R.color.divider2));
                break;
        }
        return convertView;
    }

    class Holder{
        TextView textView1,textView2;
    }
}
   运行项目,效果如下图,可以看到第一个listview中每条分割线的颜色都不一样了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值