我希望在ListView中有渐变背景的行,我希望它们在按下时更改为纯色。但我无法做到对。 我知道ListViews有的问题,我在其他主题中阅读了一些解决方案,但它们似乎都不适合我。任何帮助都会很棒。
当我运行以下代码时,我得到的图像没问题:
但是当我按下第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>
我的活动布局:
<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>
我的活动:
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));
}
}
答案 0 :(得分:1)
mBackgroundDrawable对于所有行都很常见。您需要将唯一的mBackgroundDrawable附加到每一行(例如,在您添加它的循环内)。这非常简单,但随意询问您是否需要一个具体的例子! ;)
答案 1 :(得分:1)
你可以通过XML自己实现它。
的ListView
<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"/>
可绘/ 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>
可绘/ 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>
可绘/ 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的attr,它的值是“@ drawable / list_selector”。这是我们设置选择器的地方。 我认为其余的都是自我解释。
如果您需要更多说明,请阅读此http://javatechig.com/android/android-listview-tutorial