带有渐变背景的ListView的行按下颜色
ListView's row pressed color with gradient background
我希望我的 ListView 中的行具有渐变背景,并且我希望它们在按下时变为纯色。但我做对了。
我知道 ListViews 存在的问题,并且我阅读了其他主题中的一些解决方案,但其中 none 似乎对我有用。任何帮助都会很棒。
当我 运行 下面的代码时,我得到了这张没问题的图片:
但是当我按下第 2 项时,所有行都会变成绿色!:
这是我的适配器:
public class MyAdapter extends BaseAdapter {
private final Context mContext;
private ArrayList<String> mMenuItems;
StateListDrawable mBackgroundDrawable;
public MyAdapter(Context context) {
this.mContext = context;
mMenuItems= new ArrayList<>();
for (int i = 0; i < 10; i++) {
mMenuItems.add(String.format("Item %d", i));
}
// make background drawable
GradientDrawable backgroundDrawable = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
new int[]{ Color.RED, Color.YELLOW, Color.RED});
backgroundDrawable.setCornerRadius(0f);
ColorDrawable selectedColorDrawable= new ColorDrawable(Color.GREEN);
mBackgroundDrawable = new StateListDrawable();
mBackgroundDrawable.addState(new int[] {android.R.attr.state_pressed}, selectedColorDrawable);
mBackgroundDrawable.addState(new int[] {android.R.attr.state_focused}, selectedColorDrawable);
mBackgroundDrawable.addState(new int[] {android.R.attr.state_selected}, selectedColorDrawable);
mBackgroundDrawable.addState(new int[] {}, backgroundDrawable);
}
@Override
public int getCount() {
return mMenuItems.size();
}
@Override
public Object getItem(int position) {
return mMenuItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewGroup item = getViewGroup(convertView);
if (Build.VERSION.SDK_INT<Build.VERSION_CODES.JELLY_BEAN) {
item.setBackgroundDrawable(mBackgroundDrawable);
} else {
item.setBackground(mBackgroundDrawable);
}
// set title
TextView titleView= (TextView) item.findViewById(R.id.menu_title);
titleView.setText(mMenuItems.get(position));
return item;
}
public ViewGroup getViewGroup(View reuse)
{
if(reuse instanceof ViewGroup)
return (ViewGroup)reuse;
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup item = (ViewGroup)inflater.inflate(R.layout.row_menu, null);
return item;
}
}
我的row_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="@+id/menu_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"/>
</RelativeLayout>
我的activity的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<ListView
android:id="@+id/activity_main_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
我的activity:
public class MainActivity extends ActionBarActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView) findViewById(R.id.activity_main_listview);
list.setAdapter(new MyAdapter(this));
}
}
mBackgroundDrawable 对所有行都是通用的。您需要将唯一的 mBackgroundDrawable 附加到每一行(例如在您添加它的循环内)。这非常简单,但如果您需要具体示例,请随时询问! ;)
您可以通过 XML 它自己实现它。
列表视图
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector"
android:background="@color/list_background"/>
drawables/list_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_activated="true"/>
</selector>
drawables/list_item_bg_pressed.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="@color/list_background_pressed"
android:endColor="@color/list_background_pressed"
android:angle="90" />
</shape>
drawables/list_item_bg_normal.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="@color/list_background"
android:endColor="@color/list_background"
android:angle="90" />
</shape>
如您所见,ListView 有一个名为 android:listSelector 的属性,它的值为“@drawable/list_selector”。这是我们设置选择器的地方。
我认为其余的是不言自明的。
如果您需要更多说明,请阅读此 http://javatechig.com/android/android-listview-tutorial
我希望我的 ListView 中的行具有渐变背景,并且我希望它们在按下时变为纯色。但我做对了。 我知道 ListViews 存在的问题,并且我阅读了其他主题中的一些解决方案,但其中 none 似乎对我有用。任何帮助都会很棒。
当我 运行 下面的代码时,我得到了这张没问题的图片:
但是当我按下第 2 项时,所有行都会变成绿色!:
这是我的适配器:
public class MyAdapter extends BaseAdapter {
private final Context mContext;
private ArrayList<String> mMenuItems;
StateListDrawable mBackgroundDrawable;
public MyAdapter(Context context) {
this.mContext = context;
mMenuItems= new ArrayList<>();
for (int i = 0; i < 10; i++) {
mMenuItems.add(String.format("Item %d", i));
}
// make background drawable
GradientDrawable backgroundDrawable = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
new int[]{ Color.RED, Color.YELLOW, Color.RED});
backgroundDrawable.setCornerRadius(0f);
ColorDrawable selectedColorDrawable= new ColorDrawable(Color.GREEN);
mBackgroundDrawable = new StateListDrawable();
mBackgroundDrawable.addState(new int[] {android.R.attr.state_pressed}, selectedColorDrawable);
mBackgroundDrawable.addState(new int[] {android.R.attr.state_focused}, selectedColorDrawable);
mBackgroundDrawable.addState(new int[] {android.R.attr.state_selected}, selectedColorDrawable);
mBackgroundDrawable.addState(new int[] {}, backgroundDrawable);
}
@Override
public int getCount() {
return mMenuItems.size();
}
@Override
public Object getItem(int position) {
return mMenuItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewGroup item = getViewGroup(convertView);
if (Build.VERSION.SDK_INT<Build.VERSION_CODES.JELLY_BEAN) {
item.setBackgroundDrawable(mBackgroundDrawable);
} else {
item.setBackground(mBackgroundDrawable);
}
// set title
TextView titleView= (TextView) item.findViewById(R.id.menu_title);
titleView.setText(mMenuItems.get(position));
return item;
}
public ViewGroup getViewGroup(View reuse)
{
if(reuse instanceof ViewGroup)
return (ViewGroup)reuse;
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup item = (ViewGroup)inflater.inflate(R.layout.row_menu, null);
return item;
}
}
我的row_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="@+id/menu_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"/>
</RelativeLayout>
我的activity的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<ListView
android:id="@+id/activity_main_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
我的activity:
public class MainActivity extends ActionBarActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView) findViewById(R.id.activity_main_listview);
list.setAdapter(new MyAdapter(this));
}
}
mBackgroundDrawable 对所有行都是通用的。您需要将唯一的 mBackgroundDrawable 附加到每一行(例如在您添加它的循环内)。这非常简单,但如果您需要具体示例,请随时询问! ;)
您可以通过 XML 它自己实现它。
列表视图
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector"
android:background="@color/list_background"/>
drawables/list_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_activated="true"/>
</selector>
drawables/list_item_bg_pressed.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="@color/list_background_pressed"
android:endColor="@color/list_background_pressed"
android:angle="90" />
</shape>
drawables/list_item_bg_normal.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="@color/list_background"
android:endColor="@color/list_background"
android:angle="90" />
</shape>
如您所见,ListView 有一个名为 android:listSelector 的属性,它的值为“@drawable/list_selector”。这是我们设置选择器的地方。 我认为其余的是不言自明的。
如果您需要更多说明,请阅读此 http://javatechig.com/android/android-listview-tutorial