我们怎样才能根据所选(点击)项目为listview中的1个项目设置不同的背景颜色,字体颜色?
我想在点击列表视图项目时制作活动按钮样式,我们可以这样做吗?
我能够获得点击项目的价值,那么我们如何设置该列表视图项目的行为与其他项目不同?
答案 0 :(得分:1)
您可以按照以下步骤操作:
1)首先,创建单行ListView
(single_row.xml
)的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:id="@+id/llList"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="TextView" />
</LinearLayout>
2)为ListView
创建 自定义适配器:
public class CustomListAdapter extends BaseAdapter{
String [] text;
Context context;
int selectedPosition;
private static LayoutInflater inflater=null;
public CustomListAdapter(Context context, String[] text, int selectedPosition) {
// TODO Auto-generated constructor stub
this.context = context;
this.text = text;
this.selectedPosition = selectedPosition;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView tv;
LinearLayout llList;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.single_row, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.llList=(LinearLayout) rowView.findViewById(R.id.llList);
holder.tv.setText(text[position]);
holder.llList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.llList.setBackgroundColor(Color.CYAN);
}
});
return rowView;
}
}
3)将此自定义适配器设置为ListView
中的onCreate()
:
int selectedItem = 0; //Declare this Globally
lv=(ListView) findViewById(R.id.listView);
lv.setAdapter(new CustomListAdapter(this, yourStringList , selectedItem));
4)然后获取 ListView
到onItemSelectedListener:
的所选位置,并将其传递给适配器。
lv.setOnItemClickListener(new View.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
selectedItem = position;
CustomListAdapter.notifyDataSetChanged();
}
};
希望这有帮助。
答案 1 :(得分:0)
定义您自己的CheckableLinearLayout
工具Checkable
并覆盖abstract
方法。
让CheckableLinearLayout
成为项目视图的根布局。然后,为根视图创建一个选择器以添加所选的效果。
ListView
chooseMode - &gt; singleMode
答案 2 :(得分:0)
您可以跟踪当前所选元素的位置,如下所示:
OnItemClickListener listViewOnItemClick = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
mSelectedItem = position;
mAdapter.notifyDataSetChanged();
}
};
并覆盖适配器的getView方法:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View view = View.inflate(context, R.layout.item_list, null);
if (position == mSelectedItem) {
// set your color
}
return view;
}