Android ListView:是否可以将数据添加到几行之前,而 ListView 的其余部分是动态加载的?
Android ListView: Is it possible to add data to a few rows before-wards, while the rest of the ListView is loaded dynamically?
我想创建一个 ListView
除了最后两行之外的所有行都具有相同的行布局。同样,我想填充来自名为 MyDataClass
的 class 对象的相同 ArrayList
的所有行,但最后两行除外。
对于最后两行:我希望倒数第二行有一个 Button
,而最后一行只有 ImageView
。
这有两部分:(a) 将ListView
中要显示的所有数据添加到ListView
的数据源(这里的ArrayList
). (b) 在 getView
内,执行如下操作:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (position == arrayList.size()-2 )
xml_type = R.layout.row_imageView
else if (position == arrayList.size()-1 )
xml_type = R.layout.row_button
else
xml_type = R.layout.row_normal
View rowView = inflater.inflate(xml_type, parent, false);
}
现在问题来自 (a):如果图像和按钮是我添加到每一行的东西,那么制作 MyDataClass
对象的图像和按钮字段可能是有意义的,但它们只是一张图片和一个按钮,需要添加到最后两行。
那么有没有办法对不同的行使用不同的 datasets/data 来源?
或者更好的是,是否可以将数据设置为之前的最后两行,而 ListView
的其余部分是动态填充的?
ListView
只显示可见的行,而且速度很快。如果数据不可用,那么它将等待直到数据可用。
问题是您需要在后台加载数据,这样 ListView
才不会延迟显示它已有的内容。如果您有占位符或其他静态内容或易于显示的选项,那么 ListView
将按您希望的方式执行。
这听起来像列表页脚,可以使用 addFooterView
向 ListView 添加页脚视图。
public void addFooterView (View v)
Add a fixed view to appear at the bottom of the list. If addFooterView is called more than once, the views will appear in the order they were added. Views added using this call can take focus if they want.
MainActivity.java
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// grab your list
ListView list = (ListView) findViewById(R.id.list);
// grab a layout inflater
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// inflate the image row layout
ImageView footerImage = (ImageView)inflater.inflate(R.layout.list_footer_image, null);
// .. maybe change the image here, bind click listener - whatever
// add the footer image to the listview
list.addFooterView(footerImage);
// inflate the layout for the button row
Button footerButton = (Button)inflater.inflate(R.layout.list_footer_button, null);
// bind a click listener
footerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// user has clicked the button in the footer, do something
}
});
// add footer button to the list view
list.addFooterView(footer);
// ... populate your list with the adapter as usual
}
}
layout/list_footer_image.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/your_image"/>
layout/list_footer_button.xml
<?xml version="1.0" encoding="utf-8"?>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click me!"/>
我想创建一个 ListView
除了最后两行之外的所有行都具有相同的行布局。同样,我想填充来自名为 MyDataClass
的 class 对象的相同 ArrayList
的所有行,但最后两行除外。
对于最后两行:我希望倒数第二行有一个 Button
,而最后一行只有 ImageView
。
这有两部分:(a) 将ListView
中要显示的所有数据添加到ListView
的数据源(这里的ArrayList
). (b) 在 getView
内,执行如下操作:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (position == arrayList.size()-2 )
xml_type = R.layout.row_imageView
else if (position == arrayList.size()-1 )
xml_type = R.layout.row_button
else
xml_type = R.layout.row_normal
View rowView = inflater.inflate(xml_type, parent, false);
}
现在问题来自 (a):如果图像和按钮是我添加到每一行的东西,那么制作 MyDataClass
对象的图像和按钮字段可能是有意义的,但它们只是一张图片和一个按钮,需要添加到最后两行。
那么有没有办法对不同的行使用不同的 datasets/data 来源?
或者更好的是,是否可以将数据设置为之前的最后两行,而 ListView
的其余部分是动态填充的?
ListView
只显示可见的行,而且速度很快。如果数据不可用,那么它将等待直到数据可用。
问题是您需要在后台加载数据,这样 ListView
才不会延迟显示它已有的内容。如果您有占位符或其他静态内容或易于显示的选项,那么 ListView
将按您希望的方式执行。
这听起来像列表页脚,可以使用 addFooterView
向 ListView 添加页脚视图。
public void addFooterView (View v)
Add a fixed view to appear at the bottom of the list. If addFooterView is called more than once, the views will appear in the order they were added. Views added using this call can take focus if they want.
MainActivity.java
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// grab your list
ListView list = (ListView) findViewById(R.id.list);
// grab a layout inflater
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// inflate the image row layout
ImageView footerImage = (ImageView)inflater.inflate(R.layout.list_footer_image, null);
// .. maybe change the image here, bind click listener - whatever
// add the footer image to the listview
list.addFooterView(footerImage);
// inflate the layout for the button row
Button footerButton = (Button)inflater.inflate(R.layout.list_footer_button, null);
// bind a click listener
footerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// user has clicked the button in the footer, do something
}
});
// add footer button to the list view
list.addFooterView(footer);
// ... populate your list with the adapter as usual
}
}
layout/list_footer_image.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/your_image"/>
layout/list_footer_button.xml
<?xml version="1.0" encoding="utf-8"?>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click me!"/>