According to this link: ListFragment android developers
I want to set my custom layout for list, but it makes exceptions.
Here is the code:
p
Just replace the original ListView with your CustomListView Layout within the onCreateView method. Worked for me.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = super.onCreateView(inflater, container,
savedInstanceState);
ListView lv = (ListView) layout.findViewById(android.R.id.list);
ViewGroup parent = (ViewGroup) lv.getParent();
// Remove ListView and add CustomView in its place
int lvIndex = parent.indexOfChild(lv);
parent.removeViewAt(lvIndex);
LinearLayout mLinearLayout = (LinearLayout) inflater.inflate(
R.layout.custom_list, container, false);
parent.addView(mLinearLayout, lvIndex, lv.getLayoutParams());
return layout;
}
A little bit late, but did not see this answer.
Inside Fragment
:
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
final Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_list_activity,
container, false);
ListFragmentHelper.adaptView(view);
return view;
}
but bear in mind that in fragment_list_activity.xml
you need to set the IDs android:id="@id/loading"
, android:id="@id/content"
and android:id="@id/empty"
for your views!
This code worked for me:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = super.onCreateView(inflater, container, savedInstanceState);
((TextView)root.findViewById(0x00ff0001)).setText("some text");
ListView list = (ListView) root.findViewById(android.R.id.list);
list.setEmptyView(root.findViewById(0x00ff0001));
return root;
}
I just needed to change my empty text TextView
properties and did this
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
// Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return)
mAdapter.swapCursor(cursor);
if (cursor.getCount() == 0) {
setEmptyText(getString(R.string.no_users));
TextView tvEmpty = (TextView) getListView().getEmptyView();
tvEmpty.setTextColor(getResources().getColor(R.color.blue));
}
// The list should now be shown.
if (isResumed()) {
setListShown(true);
} else {
setListShownNoAnimation(true);
}
}
Do not forget that if you try initializing tvEmpty
before you call setEmptyText("Empty")
, you'll run into a NullPointerException
This might be easier: Create your layout as you would normally but don't add the list/empty/loading stuff. Then in your listfragment's onCreateView:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = super.onCreateView(inflater, container, savedInstanceState);
ViewGroup parent = (ViewGroup) inflater.inflate(R.layout.mylayout, container, false);
parent.addView(v, 0);
return parent;
}
Now you can use setListShown...
Today I had the same issue. Looking the solution in Google, I found a post in a japanese blog. (http://blog.nkzn.net/entry/2012/06/14/160706).
For use setListShown(boolean)
you must do first in your layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
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" >
</ListView>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:visibility="gone" >
<ProgressBar
android:id="@android:id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
Now in your ListFragment:
public class SampleListFragment extends ListFragment {
/** ID for progressbar parent */
private static final int INTERNAL_PROGRESS_CONTAINER_ID = 0x00ff0002;
/** ID for listview parent */
private static final int INTERNAL_LIST_CONTAINER_ID = 0x00ff0003;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.custom_list_layout, container, false);
// get you progressBar and his parent
ProgressBar pBar = (ProgressBar) view.findViewById(android.R.id.progress);
LinearLayout pframe = (LinearLayout) pBar.getParent();
pframe.setId(INTERNAL_PROGRESS_CONTAINER_ID);
ListView listView = (ListView) view.findViewById(android.R.id.list);
listView.setItemsCanFocus(false);
FrameLayout lFrame = (FrameLayout) listView.getParent();
lFrame.setId(INTERNAL_LIST_CONTAINER_ID);
return view;
}
}
with this 2 ids numbers seted in his parents setListShown(boolean) will work without problems.