在Android中使用ListView滚动线性布局
在我的应用程序中,我有一些按钮和一个具有不同布局的列表视图,列表视图位于最后一个位置 我的问题是我将在Android中使用ListView滚动线性布局,android,android-listview,scrollview,android-linearlayout,Android,Android Listview,Scrollview,Android Linearlayout,在我的应用程序中,我有一些按钮和一个具有不同布局的列表视图,列表视图位于最后一个位置 我的问题是我将滚动视图附加为根布局,以便正确地进行滚动。但是列表视图只显示一个列表项,不滚动 布局: <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/taskscrollvi
滚动视图
附加为根布局,以便正确地进行滚动。但是列表视图
只显示一个列表项,不滚动
布局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/taskscrollviewid"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:scrollbars="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:id="@+id/txtdate"
android:layout_width="0dip"
android:layout_height="30px"
android:layout_weight="1"
android:background="@drawable/taskbuttonselector" />
<Button
android:id="@+id/btnTaskTimeid"
android:layout_width="0dip"
android:layout_height="30px"
android:layout_weight="1"
android:background="@drawable/taskbuttonselector" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<EditText
android:id="@+id/txtItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/edittextselector"
android:drawableEnd="@drawable/inbox"
android:gravity="top|left"
android:hint="Enter task"
android:inputType="textMultiLine"
android:maxLines="5"
android:minLines="3"
android:scrollbars="vertical"
android:singleLine="false" />
<ImageButton
android:id="@+id/imagebuttonid"
android:layout_width="31dp"
android:layout_height="36dp"
android:layout_gravity="right|bottom"
android:background="@drawable/inbox" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:id="@+id/btnaddcategoryid"
android:layout_width="0dip"
android:layout_height="30px"
android:layout_weight="1"
android:background="@drawable/taskbuttonselector"
android:text="AddCategory" />
<Button
android:id="@+id/btnaddseverityid"
android:layout_width="0dip"
android:layout_height="30px"
android:layout_weight="1"
android:background="@drawable/taskbuttonselector"
android:text="AddSeverity" />
</LinearLayout>
<Button
android:id="@+id/btnadddb"
android:layout_width="250px"
android:layout_height="30px"
android:layout_gravity="center|center_horizontal"
android:layout_marginTop="10dp"
android:background="@drawable/taskbuttonselector"
android:text="Add This Task" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:id="@+id/fillbut"
android:layout_width="0dip"
android:layout_height="30px"
android:layout_weight="1"
android:background="@drawable/taskbuttonselector"
android:text="Remain Task" />
<Button
android:id="@+id/remainbut"
android:layout_width="0dip"
android:layout_height="30px"
android:layout_weight="1"
android:background="@drawable/taskbuttonselector"
android:text="Finish Task" />
</LinearLayout>
<ListView
android:id="@+id/listid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:clickable="true"
android:orientation="vertical" >
</ListView>
</LinearLayout>
</ScrollView>
添加
到您的列表视图
只需创建一个类
public class Utility {
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已经了解了滚动,并且会自动滚动
几天前我也遇到过同样的问题。这是我的问题
在@David Wasser的回答之后,我重新考虑了我的布局设计 试试这个-
myList = (ListView) findViewById(R.id.listView);
myList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listview_array));
Helper.getListViewSize(myList);
And-
public class Helper {
public static void getListViewSize(ListView myListView) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null) {
//do nothing return null
return;
}
//set listAdapter in loop for getting final size
int totalHeight = 0;
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, myListView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
//setting listview item in adapter
ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
myListView.setLayoutParams(params);
// print height of adapter on log
Log.i("height of listItem:", String.valueOf(totalHeight));
}
}
myList=(ListView)findViewById(R.id.ListView);
setAdapter(新的ArrayAdapter(这个,android.R.layout.simple_list_item_1,listview_数组));
getListViewSize(myList);
及-
公营助理员{
公共静态无效getListViewSize(ListView myListView){
ListAdapter myListAdapter=myListView.getAdapter();
if(myListAdapter==null){
//不执行任何操作返回空值
返回;
}
//在循环中设置listAdapter以获取最终大小
int totalHeight=0;
对于(int size=0;size
你也可以参考这篇文章-
最简单的解决方案。。只给底部加衬垫
<ListView
android:id="@+id/lv_Jobs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:smoothScrollbar="true"
android:paddingBottom="50dp"
android:textColor="#000000" />
在scrollview中使用listview是Android开发中的一大缺点,请快速将其删除,并将其放置在scrollview之外。请查看以下答案[在此处输入链接描述][1][1]:检查thuisAny luck?请尝试我的答案,它应该会起作用。只需在列表视图中添加标题视图,我想这就是您想要的。为了每个人,请不要。。。我是说永远!!按照一些答案的建议去做……不。。。不要这样做。。。说真的,永远不要这么做。。。设备迟早会耗尽内存。如果你这样做,ListView
的全部目的都会被点燃。这不是一个正确的答案。。我不知道他为什么给你正确答案。。这只是一个静态代码lol检查我的答案
myList = (ListView) findViewById(R.id.listView);
myList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listview_array));
Helper.getListViewSize(myList);
And-
public class Helper {
public static void getListViewSize(ListView myListView) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null) {
//do nothing return null
return;
}
//set listAdapter in loop for getting final size
int totalHeight = 0;
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, myListView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
//setting listview item in adapter
ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
myListView.setLayoutParams(params);
// print height of adapter on log
Log.i("height of listItem:", String.valueOf(totalHeight));
}
}
<ListView
android:id="@+id/lv_Jobs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:smoothScrollbar="true"
android:paddingBottom="50dp"
android:textColor="#000000" />