В моем приложении у меня есть несколько фрагментов с разными ListView
s, но для всех них я хочу использовать тот же пустой вид, поэтому я создал отдельный макет с пустым представлением и добавил его к другим макетам, используяСделать макет clickable
<include
android:id="@+id/empty"
layout="@layout/empty_view"/>
Все работает, как ожидалось, но сейчас я пытаюсь сделать что посмотреть в ссылку, так что я могу перезагрузить ListView
, когда пользователь нажимает на пустой взгляд, но я не могу заставить его работать. Любая идея о том, как это сделать?
Это то, что я пытался до сих пор:
(empty_view.xml)
<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:gravity="center">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="center"
android:src="@drawable/error_icon"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#929292"
android:textSize="24sp"
android:text="@string/empty_list_error"/>
</LinearLayout>
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>
Java класс (onCreateView)
LinearLayout emptyView = (LinearLayout) view.findViewById(R.id.empty);
emptyView.setOnClickListener(this);
Я также попытался добавить Следующий атрибуты в XML, но до сих пор не работает:
android:focusableInTouchMode="true"
android:focusable="true"
android:descendantFocusability="blocksDescendants"
попробуйте дать clickListener отдельным элементам и проверить, например 'LinearLayout emptyView = (LinearLayout) view.findViewById (R.id.empty); emptyView.findViewById (R.id.emptyTextView) .setOnClickListener (this); emptyView.findViewById (R.id.emptyImageView) .setOnClickListener (this); ' – Antrromet
@Antrromet, я бы хотел этого избежать, потому что тогда мне нужно будет добавить больше слушателей, если я добавлю больше представлений в пустой макет представления. Вот почему я хочу, чтобы весь вид clickable –
под 'android: clickable =" true "' в вашем списке, установите 'android: onClick =" test "'. то в вашем коде создайте метод 'test (View v) {}' и посмотрите, вызвано ли это. (поместите что-то внутри и точку останова). Плюс удалить все кодированные слушатели для этого теста – Doomsknight