1、创建activity界面布局.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#00000000" />
</LinearLayout>
2、自定义列表的适配器adapter
package com.example.t;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyListviewAdapter extends BaseAdapter {
private LayoutInflater inflater = null;
private ArrayList<String> arraylist = null;
private Bitmap icon;
private int selectedPosition = -1;
private Context context;
public void setSelectedPosition(int position) {
selectedPosition = position;
}
public void setDatas(ArrayList<String> tmp) {
// 保存适配器中的每项的文字信息
this.arraylist.clear();
this.arraylist.addAll(tmp);
notifyDataSetChanged();
}
public MyListviewAdapter(Context context) {
this.context = context;
// LayoutInflater用来加载界面
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return arraylist.size();
}
@Override
public Object getItem(int position) {
return arraylist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
// 保存每项中的控件的引用
class ViewHolder {
TextView text;
LinearLayout layout;
}
@Override
public View getView(int position, View convert, ViewGroup parent) {
ViewHolder holder;
if (convert == null) // 实现复用行布局
{
// 调用LayoutInflater的inflate方法加载layout文件夹中的界面
convert = inflater.inflate(R.layout.lists, null);
holder = new ViewHolder();
holder.text = (TextView) convert.findViewById(R.id.text);
holder.layout = (LinearLayout) convert.findViewById(R.id.layout);
// 保存包含当前项控件的对象
convert.setTag(holder);
} else {
// 获取包含当前项控件的对象
holder = (ViewHolder) convert.getTag();
}
// 设置当前项的内容
holder.text.setText(arraylist.get(position));
// 设置选中效果
if (selectedPosition == position) {
holder.text.setTextColor(context.getResources().getColor(R.color.textcolor_select));
holder.layout.setBackgroundResource(R.drawable.list_select)
} else {
holder.text.setTextColor(context.getResources().getColor(R.color.textcolor_normal));
holder.layout.setBackgroundResource(R.drawable.list_normal)
}
return convert;
}
}
3、列表的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40px"
android:id="@+id/layout"
android:padding="6px">
<TextView android:id="@+id/text"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
4、在activity中
// 添加列表框项目点击事件
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// 设置适配器的选中项
listAdapter.setSelectedPosition(position);
// 更新列表框
listAdapter.notifyDataSetInvalidated();
}
});
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。