在android中,经常需要用到listview,今天学习listview,和自定义的listview,以及下拉刷新的listview
传统的listview,布局如下:
<LinearLayout 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"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listviewid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
在activity中,有如下代码
package com.example.listview;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String []arrs = new String[]{"yyyy","tttt","eeee","ffff","aaaa","bbbb","aaaa"};
ListView listView = (ListView) findViewById(R.id.listviewid);
//为listview设置数据
ArrayAdapter<String>adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrs);
}
}
自定义布局的listview
自定义的布局:
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageId"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/textId"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4" />
</LinearLayout>
activivy的代码如下:
package com.example.listview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String []arrs = new String[]{"yyyy","tttt","eeee","ffff","aaaa","bbbb","aaaa"};
int[] images = new int[]{R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,};
List<Map<String,String>>list = new ArrayList<Map<String,String>>();
for (int i = 0; i < images.length; i++) {
Map<String,String>map = new HashMap<String, String>();
map.put("imageId",images[i]+"");
map.put("textId", arrs[i]);
list.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.list_item,new String[]{"imageId","textId"},new int[]{R.id.imageId,R.id.textId});
ListView listView = (ListView) findViewById(R.id.listviewid);
listView.setAdapter(adapter);
}
}
下拉刷新的listview
list_item.xml的布局如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="@+id/loadBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="加载更多"
/>
<ProgressBar
android:id="@+id/loadPro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
activity的代码如下所示:
package com.example.listview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
List<String>list = new ArrayList<String>();
ListView listView;
Button loadButton;
ProgressBar progressBar;
int max = 22;//加载数据的最大条数
int last;//当前最后一条数据的位置
int count = 5;//每次增加的条数
ArrayAdapter<String>adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
listView = (ListView) findViewById(R.id.listviewid);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
View footer = LayoutInflater.from(this).inflate(R.layout.list_item,null);
loadButton = (Button) footer.findViewById(R.id.loadBtn);
progressBar = (ProgressBar) footer.findViewById(R.id.loadPro);
progressBar.setVisibility(View.GONE);
listView.addFooterView(footer);
loadButton.setOnClickListener(new MyClickListener());
}
private void initView() {
// TODO Auto-generated method stub
for (int i = 0; i < 15; i++) {
list.add("test load more"+i);
}
}
public class MyClickListener implements OnClickListener {
@Override
public void onClick(View arg0) {
loadButton.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
// TODO Auto-generated method stub
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
last = adapter.getCount();
if (last+count < max) {//后面还有数据需要加载
for (int i = last; i < last + 5; i++) {
list.add("test add"+i);
}
loadButton.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
}else{//当前是最后一页
for (int i = last; i < last+(max-last); i++) {
list.add("test add"+i);
}
loadButton.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this,"全部数据以经加载完成",1000).show();
}
adapter.notifyDataSetChanged();
}
}, 2000);
}
}
}