如题,一个朋友问的,简单的实现了 一下!现在把demo放上来,大家看看!有什么不足的地方指正下!谢谢O(∩_∩)O~
先简单的把代码贴出来一些吧! 下边有整个demo的下载!
确实出错了!滑动的时候会出现错乱现象!现在修改了一下!
适配器类 MyListAdapter 中从getView()方法之后的代码都是重新写的!
自定义的适配器类
[mw_shl_code=java,true]package com.listview.lzan;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
public class MyListAdapter extends BaseAdapter{
private Activity activity;
private int[] type;
private LayoutInflater mInflater;
public MyListAdapter(Activity activity,int[] type){
this.activity = activity;
this.type = type;
mInflater = activity.getLayoutInflater();
}
@Override
public int getCount() {
return type.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//这里判断要显示什么控件
switch(type[position]){
case 0://textview
convertView = getTextView();
break;
case 1://Button
convertView = getButton();
break;
case 2://imageView
convertView = getImageView();
break;
case 3://自定义布局
convertView = getMyView();
break;
}
return convertView;
}
private TextView getTextView(){
TextView text = new TextView(activity);
text.setText("这是TextView控件");
text.setPadding(0, 15, 0, 15);
return text;
}
private Button getButton(){
Button but = new Button(activity);
but.setText("这是Button控件");
return but;
}
private ImageView getImageView(){
ImageView img= new ImageView(activity);
img.setImageResource(R.drawable.sub_icon);
return img;
}
private View getMyView(){
View view = mInflater.inflate(R.layout.allwidget, null);
return view;
}
}
[/mw_shl_code]
自定义布局控件的xml布局文件 allwidget.xml
[mw_shl_code=java,true] <?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"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imgview"
android:src="@drawable/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
android:text="这是自己定义的一个布局"
android:textColor="@android:color/white"
android:textSize="18dp"/>
</LinearLayout> [/mw_shl_code]
主布局类 main.xml
[mw_shl_code=java,true]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/list"
android:divider="@android:color/white"
android:dividerHeight="1dp"></ListView>
</LinearLayout>[/mw_shl_code]
主Activity类
[mw_shl_code=java,true]package com.listview.lzan;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class ListviewActivity extends Activity {
/** Called when the activity is first created. */
private ListView list;
private int[] type = {0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (ListView)findViewById(R.id.list);
list.setAdapter(new MyListAdapter(this,type));
}
}[/mw_shl_code]
附件也是修改过后的!
点击下载附件也已更新,免积分下载
先简单的把代码贴出来一些吧! 下边有整个demo的下载!
确实出错了!滑动的时候会出现错乱现象!现在修改了一下!
适配器类 MyListAdapter 中从getView()方法之后的代码都是重新写的!
自定义的适配器类
[mw_shl_code=java,true]package com.listview.lzan;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
public class MyListAdapter extends BaseAdapter{
private Activity activity;
private int[] type;
private LayoutInflater mInflater;
public MyListAdapter(Activity activity,int[] type){
this.activity = activity;
this.type = type;
mInflater = activity.getLayoutInflater();
}
@Override
public int getCount() {
return type.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//这里判断要显示什么控件
switch(type[position]){
case 0://textview
convertView = getTextView();
break;
case 1://Button
convertView = getButton();
break;
case 2://imageView
convertView = getImageView();
break;
case 3://自定义布局
convertView = getMyView();
break;
}
return convertView;
}
private TextView getTextView(){
TextView text = new TextView(activity);
text.setText("这是TextView控件");
text.setPadding(0, 15, 0, 15);
return text;
}
private Button getButton(){
Button but = new Button(activity);
but.setText("这是Button控件");
return but;
}
private ImageView getImageView(){
ImageView img= new ImageView(activity);
img.setImageResource(R.drawable.sub_icon);
return img;
}
private View getMyView(){
View view = mInflater.inflate(R.layout.allwidget, null);
return view;
}
}
[/mw_shl_code]
自定义布局控件的xml布局文件 allwidget.xml
[mw_shl_code=java,true] <?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"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imgview"
android:src="@drawable/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
android:text="这是自己定义的一个布局"
android:textColor="@android:color/white"
android:textSize="18dp"/>
</LinearLayout> [/mw_shl_code]
主布局类 main.xml
[mw_shl_code=java,true]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/list"
android:divider="@android:color/white"
android:dividerHeight="1dp"></ListView>
</LinearLayout>[/mw_shl_code]
主Activity类
[mw_shl_code=java,true]package com.listview.lzan;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class ListviewActivity extends Activity {
/** Called when the activity is first created. */
private ListView list;
private int[] type = {0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (ListView)findViewById(R.id.list);
list.setAdapter(new MyListAdapter(this,type));
}
}[/mw_shl_code]
点击下载附件也已更新,免积分下载