Android 使布局可点击
在我的应用程序中,我有几个片段具有不同的Android 使布局可点击,android,onclicklistener,Android,Onclicklistener,在我的应用程序中,我有几个片段具有不同的ListViews,但对于所有片段,我希望使用相同的空视图,因此我使用空视图创建了一个单独的布局,并使用 <include android:id="@+id/empty" layout="@layout/empty_view"/> fragment_list.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
ListView
s,但对于所有片段,我希望使用相同的空视图,因此我使用空视图创建了一个单独的布局,并使用
<include
android:id="@+id/empty"
layout="@layout/empty_view"/>
fragment_list.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">
<Spinner
android:id="@+id/spinner_filter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/activity_vertical_margin"
android:layout_marginLeft="@dimen/activity_vertical_margin"
android:layout_gravity="center_horizontal"
android:visibility="gone"/>
<include
android:id="@+id/empty"
layout="@layout/empty_view"/>
<ProgressBar
android:id="@+id/loading_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:indeterminate="true"
style="@android:style/Widget.Holo.ProgressBar.Large"/>
<LinearLayout
android:id="@+id/list_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primary_color"
android:padding="@dimen/activity_vertical_margin"
android:layout_below="@id/spinner_filter"
android:visibility="gone">
<TextView
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"
android:textColor="@color/white_90pct"
android:textAllCaps="true"
android:text="@string/subject_indicator"/>
<View
android:layout_width="1dp"
android:layout_height="15dp"
android:layout_gravity="center_vertical"
android:background="@color/light_color"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:singleLine="true"
android:gravity="end"
android:textColor="@color/white_90pct"
android:textAllCaps="true"
android:text="@string/grade_indicator"/>
</LinearLayout>
<ListView
android:id="@+id/semesters_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/list_header"/>
</RelativeLayout>
我还尝试将下一个属性添加到xml中,但仍然不起作用:
android:focusableInTouchMode="true"
android:focusable="true"
android:descendantFocusability="blocksDescendants"
试一试
您是否将empty\u视图
包含到另一个阻止子体聚焦的视图中
自编辑后
您的ListView位于空视图和阻止可点击性的顶部。由于您已将
线性布局指定为可点击
,因此只需指定onClick
事件的方法即可
因此,添加您的linearLayouts xml,单击onClick
属性如下:(focusable
可能也需要)
由于空视图填充其父视图,请确保空视图的include标记是其他布局中的最后一个,因此它将在z顺序中位于最顶端。尝试为各个项目提供clickListener,然后选中,如LinearLayout emptyView=(LinearLayout)视图。findViewById(R.id.empty);emptyView.findViewById(R.id.emptyTextView.setOnClickListener(此);emptyView.findViewById(R.id.emptyImageView.setOnClickListener(此)代码>@Antromet,我有点想避免这种情况,因为我需要添加更多的侦听器,以防在空视图布局中添加更多视图。这就是为什么我想在listview的android:clickable=“true”
下,设置android:onClick=“test”
,使整个视图可点击。然后在代码中创建方法test(View v){}
,并查看是否调用了该方法。(在它里面放点东西并设置断点)。另外,删除此测试的任何编码侦听器。我测试了您的代码,发现它可以工作;点击onClick
中的断点。另外,当您调用setOnClickListener()
时,根据文档,视图组将变得可单击。这是否与提供默认的OnClickListener
相同?不管怎样,我试过了,但没有成功。您认为这与空视图布局不是原始布局的一部分这一事实有关吗?正如我所说,它是使用
添加到主布局的。我编辑了我的问题,并在使用空视图的地方添加了片段的完整布局。你能检查一下有什么问题吗?您是否测试了将空视图添加为
的代码?我确实以这种方式进行了测试,但在XML中的空视图下面没有其他视图。随着XML的深入,兄弟姐妹会被添加到彼此之上。是的,就是这样。非常感谢!你是对的!显然有什么东西挡住了它,我将布局移到了xml文件的底部,这就解决了它!谢谢!
LinearLayout emptyView = (LinearLayout) view.findViewById(R.id.empty);
emptyView.setOnClickListener(this);
android:focusableInTouchMode="true"
android:focusable="true"
android:descendantFocusability="blocksDescendants"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:onClick="layoutClicked"
android:focusable="true"
android:gravity="center">
public void layoutClicked(View v)
{
// Your code here
}