I have a ListView with an associated ArrayAdapter that displays its contents in several activities. Unfortunately it got necessary now, that my ListView in one of the settings does not display all its elements, but just the ones where "property" is not set to true. I would like to avoid to use two ArrayAdapters with different content, since then I somehow need to keep them in sync. I tried it like this (this method now just assumes that getView is called in the setting where we want to hide certain elements):
public View getView(int position, View convertView, Context context) {
....
if(!arrayitems[position].isProperty()) { //arrayitems is the underlying array
convertView.setVisibility(View.VISIBLE);
}
else {
convertView.setVisibility(View.GONE);
}
return convertView;
}
This works, however I get a white row if the element has isProperty == true. I would like to make the row invisible in the sense, that it does not take up any space anymore. Is that possible?
The used xml file for convertView looks like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/text_item_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="4dp"
android:maxLines="1"
android:ellipsize="none"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="#+id/text_item_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
<TextView android:id="#+id/text_item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:maxLines="3"
android:ellipsize="none" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/text_item_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="4dp"
android:textSize="10sp" />
<TextView
android:id="#+id/text_item_voted"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
I tried to replay all android:layout_height="wrap_content" with "fill_parent" but it did not change anything...
Thanks!
Set the Visibility of all the contents of the list view to GONE and then set the visibility of the View to Gone.... this will hide the row without occupying space..... it worked for me even i have been searching for this but after a research i found this....
EDIT 1 :
Setting the visibility of the View to GONE is enough. No need to set the child element's visibility.
I'm having same problem as you've
I've figured it out by inflating another layout layout with no height and width
Adapter.java
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (YOUR_CONDITION) {
return inflater.inflate(R.layout.blank_layout, parent,
false);
} else {
View vi = inflater.inflate(R.layout.list_item, parent, false);
return vi;
}
}
And blank layout will be like this,
blank_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical" >
</LinearLayout>
So what Romain meant was to add an id to your LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:orientation="vertical"
android:id="#+id/linear_layout">
Then in your Java you would do
public View getView(int position, View convertView, Context context) {
....
holder.lLayout = (LinearLayout) convertView.findViewById(R.id.linear_layout);
....
if(!arrayitems[position].isProperty()) { //arrayitems is the underlying array
holder.lLayout.setVisibility(View.VISIBLE);
}
else {
holder.lLayout.setVisibility(View.GONE);
}
return convertView;
}
You may need to change it a bit if your inflating your views differently. I've done this myself and it works really well.
You can use the getview layout of android:layout_height="wrap_content" and do use Visibility gone to hide your layout
Another user had the same problem and he solved it this way: https://stackoverflow.com/a/7928923/1548679 .
I hope this could help you.
Greets
By modifying getCount() and also the position in getView() with your logic you can make it work for sample Check http://www.sherif.mobi/2012/01/listview-with-ability-to-hide-rows.html by #sherif-elkhatib
Related
I have this problem in my detail view in my Master/Detail Template based app.
im populating expandable list in detail view. it is working fine in tablet view. but in mobile device it is only showing me first result only. i even tried removing CollapsingToolbarLayout to make sure listview not getting block by any other scroll options. but i couldn't find the problem.
Setting up adapter
ArrayList<ClientList> clientLists = ClientList.getClientList(getActivity(), clientResult);
Log.e("Client","--"+clientLists.size()); //Im getting the list size as normal
clientExpandableAdapter = new ClientExpandableAdapter(getActivity(), clientLists);
expandableListView.setAdapter(clientExpandableAdapter);
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
return false;
}
});
Detail view layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/item_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xxx.xxx.ItemDetailFragment"
android:textIsSelectable="true"
android:paddingBottom="16dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:background="#drawable/bottom_border"
android:id="#+id/mainViewHeader">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:id="#+id/titleHeader"
android:text="Clients" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:id="#+id/titleCountHeader"
android:text="Clients"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/mainViewHeader"
android:id="#+id/backgroundView"
android:background="#color/colorWhite">
<ExpandableListView
android:id="#+id/mainExpandableDatalist"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ExpandableListView>
</RelativeLayout>
</RelativeLayout>
In this type of issues many mistakes are possible according to me.
You might have taken ScrollView as parent of listview which is not at all required.
Height of ListView or custom ListView layout's root element could be match_parent | fill_parent that shows single item in whole screen.
It will be better if you show layout files too.
This is the view when i'm scrolling it.. it added extra layout that expand some space within my listview item
What listview properties should i assign to fix it?
Here is my List View
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:scrollbarStyle="outsideOverlay"
android:overScrollMode="never"
android:saveEnabled="false" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/relativeHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:cacheColorHint="#android:color/transparent" >
<LinearLayout
android:id="#+id/linearNumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="10dp"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<TextView
android:id="#+id/textViewNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|left"
android:text="Number: "
android:textStyle="bold"
android:textColor="#000000" />
<TextView
android:id="#+id/textViewNumberValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|left"
android:text="222-222-222-222"
android:textColor="#000000" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
My Initialization of ListView
private void setupListViewAdapter(ListView listView, View headerView, View footerView){
listView.setDividerHeight(5);
listView.setFadingEdgeLength(200);
listView.addHeaderView(headerView, null, false);
listView.addFooterView(footerView, null, false);
listView.setFastScrollEnabled(false);
listView.setAdapter(mDataAdapter);
}
This code made my listview display unwanted view.
private void initListView(LayoutInflater inflater, ViewGroup container) {
View headerViewROR = inflater.inflate(R.layout.vp_ror_header, null,false);
View footerViewROR = inflater.inflate(R.layout.vp_remarks_footer, null,false);
listView.addHeaderView(headerViewROR, null, false); //--> multiple occurrence need to be removed
listView.addFooterView(footerViewROR, null, false); //--> multiple occurrence need to be removed
setupListViewAdapter(listView, headerViewROR,footerViewROR);
}
Initialize adding header and footer twice that causes the listview behavior.. because i also called the listView.addFooterView to my setupListViewAdapter function.
private void setupListViewAdapter(ListView listView, View headerView, View footerView){
listView.setDividerHeight(5);
listView.setFadingEdgeLength(200);
listView.addHeaderView(headerView, null, false);//--> here is another assignment
listView.addFooterView(footerView, null, false);//--> here is another assignment
listView.setScrollingCacheEnabled(false);
listView.requestFocus(0);
listView.setAdapter(mDataAdapter);
}
First thing yu can have more than one header and footer in list view .Every time you call addHeader() it will add the view in list header . In one case when your list items are empty . Then you need to setAdapter(null) .Then only you will be able to see views you added in header and footer ..
I want to make XML template to reuse the code. The problem is that one of the template has several linearlayouts. However, when I run the app, it only shows one linearLayout. (I can only see id: manage_services. not manage_view neither manage_shows).
Here is my code. I hope anyone can figure out the problem.
drawable_manage.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/manage_services"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:drawableLeft="#drawable/menu_settings"
android:text="#string/title_manage_services"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout>
<View
android:id="#+id/manage_view"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#color/menu_divider" />
<LinearLayout
android:id="#+id/manage_shows"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="#dimen/slide_menu_header" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:drawableLeft="#drawable/menu_settings"
android:text="#string/title_manage_shows"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
MainMenuActivity.java
private void addPanels(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
android.support.v4.widget.DrawerLayout parent = (android.support.v4.widget.DrawerLayout) inflater.inflate(R.layout.activity_main_menu,null);
View layout_manage = inflater.inflate(R.layout.drawable_layout_manage, null);
View layout_names = inflater.inflate(R.layout.drawable_layout_names, null);
View layout_emails = inflater.inflate(R.layout.drawable_layout_email, null);
LinearLayout leftLinearLayout = (LinearLayout)parent.findViewById(R.id.left_drawer);
leftLinearLayout.addView(layout_names);
leftLinearLayout.addView(layout_emails);
leftLinearLayout.addView(layout_manage);
setContentView(parent);
}
Change your parent LinearLayout's orientation from horizontal to vertical.
android:orientation="vertical"
Are you wanting to reuse your drawable_manage.xml linearlayouts in these (i.e. #+id/manage_services, #+id/manage_shows) or do you have separate layout files?
View layout_manage = inflater.inflate(R.layout.drawable_layout_manage, null);
View layout_names = inflater.inflate(R.layout.drawable_layout_names, null);
View layout_emails = inflater.inflate(R.layout.drawable_layout_email, null);
LinearLayout leftLinearLayout = (LinearLayout)parent.findViewById(R.id.left_drawer);
Try as others suggested and change the orientation, and make sure you are trying to inflate the correct layout. Make sure you are inflating drawable_manage in the correct location and calling to the child linearlayouts by the correct ids.
I am working on android project where I have an activity which is using the theme holo dialog.
Within the dialog I have a list view and what I want to have is at the bottom always on show a linear layout which contains two buttons next to each other. The list view is populated by retrieving data from the call log and populating the adapter of the list.
The problem I am having is the list view always shows on top taking up the whole dialogue with the buttons at the bottom, with the content of the list overlapping the buttons. I want the list to be within the space of the top of the dialogue to the top of the linear layout button group. Below is the code for the main content view. This layout is set using the setContentView in the onCreate method
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
<LinearLayout android:id="#+id/call_log_select_host_button_group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<Button android:id="#+id/call_log_select_btnCancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Block"/>
<Button android:id="#+id/call_log_select_btnBlock"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
</LinearLayout>
</RelativeLayout>
I'm not sure if it makes any difference but just in case the below is how I am populating the list and setting the view.
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
CallInformation callInformation = (CallInformation)arrayList.get(position);
LayoutInflater inflator = (LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflator.inflate(R.layout.call_log_selection, parent, false);
ImageView imageView = (ImageView)rowView.findViewById(R.id.call_log_selector_image);
if (callInformation.type == android.provider.CallLog.Calls.INCOMING_TYPE)
{
imageView.setImageResource(android.R.drawable.sym_call_incoming);
}
else if (callInformation.type == android.provider.CallLog.Calls.OUTGOING_TYPE)
{
imageView.setImageResource(android.R.drawable.sym_call_outgoing);
}
else if (callInformation.type == android.provider.CallLog.Calls.MISSED_TYPE)
{
imageView.setImageResource(android.R.drawable.sym_call_missed);
}
CheckBox checkbox = (CheckBox)rowView.findViewById(R.id.call_log_selector_checkbox);
checkbox.setText(callInformation.content);
checkbox.setTag(callInformation.telephone);
return rowView;
}
}
The below code is the XML layout that is inflated by the adapter get view function
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView android:id="#+id/call_log_selector_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:layout_gravity="center"
android:src="#android:drawable/stat_sys_phone_call"/>
<CheckBox android:id="#+id/call_log_selector_checkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
Thanks for any help you can provide.
Try telling your ListView to stay above your LinearLayout
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/call_log_select_host_button_group> // Add this line here
</ListView>
I have implemented a ListView in my application using a custom implementation of CursorAdapter. My problem is, that whenever I fling to scroll quickly to the bottom of the list (right after launching the application), I sometimes end up with all the ListView items drawn overlapping each other. If I scroll back up or touch one of the items, they become properly arranged.
Here is how it looks after I quickly scroll down :
http://i.stack.imgur.com/cTcfD.png
Here is how it looks when I am select-ing one of the items :
http://i.stack.imgur.com/ZTRSt.png
Here is the XML for my ListView :
<ListView
android:id="#+id/all_reminders_list"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_alignParentLeft="true"
android:clickable="true"
android:dividerHeight="1.0sp"
android:animateLayoutChanges="true">
Here's the newView(..) method of my custom CursorAdapter :
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.view_list_item, parent, false);
return view;
}
And this is the bindView(..) method :
public void bindView(View view, Context context, Cursor cursor) {
TextView whatTextView = (TextView) view.findViewById(R.id.item_what_text);
whatTextView.setText(cursor.getString(1));
TextView whenTextView = (TextView) view.findViewById(R.id.item_when_text);
if(cursor.getInt(9) != 0) // DONE_FLAG = 1 (completed)
{
//Arrow visibility
ImageView arrow = (ImageView)view.findViewById(R.id.list_item_arrow);
arrow.setVisibility(View.INVISIBLE);
//Text color
whatTextView.setTextColor(Color.LTGRAY);
whenTextView.setTextColor(Color.LTGRAY);
//WHEN text
whenTextView.setText(TimeCalculationHelper.getCompletedTimeString(cursor.getLong(2)));
}
else // DONE_FLAG = 0
{
//Arrow visibility
ImageView arrow = (ImageView)view.findViewById(R.id.list_item_arrow);
arrow.setVisibility(View.VISIBLE);
//Text color
whatTextView.setTextColor(Color.BLACK);
whenTextView.setTextColor(Color.BLACK);
//WHEN text
whenTextView.setText(TimeCalculationHelper.getTimeRemainingString(cursor.getLong(2)));
}
}
I've also noticed that I have been able to replicate it only when my device (Galaxy S2) is in power saving mode. Is there something I should be doing differently here? Thanks in advance!
EDIT : Including the list item's 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="wrap_content"
android:orientation="horizontal"
android:paddingLeft="2dp">
<TextView android:id="#+id/item_what_text"
android:lines="1"
android:maxLines="2"
android:textSize="22dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="3dp"/>
<TextView android:id="#+id/item_when_text"
android:lines="1"
android:maxLines="1"
android:textSize="14dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="13 minutes"
android:paddingBottom="2dp"
android:layout_below="#+id/item_what_text"/>
<ImageView
android:id="#+id/list_item_arrow"
android:src="#drawable/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
I also faced the same problem, with List items in my ListView overlapping each other whilst scrolling.
My fix:
I just specified a background for the parent layout that contained the ListView in question. Previously it was transparent.
I had the same problem and found it was caused by setting the animateLayoutChanges attribute to true on the listview.
Unfortunately I lose the animation of the listview by removing it but at least it draws properly when scrolling fast.
Giving the parent layout a background also appears to fix the issue, as mentioned by Pavan. Will experiment and change my answer if I discover issues with the background change.
For solving this ,i have one more solution:
I faced problem by using this layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
android:paddingTop="4dip"
android:dividerHeight="0dp"
android:divider="#null"
android:cacheColorHint="#00000000"
android:listSelector="#color/transprent_editbox"
android:focusableInTouchMode="false" />
</RelativeLayout>
In order to solve that Problem,i just did few modifications on Above layout,those are:
1)Changed top layout from Relative to Linear Layout.
2)Put my ListView in other Relative Layout ..
i did like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
android:paddingTop="4dip"
android:dividerHeight="0dp"
android:divider="#null"
android:cacheColorHint="#00000000"
android:listSelector="#color/transprent_editbox"
android:focusableInTouchMode="false" />
</RelativeLayout>
</LinearLayout>
After using this,my problem is solved.
1.
in my opinion, height is wrap_content
because listview is restore row
2.
other opinion is write this code android:cacheColorHint="#00000000" in ListView
I also faced the same problem and found that animateLayoutChanges attribute causing the problems.
So i changed
android:animateLayoutChanges="true"
to
android:animateLayoutChanges="false"
and it worked.