【发布时间】:2014-04-29 04:16:50
【问题描述】:
我正在测试创建复合视图,因此我从包含 ImageView 和 TextView 的线性布局开始。这些 LinearLayout 将表示类似于部分列表的视图,并且我将其设置为可点击的。布局的内部视图具有选择器作为其颜色(TextView)和图像(ImageView)。在 API 19 (KitKat) 中,点击行为正常(屏幕截图 1),但在 API 15 和 API 10(屏幕截图 2)中,点击并没有改变选择器状态。
每个“列表项”的代码是:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/listitem_login"
android:clickable="true"
android:gravity="center"
android:orientation="horizontal"
android:padding="15dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:src="@drawable/signin_signup_icon_facebook" />
<TextView
style="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Facebook"
android:textColor="@color/option_text_color" />
</LinearLayout>
编辑:我使用了触摸侦听器方法。如果有一天我找到了最好的解决方案(没有 Java 代码),我会在这里进行编辑。我的方法,基于答案(将 View.OnTouchListener 添加到 LinearLayouts):
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
ViewGroup container = (ViewGroup) view;
boolean pressed;
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) pressed = true;
else if (motionEvent.getAction() == MotionEvent.ACTION_UP) pressed = false;
else return false;
for (int i = 0; i < container.getChildCount(); i++) {
container.getChildAt(i).setPressed(pressed);
}
return false;
}
在旧的 2.3 Android 中,代码运行速度有点慢,但也能正常工作。
【问题讨论】:
-
请发布您的适配器文件代码
-
这不是一个列表。这是一个带有内部 LinearLayouts 的 LinearLayout
-
查看我的更新答案
标签: java android android-layout