Get the identifier of the resource from which it was overstated

Is there a way to get the resource identifier (e.g. R.layout.viewtoInflate) of the layout from which the view was inflated?

I am trying to implement a list view that uses 2 custom layouts for list items. Which layout of the element is used based on the field in the objects used to populate the list.

What I am missing in my user adapter is a way to find out from which resource the envelope I get in getView () was received. If I could get this information, I could compare and determine if I can reuse the conversion view as it is, or if I have to replace it with the appropriate layout for the current element.

Ideally, it would be something like this:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    JSONObject currItem = mItems.getJSONObject(position);

    int layoutType;

    if (currItem.getBoolean("alternate"))
        layoutType = R.layout.list_item_b;
    else
        layoutType = R.layout.list_item_a;

    if (convertView == null || <convertView.resourceID> != layoutType ) {
        convertView = inflater.inflate(layoutType, null);

        //Finish setting up new View and its holder
    }
    else {
        //Get view holder for view reuse
    }

    //populate view with the required content

    return convertView;
}

, , .

+5
4

, getViewTypeCount() getItemViewType(). , , convertView ( .) . .

, , . , findViewById() , , , null .

+5

- , getViewTypeCount(), getItemViewType().

, , setTag() getTag() - layoutId - .

+3

What I am doing is storing the resource id of the bloated view in the view holder when creating the view. This allows me to get the layout resource id when I fill out the views.

Hope this helps.

+1
source

If you are requesting identification of elements from the layout that you have fanned. This happens as follows: -

//Suppose you have an TextView in the inflated layout of id textView1. Here you go

convertView = inflater.inflate(layoutType, null);
TextView tv = (TextView)convertview.findViewById(R.id.textView1);
-1
source

All Articles