SimpleAdapter 的简单介绍
SimpleAdapter是AdaterView的控件设置adapter用
SimpleAdapter是Adapter类的子类,可用于list集合的多个对象包装成多个列表项
SimpleAdapter adapter=new SimpleAdapter(Context,list<?extends Map<String,?>> datas,int itemResourse,new String[]{map集合中的所有键值},new int[]{itemResourse每项布局上的控件id});//第二个参数和第四个参数的集合中map集合的key是对应的并且和控件上的控件也是对应的。
简单使用:可以直接使用listview等AdapterView,setadapter(new SimpleAdapter());
自定义SimpleAdapter
定义一个类重写构造,重写getview方法
实例:
布局
<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="com.example.horizontalscrollview_listview.MainActivity"
android:orientation="vertical">
<HorizontalScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="40dp"
android:scrollbars="none"
android:layout_weight="0">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center" >
<TextView
android:layout_width="150dip"
android:layout_height="wrap_content"
android:gravity="center"
android:text="你的名字" />
<TextView
android:layout_width="150dip"
android:layout_height="wrap_content"
android:gravity="center"
android:text="你的爱好" />
<TextView
android:layout_width="150dip"
android:layout_height="wrap_content"
android:gravity="center"
android:text="你的专业" />
<TextView
android:layout_width="150dip"
android:layout_height="wrap_content"
android:gravity="center"
android:text="你的年龄" />
<TextView
android:layout_width="150dip"
android:layout_height="wrap_content"
android:gravity="center"
android:text="是否是独生子" />
<TextView
android:layout_width="150dip"
android:layout_height="wrap_content"
android:gravity="center"
android:text="是否接受过培训" />
<TextView
android:layout_width="150dip"
android:layout_height="wrap_content"
android:gravity="center"
android:text="从事工作时间" />
</LinearLayout>
</HorizontalScrollView>
<ListView
android:id="@+id/horizontalscall_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:scrollbars="none">
</ListView>
</LinearLayout>
每项布局:
<?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:minHeight="50dip"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants">
<HorizontalScrollView
android:id="@+id/item_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/item_data1"
android:layout_width="150dip"
android:gravity="center"
style="@style/listItemStyle2" />
<TextView
android:id="@+id/item_data2"
android:layout_width="150dip"
android:gravity="center"
style="@style/listItemStyle3"/>
<TextView
android:id="@+id/item_data3"
android:layout_width="150dip"
android:gravity="center"
style="@style/listItemStyle2" />
<TextView
android:id="@+id/item_data4"
android:layout_width="150dip"
android:gravity="center"
style="@style/listItemStyle3"/>
<TextView
android:id="@+id/item_data5"
android:layout_width="150dip"
android:gravity="center"
style="@style/listItemStyle2" />
<TextView
android:id="@+id/item_data6"
android:layout_width="150dip"
android:gravity="center"
style="@style/listItemStyle3"/>
<TextView
android:id="@+id/item_data7"
android:layout_width="150dip"
android:gravity="center"
style="@style/listItemStyle2" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
Activity:
public class MainActivity extends ActionBarActivity {
ListView horizontalscall_listview;
HorizontalScrollView scroll;
public List<HorizontalScrollView> mHScrollViews =new ArrayList<HorizontalScrollView>();
private List<Map<String,String>> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
horizontalscall_listview=(ListView) findViewById(R.id.horizontalscall_listview);
scroll=(HorizontalScrollView) findViewById(R.id.scroll);
mHScrollViews.add(scroll);
add();
}
public void add(){
data=new ArrayList<Map<String,String>>();
for(int i=0;i<5;i++){
Map<String,String> map=new HashMap<String,String>();
map.put("name", "刘海");
map.put("like", "篮球");
map.put("zhuanye", "计算机");
map.put("age", "24");
map.put("lone", "否");
map.put("student", "否");
map.put("time", "now");
data.add(map);
}
SimpleAdapter adapter=new NewSimpleAdapter(this,data,R.layout.activity_pd_item,new String[]{"name","like","zhuanye"
,"age","lone","student","time"},new int[]{R.id.item_data1,R.id.item_data2,R.id.item_data3,R.id.item_data4,R.id.item_data5,R.id.item_data6,R.id.item_data7});
horizontalscall_listview.setAdapter(adapter);
}
}
适配器:
public class NewSimpleAdapter extends SimpleAdapter {
private LayoutInflater inflater=null;
private List<Map<String, String>> data;
private int resource;
private int[] to;
private String[] from;
public NewSimpleAdapter(Context context,List<Map<String, String>> data, int resource, String[] from,
int[] to) {
super(context, data, resource, from, to);
inflater=LayoutInflater.from(context);
this.data=data;
this.resource=resource;
this.to=to;
this.from=from;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
convertView=inflater.inflate(resource, null);//单项布局加载
View[] view=new View[to.length];//将他作为重复使用的对象//优化的实现
for(int i=0;i<to.length;i++){
view[i]=convertView.findViewById(to[i]);//将每项中的所有控件都保存到每项的集合中
}
convertView.setTag(view);
}
View[] v=(View[]) convertView.getTag();//每项布局上的所有控件的集合
for(int i=0;i<to.length;i++){
((TextView)v[i].findViewById(to[i])).setText(data.get(position).get(from[i]));//社会scroll上的内容,from集合中保存的是data集合中每个map的键值
}
return convertView;
}
}
自定义适配器和水平滑动控件实现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="com.example.horizontalscrollview_listview.MainActivity"
android:orientation="vertical">
<com.example.horizontalscrollview_listview.CHScrollViewXs
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="40dp"
android:scrollbars="none"
android:layout_weight="0">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center" >
<TextView
android:layout_width="150dip"
android:layout_height="wrap_content"
android:gravity="center"
android:text="你的名字" />
<TextView
android:layout_width="150dip"
android:layout_height="wrap_content"
android:gravity="center"
android:text="你的爱好" />
<TextView
android:layout_width="150dip"
android:layout_height="wrap_content"
android:gravity="center"
android:text="你的专业" />
<TextView
android:layout_width="150dip"
android:layout_height="wrap_content"
android:gravity="center"
android:text="你的年龄" />
<TextView
android:layout_width="150dip"
android:layout_height="wrap_content"
android:gravity="center"
android:text="是否是独生子" />
<TextView
android:layout_width="150dip"
android:layout_height="wrap_content"
android:gravity="center"
android:text="是否接受过培训" />
<TextView
android:layout_width="150dip"
android:layout_height="wrap_content"
android:gravity="center"
android:text="从事工作时间" />
</LinearLayout>
</com.example.horizontalscrollview_listview.CHScrollViewXs>
<ListView
android:id="@+id/horizontalscall_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:scrollbars="none">
</ListView>
</LinearLayout>
单项布局:
<?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:minHeight="50dip"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants">
<com.example.horizontalscrollview_listview.CHScrollViewXs
android:id="@+id/item_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/item_data1"
android:layout_width="150dip"
android:gravity="center"
style="@style/listItemStyle2" />
<TextView
android:id="@+id/item_data2"
android:layout_width="150dip"
android:gravity="center"
style="@style/listItemStyle3"/>
<TextView
android:id="@+id/item_data3"
android:layout_width="150dip"
android:gravity="center"
style="@style/listItemStyle2" />
<TextView
android:id="@+id/item_data4"
android:layout_width="150dip"
android:gravity="center"
style="@style/listItemStyle3"/>
<TextView
android:id="@+id/item_data5"
android:layout_width="150dip"
android:gravity="center"
style="@style/listItemStyle2" />
<TextView
android:id="@+id/item_data6"
android:layout_width="150dip"
android:gravity="center"
style="@style/listItemStyle3"/>
<TextView
android:id="@+id/item_data7"
android:layout_width="150dip"
android:gravity="center"
style="@style/listItemStyle2" />
</LinearLayout>
</com.example.horizontalscrollview_listview.CHScrollViewXs>
</LinearLayout>
自定义滑动控件
public class CHScrollViewXs extends HorizontalScrollView {
MainActivity activity;
public CHScrollViewXs(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
activity = (MainActivity) context;
}
public CHScrollViewXs(Context context, AttributeSet attrs) {
super(context, attrs);
activity = (MainActivity) context;
}
public CHScrollViewXs(Context context) {
super(context);
activity = (MainActivity) context;
}
@SuppressLint("ClickableViewAccessibility") @Override
public boolean onTouchEvent(MotionEvent ev) {
activity.scroll = this;
return super.onTouchEvent(ev);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
if(activity.scroll == this) {
activity.onScrollChanged(l, t, oldl, oldt);//当滑动后调用main中的滑动
}else{
super.onScrollChanged(l, t, oldl, oldt);
}
}
}
自定义适配器
public class NewSimpleAdapter extends SimpleAdapter {
private LayoutInflater inflater=null;
private List<Map<String, String>> data;
private int resource;
private int[] to;
private String[] from;
private List<HorizontalScrollView> scrollViews;
private ListView listview;
public NewSimpleAdapter(Context context,List<Map<String, String>> data, int resource, String[] from,
int[] to,List<HorizontalScrollView> scrollViews,ListView listview) {
super(context, data, resource, from, to);
inflater=LayoutInflater.from(context);
this.data=data;
this.resource=resource;
this.to=to;
this.from=from;
this.scrollViews=scrollViews;
this.listview=listview;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
convertView=inflater.inflate(resource, null);//单项布局加载
addHViews((HorizontalScrollView) convertView.findViewById(R.id.item_scroll));
View[] view=new View[to.length];//将他作为重复使用的对象//优化的实现
for(int i=0;i<to.length;i++){
view[i]=convertView.findViewById(to[i]);//将每项中的所有控件都保存到每项的集合中
}
convertView.setTag(view);
}
View[] v=(View[]) convertView.getTag();//每项布局上的所有控件的集合
for(int i=0;i<to.length;i++){
((TextView)v[i].findViewById(to[i])).setText(data.get(position).get(from[i]));//社会scroll上的内容,from集合中保存的是data集合中每个map的键值
}
return convertView;
}
//将所有的水平滑动的控件都加入滑动集合中
public void addHViews(final HorizontalScrollView hScrollView) {
if (!scrollViews.isEmpty()) {
//对于listview的下一屏的控件先获取他的上一条的水平滑动控件的滑动距离,然后滑动本条水平控件,并让其水平滑动
int size = scrollViews.size();
HorizontalScrollView scrollView = scrollViews.get(size - 1);
final int scrollX = scrollView.getScrollX();
if (scrollX != 0) {
//在UI线程哄对其滑动
listview.post(new Runnable() {
@Override
public void run() {
hScrollView.scrollTo(scrollX, 0);
}
});
}
}
//将所有展示出来的水平滑动控件加入集合,加入集合后再滑动每项,都会调用它的onScrollChanged()进而调用MarinActivity的onScrollChanged()循环滑动集合中的所有水平控件
scrollViews.add(hScrollView);
}
}
Activity:
public class MainActivity extends ActionBarActivity {
ListView horizontalscall_listview;
HorizontalScrollView scroll;
public List<HorizontalScrollView> mHScrollViews =new ArrayList<HorizontalScrollView>();
private List<Map<String,String>> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
horizontalscall_listview=(ListView) findViewById(R.id.horizontalscall_listview);
scroll=(HorizontalScrollView) findViewById(R.id.scroll);
mHScrollViews.add(scroll);
add();
}
public void add(){
data=new ArrayList<Map<String,String>>();
for(int i=0;i<20;i++){
Map<String,String> map=new HashMap<String,String>();
map.put("name", "刘海");
map.put("like", "篮球");
map.put("zhuanye", "计算机");
map.put("age", "24");
map.put("lone", "否");
map.put("student", "否");
map.put("time", "now");
data.add(map);
}
SimpleAdapter adapter=new NewSimpleAdapter(this,data,R.layout.activity_pd_item,new String[]{"name","like","zhuanye"
,"age","lone","student","time"},new int[]{R.id.item_data1,R.id.item_data2,R.id.item_data3,R.id.item_data4,R.id.item_data5,R.id.item_data6,R.id.item_data7},mHScrollViews,horizontalscall_listview);
horizontalscall_listview.setAdapter(adapter);
}
//全部listview上控件的水平滑动
public void onScrollChanged(int l, int t, int oldl, int oldt){
for(HorizontalScrollView scrollView : mHScrollViews) {
if(scroll != scrollView)
scrollView.smoothScrollTo(l, t);//循环实现所有的每项的水平控件都滑动实现滑动
}
}
}