Skip to content

Commit

Permalink
fix(android): fix global header/footer title in ListView (#14175)
Browse files Browse the repository at this point in the history
* fix(android): fix ListView's header and footer title alignment

* fix(android): set title attributes only when required

* chore: reuse same code
  • Loading branch information
prashantsaini1 authored Jan 28, 2025
1 parent de395b2 commit 098f8c2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 62 deletions.
4 changes: 2 additions & 2 deletions android/modules/ui/res/layout/titanium_ui_listview_holder.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center_vertical"
android:textAlignment="viewStart"
android:minHeight="18dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
Expand All @@ -58,7 +58,7 @@
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center_vertical"
android:textAlignment="viewStart"
android:minHeight="18dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import java.lang.ref.WeakReference;
Expand All @@ -50,7 +51,7 @@ public class ListViewHolder extends TiRecyclerViewHolder<ListItemProxy>
private final TextView headerTitle;

// Middle
private final ViewGroup container;
private final ConstraintLayout container;
private final ImageView leftImage;
private final TiCompositeLayout content;
private final ImageView rightImage;
Expand All @@ -68,9 +69,6 @@ public ListViewHolder(final Context context, final ViewGroup viewGroup)

this.headerTitle = viewGroup.findViewById(R.id.titanium_ui_listview_holder_header_title);

// Header attributes.
setTitleAttributes("header", context, this.headerTitle);

this.container = viewGroup.findViewById(R.id.titanium_ui_listview_holder);

this.leftImage = viewGroup.findViewById(R.id.titanium_ui_listview_holder_left_image);
Expand All @@ -82,9 +80,6 @@ public ListViewHolder(final Context context, final ViewGroup viewGroup)
this.footer = viewGroup.findViewById(R.id.titanium_ui_listview_holder_footer);

this.footerTitle = viewGroup.findViewById(R.id.titanium_ui_listview_holder_footer_title);

// Footer attributes.
setTitleAttributes("footer", context, this.footerTitle);
}

/**
Expand Down Expand Up @@ -335,70 +330,24 @@ private void setHeaderFooter(TiViewProxy listViewProxy,
// Handle `header` and `footer`.
if (updateHeader) {
if (properties.containsKeyAndNotNull(TiC.PROPERTY_HEADER_TITLE)) {

// Handle header title.
this.headerTitle.setText(properties.getString(TiC.PROPERTY_HEADER_TITLE));
this.headerTitle.setVisibility(View.VISIBLE);
String titleText = properties.getString(TiC.PROPERTY_HEADER_TITLE);
handleHeaderFooterTitle(context, this.headerTitle, titleText, "header");

} else if (properties.containsKeyAndNotNull(TiC.PROPERTY_HEADER_VIEW)) {

// Handle header view.
final TiViewProxy headerProxy = (TiViewProxy) properties.get(TiC.PROPERTY_HEADER_VIEW);
if ((context instanceof Activity) && (headerProxy.getActivity() != context)) {
headerProxy.releaseViews();
headerProxy.setActivity((Activity) context);
}

final TiUIView view = headerProxy.getOrCreateView();
if (view != null) {
final View headerView = view.getOuterView();
if (headerView != null) {
final ViewGroup parent = (ViewGroup) headerView.getParent();
if (parent != null) {
parent.removeView(headerView);
}

// Amend maximum size for header to parent ListView measured height.
this.header.setChildFillHeight(nativeListView.getMeasuredHeight());

this.header.addView(headerView, view.getLayoutParams());
this.header.setVisibility(View.VISIBLE);
}
}
handleHeaderFooterView(context, nativeListView, this.header, headerProxy);
}
}

if (updateFooter) {
if (properties.containsKeyAndNotNull(TiC.PROPERTY_FOOTER_TITLE)) {

// Handle footer title.
this.footerTitle.setText(properties.getString(TiC.PROPERTY_FOOTER_TITLE));
this.footerTitle.setVisibility(View.VISIBLE);
String titleText = properties.getString(TiC.PROPERTY_FOOTER_TITLE);
handleHeaderFooterTitle(context, this.footerTitle, titleText, "footer");

} else if (properties.containsKeyAndNotNull(TiC.PROPERTY_FOOTER_VIEW)) {

// Handle footer view.
final TiViewProxy footerProxy = (TiViewProxy) properties.get(TiC.PROPERTY_FOOTER_VIEW);
if ((context instanceof Activity) && (footerProxy.getActivity() != context)) {
footerProxy.releaseViews();
footerProxy.setActivity((Activity) context);
}

final TiUIView view = footerProxy.getOrCreateView();
if (view != null) {
final View footerView = view.getOuterView();
if (footerView != null) {
final ViewGroup parent = (ViewGroup) footerView.getParent();
if (parent != null) {
parent.removeView(footerView);
}

// Amend maximum size for footer to parent ListView measured height.
this.footer.setChildFillHeight(nativeListView.getMeasuredHeight());

this.footer.addView(footerView, view.getLayoutParams());
this.footer.setVisibility(View.VISIBLE);
}
}
handleHeaderFooterView(context, nativeListView, this.footer, footerProxy);
}
}
}
Expand Down Expand Up @@ -494,4 +443,48 @@ private void setTitleAttributes(final String prefix, final Context context, fina
title.setBackgroundColor(COLOR_GRAY);
}
}

private void handleHeaderFooterTitle(Context context, TextView textView, CharSequence text, String themePrefix)
{
// Set attributes.
setTitleAttributes(themePrefix, context, textView);

// Handle title.
textView.setText(text);
textView.setVisibility(View.VISIBLE);

// Reset layout params to trigger layout update.
this.container.setLayoutParams(new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.MATCH_PARENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
));
}

private void handleHeaderFooterView(
Context context,
View nativeListView,
TiCompositeLayout viewContainer,
TiViewProxy headerOrFooterViewProxy)
{
if ((context instanceof Activity) && (headerOrFooterViewProxy.getActivity() != context)) {
headerOrFooterViewProxy.releaseViews();
headerOrFooterViewProxy.setActivity((Activity) context);
}

final TiUIView view = headerOrFooterViewProxy.getOrCreateView();
if (view != null) {
final View headerOrFooterView = view.getOuterView();
if (headerOrFooterView != null) {
final ViewGroup parent = (ViewGroup) headerOrFooterView.getParent();
if (parent != null) {
parent.removeView(headerOrFooterView);
}

// Amend maximum size for header to parent ListView measured height.
viewContainer.setChildFillHeight(nativeListView.getMeasuredHeight());
viewContainer.addView(headerOrFooterView, view.getLayoutParams());
viewContainer.setVisibility(View.VISIBLE);
}
}
}
}

0 comments on commit 098f8c2

Please sign in to comment.