Android 如何对相对布局进行充气
我有一个任务来扩展XML视图。我真的不知道该怎么办。我 使用基本适配器Android 如何对相对布局进行充气,android,Android,我有一个任务来扩展XML视图。我真的不知道该怎么办。我 使用基本适配器 @Override public View getView(int position, View convertView, ViewGroup parent) { //TODO - Get the current ToDoItem final ToDoItem toDoItem = mItems.get(position); //TODO - Inflate the View for this
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//TODO - Get the current ToDoItem
final ToDoItem toDoItem = mItems.get(position);
//TODO - Inflate the View for this ToDoItem
// from todo_item.xml.
RelativeLayout itemLayout =(RelativeLayout)convertView.findViewById(R.id.RelativeLayout1);
View InflatedView= null;
todo_item.xml看起来像
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView>
<!-- some code -->
</TextView>
</RelativeLayout>
但这不起作用。我也不理解传递给getview方法的convertView。ListViewAPI有一些古怪之处
getView()
中提供给您的convertView
参数实际上可以为空。这就是ListView
如何在用户滚动列表时循环视图。因此,您不能使用它来为您的相对值膨胀
要获取视图,应首先检查convertView
是否为空。如果是,则需要使用提供给您的parent
参数对视图进行充气
RelativeLayout itemLayout;
if (convertView != null) {
itemLayout = (RelativeLayout) convertView;
} else {
LayoutInflater inflater = LayoutInflater.from(parent.context);
itemLayout = (RelativeLayout) inflater.inflate(R.layout.todo_item, parent, false);
}
RelativeLayout itemLayout;
if (convertView != null) {
itemLayout = (RelativeLayout) convertView;
} else {
LayoutInflater inflater = LayoutInflater.from(parent.context);
itemLayout = (RelativeLayout) inflater.inflate(R.layout.todo_item, parent, false);
}