How to get Position of an view added dynamically on LinearLayout

前端 未结 4 1097
庸人自扰
庸人自扰 2020-12-12 00:39

I need to get an dynamically added view position in LinearLayout with vertical orientation. For example i have 4 TextViews added dynamically on LinearLayout, then i need to

4条回答
  •  一个人的身影
    2020-12-12 01:30

    You can do it just like that

    ViewGroup parent;
        int position;
    
        for(int i = 0; i < parent.getChildCount(); ++i) {
            int currentViewId = parent.getChildAt(i).getId();
    
            if(currentViewId == wantendViewId) {
                position = i;
            }
        }
    

    That's (in my opinion) the simplest way

提交回复
热议问题