我试图让一个列表视图看起来在它的顶部逐渐变黑。本质上我想变成这样的东西:
变成这样的东西:
我尝试了两种不同的方法:第一种是覆盖 ListView 的 onDraw(),这不起作用,我的“额外”绘图发生在 listView 后面。第二种方法是在 listView 顶部放置另一个视图,这看起来不错,但是如果用户尝试通过触摸视图所在的屏幕来滚动列表,则视图不滚动,看起来视图消耗了 touchevent,所以大约三分之一的名单是不可触碰的。
关于如何实现这一点的任何提示?
我试图让一个列表视图看起来在它的顶部逐渐变黑。本质上我想变成这样的东西:
变成这样的东西:
我尝试了两种不同的方法:第一种是覆盖 ListView 的 onDraw(),这不起作用,我的“额外”绘图发生在 listView 后面。第二种方法是在 listView 顶部放置另一个视图,这看起来不错,但是如果用户尝试通过触摸视图所在的屏幕来滚动列表,则视图不滚动,看起来视图消耗了 touchevent,所以大约三分之一的名单是不可触碰的。
关于如何实现这一点的任何提示?
您可以在自定义视图中分配列表项背景。在基于项目位置的 getview(适配器类)中。在xml中创建一个渐变,并根据位置增加alpha值。
这是对我有用的代码片段。
列表项.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="match_parent"
android:gravity="center_vertical"
android:minHeight="64dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="17sp"
android:textColor="@android:color/white"
android:id="@+id/textView"/>
</RelativeLayout>
项目适配器.java
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(rowResourceId, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.textView);
int id = Integer.parseInt(Ids[position]);
textView.setText(Model.GetbyId(id).name);
// this is the main idea behind the UX look n feel.
rowView.setBackgroundColor(context.getResources().getColor(android.R.color.black));
textView.setAlpha((float) position/Ids.length);
return rowView;
}
请随时在 getView 中使用持有人设计模式。这只是一个概念证明。