I'm reusing the ListView AND the LayoutInflater here for both a header and a footer:
ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
View header = (View)inflater.inflate(R.layout.header, lv, false);
lv.addHeaderView(header, null, false);
View footer = (View)inflater.inflate(R.layout.footer, lv, false);
lv.addFooterView(footer, null, false);
I reckon reusing the ListView is perfectly sensible, but I'm not so sure about the LayoutInflater. Am I flirting with disaster here, or is this okay?
Please refer to : http://developer.android.com/reference/android/widget/ListView.html#getFooterViewsCount()
Returns the number of footer views in the list. Footer views are
special views at the bottom of the list that should not be recycled
during a layout.
So yeah... I don't think you are doing anything wrong.
Related
At some point in my app in need to get views (actually, it should be one) from a layout.
What I know about the layout is it's int reference, in the form of R.layout.my_layout.
How can I get the views inside?
I tried getting the root view with
View rootView = (View) getResources().getLayout(R.layout.my_layout)
but obviously it didn't work. getLayout() returns a XMLResourceParser object, which I don't know how to manage.
I can't work with IDs.
Use layout inflater
View rootView = View.inflate(context, R.layout.my_layout, null);
To load the view:
LayoutInflater inflater = LayoutInflater.from(getContext());
View v = inflater.inflate(R.layout.your_layout, null);
And to get sub views from it:
View sub = v.findViewById(R.id.your_sub_view_id);
Use this:
ViewGroup rootView = (ViewGroup) getLayoutInflater().inflate(R.layout.my_layout,null);
and get access to the childviews with
rootView.getChildAt(x);
or total amount of childs:
rootView.getChildCount();
If you have the layout resource ID, you need to inflate the layout and get the Views from that:
View rootView = getLayoutInflater().inflate(layoutId, null);
Once you get the view, you just need to use -
view.findViewById(R.id.childID)
where,
childID is the child id of the parent view's child.
I have a listview with an adapter. I want to add a footer and that this footer will be displayed when scrolling the listview. Right now with this code the footerview is displayed when the listview is finished. In the onCreate method of the list i have:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listado_imagenes_subidas_main);
// load list application
listImagenes = (ListView)findViewById(R.id.lvImages);
LayoutInflater inflater = getLayoutInflater();
ViewGroup footer = (ViewGroup) inflater.inflate(R.layout.footer3buttons, listImagenes,
false);
listImagenes.addFooterView(footer, null, false);
// create new adapter
ImagenesAdapter adapter = new ImagenesAdapter(this, ListadoImagenes());
// set adapter to list view
listImagenes.setAdapter(adapter);
}
Could you please help me with this issue?
Thank you in advanced.
If i understood correctly what you wan't is a "sticky" footer the addFooterView method add a footer after the last view item, for a sticky footer you have to roll your own, or use an external library for it.
try with this one
View footerView = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
list.addFooterView(footerView);
I have a problem using the footer of my list view. In fact, I inflate an xml and I send it to the footerView. When my activity appears, my listview is showing and after two or three seconds (sometimes more) my footer is showing. Why is it so long ?
Here is a part of my code :
private View footerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
footerView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.order_footer_item, null);
super.onCreate(savedInstanceState);
}
// This is Android annotation
#AfterViews
protected void setAfterViews() {
Adapter adapter = new Adapter();
list.setAdapter(adapter);
list.setOnItemClickListener(adapter);
TextView text = (TextView) footerView.findViewById(R.id.text);
text.setText("Test");
list.addFooterView(footerView);
}
I have no problem except slowing. Why ? How can I improve that ?
Thx in advance
Add it before setting adapter.
list.addFooterView(footerView);
And then
list.setAdapter(adapter);
This was a tricky problem for me. After spending quite a while working on it I realized that I was giving a parent parameter to the .inflate(view,parent,boolean) method instead of leaving it as null because I handle it with the getListView().addFooterView() method.
Error:
TextView footerView = (TextView) ((LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_view, this.getListView(), true);
this.getListView().addFooterView(footerView);
Fix:
TextView footerView = (TextView) ((LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_view, null, false);
this.getListView().addFooterView(footerView);
First, you should probably create the footer view in the onCreateView, where you get a LayoutInflater inflate and a ViewGroup container.
Then, you'd call
inflater.inflate(R.layout.footer_view, container, false);
And the rest as per vipul mittal solution.
Can anyone help me to find out how to add an ImageView as a header to a ListView?
My code is here:
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View snapshot = inflater.inflate(R.layout.map_snapshot, null);
ListView channelsList = (ListView) findViewById(R.id.channelsList);
channelsList.addHeaderView(snapshot);
By now it shows nothing.
Try inflating your header view passing the parent (ListView) as parameter:
ListView channelsList = (ListView) findViewById(R.id.channelsList);
View snapshot = inflater.inflate(R.layout.map_snapshot, channelList, false);
Then, just add it to the ListView:
channelsList.addHeaderView(snapshot);
Try:
ListView channelsList = (ListView) findViewById(R.id.channelsList);
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.map_snapshot, channelsList , false);
channelsList .addHeaderView(header, null, false);
In layout map_snapshot,you must be having parent layout as here i have taken Linear Layout likewise take that.
ListView channelsList = (ListView) findViewById(R.id.channelsList);
LinearLayout headerView = (LinearLayout) getLayoutInflater().inflate(R.layout.map_snapshot, null);
listView.addHeaderView(headerView);
if you want to add views in layout:
TextView mUsername=(TextView) headerView.findViewById(R.id.user_name);
You can use CommonsWare MergeAdapter or SectionedListAdapter. With this library you can use any View as Header in your Listview. See this SO question and accepted answer for more information.
I have a View extending google MapView. And a ListView with items. What I need is to put MyMapView as a header to the list view. Help please what to do
Update: When I try to do this:
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ListView list = (ListView) findViewById(R.id.list);
View m = inflater.inflate(R.layout.map, list, false);
list.addHeaderView(m);
I got an error:
MapViews can only be created inside an instances of MapActivity
I think it's because I have removed my xml layout for map into different.xml from my activity
Try this
Create your ListViewHeader.xml first
and do this
ListView lv = getListView(); // Your listView
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.YourHeader, lv, false);
lv.addHeaderView(header, null, false);
take a look on Android: Adding static header to the top of a ListActivity
Try to call
ListView.addHeaderView (View v)
before you set adapter to this ListView
if you are using a custom adapter, in getView() method provide different view at position 0.
if(position==0)
{
//Provide header view
}
else
{
//provide normal row
}
You can do the following to add a view as ListView header in android.
ListView list = (ListView) findViewById(R.id.list);
LayoutInflater inflater = getLayoutInflater();
ViewGroup viewGroup= (ViewGroup)inflater.inflate(R.layout.YourLayoutFile,list, false);
list.addHeaderView(viewGroup, null, false);