Setting up Hansel Index for dynamic views

Dynamic Views

A UI element is said to be dynamic when it can change its index within its parent ViewGroup.


For example, due to personalization, recommendation1 can be placed at index 2 for a User A whereas the same recommendation1 can be placed at index 5 for another User B. Then the UI element containing recommendation1 data is dynamic in nature.

📘

Prerequisite: Support for Dynamic views via below method is available from Android Nudge SDK v8.8.1 onwards

To ensure that a nudge doesn't lose its context in such a scenario and is always placed on the correct view, provide a custom identifier to the UI elements using the following method:

Hansel::setCustomHanselIndex(@NonNull View view, @NonNull String hanselIndex)
Hansel::setCustomHanselIndex(view: View!, hanselIndex: String!)

Where,
view: UI element that is dynamic in nature
hanselIndex: an identifier for the dynamic UI element

❗️

[Caution] Below approach for setting up Hansel Index is deprecated from Android Nudge SDK v8.8.1 above.

If your app has dynamic content populated in the ViewPager fragments or RecyclerView and you want to setup nudges on such views, then follow the below steps to set the Hansel Index.

Method [Deprecated from SDK v8.8.1]
Hansel.setHanselIndex(view, < hansel index >);

When to use
If your anchor view is part of the RecyclerView or ViewPager fragment, then implement the Hansel Index.

📘

Important

  • Hansel indices are supported only for views that are immediate children of RecyclerView or ViewAdapter
  • Hansel index should be set only after the view is attached to its parent container.

ViewPager

While using Fragment

Add the following lines of code in onViewCreated method of the fragment.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
  super.onViewCreated(view, savedInstanceState);
  Hansel.setHanselIndex(view, <string_for_hansel_index>);
}

While inflating layout XML

Set the Hansel Index after adding your view to the container.

public class MyCustomAdapter extends PagerAdapter {

  ...
  
    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
    
      ...
        ViewGroup layout = (ViewGroup) LayoutInflater.from(mContext).inflate(R.layout.your_layout_file, container, false);
        container.addView(layout);
        Hansel.setHanselIndex(layout, <string_for_hansel_index>);
      ...

        return layout;
    }
  
  ...
    
}

RecyclerView

Add the following code to your ViewHolder class that enables saving and assigning the Hansel index for your RecyclerView items

public class MyCustomViewHolder extends RecyclerView.ViewHolder {

    private View itemView;
    private String hanselIndex;

    ...

    public MyCustomViewHolder(View itemView) {
        super(itemView);
        this.itemView = itemView;

        ...

    }

    ...

    public void saveHanselIndex(String hanselIndex){
        this.hanselIndex = hanselIndex;
    }

    public void assignHanselIndex(){
        Hansel.setHanselIndex(itemView, hanselIndex);
    }
}

Add the following code to your RecyclerView’s adapter for using the methods added in your ViewHolder to set Hansel index

public class MyCustomAdapter extends RecyclerView.Adapter<MyCustomViewHolder> {

    @Override
    public void onBindViewHolder(MyCustomViewHolder holder, int position) {

        ...

        holder.saveHanselIndex(<string_for_hansel_index>);

        ...

    }

    @Override
    public void onViewAttachedToWindow(MyCustomViewHolder holder) {
        ...
        super.onViewAttachedToWindow(holder);
        holder.assignHanselIndex();
        ...

    }
}

Did this page help you?