您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。