I am trying to implement listview inside scrollview or nestedscroll view but my list dose not visible last item properly .
please help me. how to visible all item properly
here is my code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/electronic_TV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="#string/no_data_find"
android:textSize="40dp"
android:textStyle="bold"
android:visibility="gone" />
<RelativeLayout
android:id="#+id/rltop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:background="#color/white">
<TextView
android:id="#+id/tvadver"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="ADVERTISEMENT"
android:textColor="#color/light_grey"
android:textSize="12dp"
android:textStyle="normal" />
<RelativeLayout
android:id="#+id/rl_viewfliper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tvadver"
android:layout_marginBottom="10dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="10dp">
<ViewFlipper
android:id="#+id/view_flipper"
android:layout_width="fill_parent"
android:layout_height="130dp">
</ViewFlipper>
</RelativeLayout>
</RelativeLayout>
<ListView
android:id="#+id/fashionLV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tvmsg"
android:layout_weight="1"
android:divider="#android:color/transparent"></ListView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
Java code
commanAdapter = new CommanAdapter(getActivity(), commanModelArrayList);
fashionLV.setAdapter(commanAdapter);
GeneralFunction.setListViewHeightBasedOnChildren(fashionLV);
GeneralFunction code:
public static void setListViewHeightBasedOnChildren(ListView listView) {
ArrayAdapter listAdapter = (ArrayAdapter) listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
The use of "ListView" and "ScrollView" objects together is not recommended.
You should use different views in same adapter. For example;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
MyListItem currentItem = myList.get(position);
LayoutInflater Inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (currentItem.getListItemType() == "Description") {
rowView = Inflater.inflate(R.layout.descriptionlayout, null);
TextView tvDesc= (TextView) rowView.findViewById(R.id.tvAccountType);
tvDesc.setText("Desc");
} else if (currentAccountItem.getListItemType() == "ListItem") {
rowView = Inflater.inflate(R.layout.list_item_layout, null);
...
}
}
You should add "ListItemType" property to your list object type. And check this property in getView() method and inflate different layouts.
In your case, you can create new layout for your LinearLayout. And insert an object to your list for this row. And set ListItemType. and check this property and inflate this layout on getView().
Hope it helps.
You add your own footer view in your listview by using list view methods.
It will shows after all the elements of listView.
ListView inside NestedScrollView creates lot of problems and ListView is not recommended to use instead go for RecyclerView, you have to change the Adapter accordingly.
Related
I got a fragment in my tablayout displaying a listview:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/backgroundColor"
android:fitsSystemWindows="true"
tools:context=".activities.fragments.OrdersFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#+id/orderListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#color/colorPrimaryDark"
android:dividerHeight="1px" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
This Listview has items which have the following layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/order_list_order"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<ImageView
android:id="#+id/arrowView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_weight="1"
app:srcCompat="#drawable/ic_downarrow" />
<LinearLayout
android:id="#+id/previewLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/order_list_storename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Store Name"
android:textColor="#color/generalText"
android:textSize="24sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
some textview here
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
some textview here
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/extensionLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/previewLayout"
android:visibility="visible">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Produkte:"
android:textColor="#color/generalText"
android:textSize="18sp"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/exampleArray"/>
</LinearLayout>
</RelativeLayout>
These items all have also a listview which has the exampleArray as items:
<string-array name="exampleArray">
<item>Donut</item>
<item>Pizza</item>
<item>blabla</item>
</string-array>
My Problem is, that only the first element of this inner listview is shown. Also the outer Listview is not clickable anymore. Does anyone know why this is the case? Am I doing anything wrong?
Its not a good idea to put a scrollable view inside another scrollable view. Try to solve your problem using getItemViewType method.
If you don't have any other option, then you have to measure inner ListView height. This way all item will be inflated at the same time, you can't take advantage of recycle property of ListView.
To measure inner ListView use this method
/**** Method for Setting the Height of the ListView dynamically.
**** Hack to fix the issue of not showing all the items of the ListView
**** when placed inside a ScrollView ****/
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
Usage:
setListViewHeightBasedOnChildren(listView)
This way you inner LisView will lose recycle property.
There is other questions about this, You can check those too,
Check this and this
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">//changed here
<ListView
android:id="#+id/orderListView"
android:layout_width="match_parent"
android:layout_height="match_parent"//and here
android:divider="#color/colorPrimaryDark"
android:dividerHeight="1px" />
</RelativeLayout>
Please use 'match_parent' with 'ListView' else use RecyclerView
Hi i have try insert listview in scrollview but i have this problem:
the space that scrollview reserve to listview is little, i want that scrollview is than the screen , i want scrollview is visible only when listview becomes larger than the screen How i do?
this is code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:id="#+id/activity_lista__eventi"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="14dp"
android:paddingRight="14dp"
android:paddingTop="#dimen/activity_vertical_margin"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:context="com.example.fra87.eudroid.activity_class.Lista_Eventi">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical">
<SearchView
android:layout_width="match_parent"
android:layout_height="50dp"
android:queryHint="Evento"
android:id="#+id/cercaEvento"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:id="#+id/linearLayoutEventi">
<ListView
android:id="#+id/listViewEventi"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/cercaEvento"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
How i do this?
More solutions can be found here: Android list view inside a scroll view
This is a common issue that a lot of developer face. The issue is you are stacking a scrollable view inside another scrollable view. One solution to remove the listview from the scrollview.
Another is the following code:
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
I have the following layout defined in my app:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/disco_photo"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/disco_name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="#id/disco_photo"
android:layout_alignParentLeft="true"
android:layout_gravity="left"
android:layout_margin="8dp"
android:gravity="end"
android:textSize="20sp"
android:textStyle="italic" />
<TextView
android:id="#+id/disco_times"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="#id/disco_name"
android:layout_alignParentLeft="true"
android:layout_gravity="left"
android:layout_margin="8dp"
android:gravity="end"
android:textSize="20sp"
android:textStyle="italic"
android:visibility="gone" />
<ListView
android:id="#+id/disco_parties"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/disco_times"
android:layout_alignParentLeft="true"
android:layout_gravity="left"
android:layout_margin="8dp"
android:text="#string/map_view"
android:gravity="end"
android:textSize="20sp"
android:textStyle="italic" />
<RatingBar
android:id="#+id/disco_rating"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="#id/disco_parties"
android:numStars="5"
android:stepSize="1.0"
android:rating="3.0"
android:visibility="gone" />
<TextView
android:id="#+id/disco_map"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="#id/disco_rating"
android:layout_alignParentLeft="true"
android:layout_gravity="left"
android:layout_margin="8dp"
android:text="#string/map_view"
android:gravity="end"
android:textSize="20sp"
android:textStyle="italic" />
</RelativeLayout>
</ScrollView>
I want to get scroll in the whole layout but the problems comes when only ListView is getting the scrollbar, so you can only scroll this list. The rest of the layour is fixed to the screen height.
How can I make the whole layout be scrollable? Thanks in advance.
I am going through your problem. You can not use list view inside the scroll-view directly. If you want to use it, then you have to give height pragmatically to list view and it work for me.
public class test1 extends Activity {
...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test1);
...
//here 1st set your list view than call set_List_View_Height_Based_On_Children...
set_List_View_Height_Based_On_Children(your_Listview_name);
...
}
///method set_List_View_Height_Based_On_Children
public static void set_List_View_Height_Based_On_Children(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
}
ListView can't be placed inside a ScrollView unless you define that your ListView isn't scrollable. If you wanna do this you should create a custom ListView like the following
public class NonScrollListView extends ListViewCompat {
//Define a public constructor
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = View.MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, View.MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}
You should not use a ListView in a ScroolView, it has it's own scroll and they interfere with each-other.
I would recommend creating a list adapter that creates all of your different views from an array using a switch statement:
class MyAdapter extends ArrayAdapter{
public MyAdapter(Context context, int resource, List objects) {
super(context, resource, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewItem viewType = getItem(position);
switch(viewType.type){
case TEXTVIEW: convertView = LayoutInflater.from(getContext()).inflate(R.layout.textView1, parent, false);
break;
case LISTITEM: convertView = LayoutInflater.from(getContext()).inflate(R.layout.listItem, parent, false);
break;
}
return convertView;
}
}
I m create a To Do List. Now i want to show the To Do lists in a RecylerView that contains Cards and the cards displaying a textview (title) and a Listview.
The content from Listview should be the Tasks from the TodoList.
I want to get a fix Listview without scrolling, is this possible ?
Card_Layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="4dp"
card_view:contentPadding="8dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
card_view:cardBackgroundColor="#color/abc_background_cache_hint_selector_material_light"
android:background="#drawable/big_card"
android:elevation="#dimen/elevation_high">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center_vertical"
/>
<ListView
android:id="#+id/cardList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:isScrollContainer="false"
>
</ListView>
</LinearLayout>
</android.support.v7.widget.CardView>
RecylerView.xml to show the cardview
<FrameLayout
android:id="#+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/cardList"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
P.s: Is this way List in List a bad solution ? Was there a better Solution to solve this?
just use this call it after setting the adapter for listview like below
cardlist.setadapter(...);
justifyListViewHeightBasedOnChildren(cardlist);
public void justifyListViewHeightBasedOnChildren(ListView listView) {
ListAdapter adapter = listView.getAdapter();
if (adapter == null) {
return;
}
ViewGroup vg = listView;
int totalHeight = 0;
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, vg);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams par = listView.getLayoutParams();
par.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
listView.setLayoutParams(par);
listView.requestLayout();
}
I want a Fragment that has 2 ListView. I dont want the listView to scroll instead i just want the whole Fragment to scroll. Heres the code:
<?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"
android:background="#fff" >
<TextView
android:id="#+id/day"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center_horizontal"
android:paddingTop="15dp"
android:text="Monday"
android:textColor="#000"
android:textSize="55sp" />
<TextView
android:id="#+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/day"
android:gravity="center_horizontal"
android:text="27 April, 2014"
android:textColor="#000"
android:textSize="45sp" />
<ListView
android:id="#+id/home_list_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/date"
android:layout_margin="10dp"
android:background="#color/list_home_time_bg"
android:divider="#android:color/white"
android:dividerHeight="20.0sp" />
<ListView
android:id="#+id/home_list_stime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/home_list_time"
android:layout_margin="10dp"
android:background="#color/list_home_stime_bg"
android:divider="#android:color/white"
android:dividerHeight="20.0sp" />
</RelativeLayout>
When i run this code the 2nd listView gets its own scroll bar, is there a way to stop this and make the entire Fragment to scroll?
To stop the listview scrolling use -
listView.setScrollContainer(false);
To scroll fragment put it inside ScrollView -
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- everything you already have -->
</ScrollView>
For more check - how-to-get-a-non-scrollable-listview.
NOTE:
If you put ListView inside a ScrollView then all the ListView does not stretch to its full height. Below is a method to fix this issue.
/**** Method for Setting the Height of the ListView dynamically.
**** Hack to fix the issue of not showing all the items of the ListView
**** when placed inside a ScrollView ****/
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
To use this method just pass the ListView inside this method :
ListView list1 = (ListView) view.findViewById(R.id.home_list_time);
setListViewHeightBasedOnChildren(list1);
ListView list2 = (ListView) view.findViewById(R.id.home_list_stime);
setListViewHeightBasedOnChildren(list2);
More here - android-list-view-inside-a-scroll-view.