如何添加页脚中的ListView?
-
27-09-2019 - |
题
我开发的应用程序,在我的应用程序中,我使用列表视图使用DOM解析显示的数据,我想页脚在列表视图,当我点击页脚附加更多的数据添加到列表视图,我连着形象,我想该设计和工艺,请参考在红色矩形图像1和imgae2.I提及页脚
<强> FIG1页脚如 “更多新闻”强>结果
Fig2-添加在列表视图中加入额外的10记录
解决方案
创建一个由文字,你要设置为页脚页脚视图布局,然后尝试
View footerView = ((LayoutInflater) ActivityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
ListView.addFooterView(footerView);
布局页脚可能是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="7dip"
android:paddingBottom="7dip"
android:orientation="horizontal"
android:gravity="center">
<LinearLayout
android:id="@+id/footer_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center">
<TextView
android:text="@string/footer_text_1"
android:id="@+id/footer_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textStyle="bold"
android:layout_marginRight="5dip" />
</LinearLayout>
</LinearLayout>
的活性类可以是:
public class MyListActivty extends ListActivity {
private Context context = null;
private ListView list = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list = (ListView)findViewById(android.R.id.list);
//code to set adapter to populate list
View footerView = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
list.addFooterView(footerView);
}
}
其他提示
我知道这是一个非常古老的问题,但我在这里用Google搜索了路,找到了答案提供的不是100%满意,因为GCL1提到的 - 这样的页脚是不是一个真正的页脚到屏幕上 - 它只是一个“附加”到列表中。
底线 - 为别人谁可以在这里谷歌自己的方式 - 我发现了以下建议此处的
固定和始终可见页脚>尝试做如下,其中强调的是在XML列出的第一个按钮(或任何页脚元素) - ,然后将列表被添加为“layout_above”:
<RelativeLayout>
<Button android:id="@+id/footer" android:layout_alignParentBottom="true"/>
<ListView android:id="@android:id/list" **android:layout_above**="@id/footer"> <!-- the list -->
</RelativeLayout>
这里答案是有点过时。虽然码保持不变有在行为的一些变化。
public class MyListActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
TextView footerView = (TextView) ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_view, null, false);
getListView().addFooterView(footerView);
setListAdapter(new ArrayAdapter<String>(this, getResources().getStringArray(R.array.news)));
}
}
信息关于addFooterView()
方法
添加固定以便出现在列表的底部。如果
addFooterView()
被称为不止一次的意见将出现在加入他们的顺序。查看使用此调用可以重点,如果他们想加入。
最上面的应力很重要的一点的答案 -
addFooterView()
必须调用setAdapter()
.This之前调用是这样的ListView可以与一个也将占页眉和页脚视图包裹随附的光标。
从这个奇巧已经改变。
注意:当首次引入,这种方法只能用setAdapter(ListAdapter)设置适配器之前调用。与奇巧开始,此方法可以在任何时候被调用。如果ListView的适配器不延伸HeaderViewListAdapter,它将与WrapperListAdapter的支撑实例缠绕。
如果ListView的是ListActivity的子:
getListView().addFooterView(
getLayoutInflater().inflate(R.layout.footer_view, null)
);
(内侧的onCreate())
要在其中添加列表视图页脚和我有同样的活动产生的列表视图页脚点击的事件。
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list_of_f = (ListView) findViewById(R.id.list_of_f);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.web_view, null); // i have open a webview on the listview footer
RelativeLayout layoutFooter = (RelativeLayout) view.findViewById(R.id.layoutFooter);
list_of_f.addFooterView(view);
}
}
activity_main.xml中
<?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="@drawable/bg" >
<ImageView
android:id="@+id/dept_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/dept_nav" />
<ListView
android:id="@+id/list_of_f"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/dept_nav"
android:layout_margin="5dp"
android:layout_marginTop="10dp"
android:divider="@null"
android:dividerHeight="0dp"
android:listSelector="@android:color/transparent" >
</ListView>
</RelativeLayout>
在这个问题,最好的答案不是为我工作。在此之后,我发现这个方法来显示列表视图页脚,
LayoutInflater inflater = getLayoutInflater();
ViewGroup footerView = (ViewGroup)inflater.inflate(R.layout.footer_layout,listView,false);
listView.addFooterView(footerView, null, false);
和创建新的布局呼叫footer_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Done"
android:textStyle="italic"
android:background="#d6cf55"
android:padding="10dp"/>
</LinearLayout>
如果不工作参阅本文听到一>