I'm creating a custom ListFragment
for my Android application. I want to add a child to @id/listContainer
. But I'm not able to get the reference to the view using findViewById(android.R.id.listContainer)
.
This is the default layout for ListFragment
.
list_content.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:id="@+id/progressContainer"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:gravity="center">
<ProgressBar style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/loading"
android:paddingTop="4dip"
android:singleLine="true" />
</LinearLayout>
<FrameLayout android:id="@+id/listContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false" />
<TextView android:id="@+android:id/internalEmpty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
</FrameLayout>
</FrameLayout>
This is my custom ListFragment
code.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(android.R.layout.list_content, container, false);
ProgressBar progressBar = new ProgressBar(getActivity());
progressBar.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER));
progressBar.setIndeterminate(true);
ViewGroup listContainer = (ViewGroup) view.findViewById(android.R.id.listContainer);
listContainer.addView(progressBar);
return view;
}
Android Studio says error: cannot find symbol variable listContainer
.
What I am doing wrong?
Try getListView().getParent()
. since getListView() gives you the list view widget,it's parent should be the frame layout that you need.
The problem is the way you are accessing resource:
ViewGroup listContainer = (ViewGroup) view.findViewById(android.R.id.listContainer);
listContainer.addView(progressBar);
change it to
ViewGroup listContainer = (ViewGroup) view.findViewById(com.android.internal.R.id.listContainer);
listContainer.addView(progressBar);
Since the listContainer
is an internal resource, call it using
com.android.internal.R.id.RESOURCE_ID
Change this line :
ViewGroup listContainer =
(ViewGroup)view.findViewById(android.R.id.listContainer);
To
ViewGroup listContainer =
(ViewGroup)view.findViewById(R.id.listContainer);
Change View view = inflater.inflate(android.R.layout.list_content, container, false);
To
View view = inflater.inflate(R.layout.list_content, container, false);
Here R
must be import import com.your_package_name.R;
Now you can change
ViewGroup listContainer = (ViewGroup) view.findViewById(android.R.id.listContainer);
To
ViewGroup listContainer = (ViewGroup) view.findViewById(R.id.listContainer);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.