Skip to content

Commit 20f8079

Browse files
committed
Find index of ListView by calling indexOfChild() instead of iterating
through the views
1 parent cdae6e3 commit 20f8079

1 file changed

Lines changed: 6 additions & 15 deletions

File tree

extras/PullToRefreshListFragment/src/com/handmark/pulltorefresh/extras/listfragment/PullToRefreshListFragment.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.os.Bundle;
44
import android.support.v4.app.ListFragment;
5+
import android.util.Log;
56
import android.view.LayoutInflater;
67
import android.view.View;
78
import android.view.ViewGroup;
@@ -34,21 +35,11 @@ public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bun
3435
ListView lv = (ListView) layout.findViewById(android.R.id.list);
3536
ViewGroup parent = (ViewGroup) lv.getParent();
3637

37-
// Iterate through parent's children until we find the ListView, we need
38-
// to do it this way as we need to find out the child index
39-
for (int i = 0, z = parent.getChildCount(); i < z; i++) {
40-
View child = parent.getChildAt(i);
41-
42-
if (child == lv) {
43-
// Remove the ListView first
44-
parent.removeViewAt(i);
45-
46-
// Now create ListView, and add it in it's place...
47-
mPullToRefreshListView = onCreatePullToRefreshListView(inflater, savedInstanceState);
48-
parent.addView(mPullToRefreshListView, i, lv.getLayoutParams());
49-
break;
50-
}
51-
}
38+
//Remove ListView and add PullToRefreshListView in its place
39+
int lvIndex = parent.indexOfChild(lv);
40+
parent.removeViewAt(lvIndex);
41+
mPullToRefreshListView = onCreatePullToRefreshListView(inflater, savedInstanceState);
42+
parent.addView(mPullToRefreshListView, lvIndex, lv.getLayoutParams());
5243

5344
return layout;
5445
}

0 commit comments

Comments
 (0)