//布局文件
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.mylistview.MainActivity"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lv" ></ListView> </android.support.constraint.ConstraintLayout>//布局文件2
<?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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/name" android:text="商品1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/goods" /> </LinearLayout>//布局文件3
<?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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/name2" android:text="商品2" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/img" android:background="@mipmap/ic_launcher" /> </LinearLayout>
//MainActivity
package com.example.mylistview; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ListView lv; List<String> list=new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv =(ListView)findViewById(R.id.lv); indata(); lv.setAdapter(new mydata()); } private void indata() { for (int i=0;i<50;i++){ list.add(i+""); } } class mydata extends BaseAdapter{ final int textview1=0; final int textview2=1; @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder vh; int type = getItemViewType(i); if(view==null){ vh=new ViewHolder(); switch (type){ case 0: view=View.inflate(MainActivity.this,R.layout.textview1,null); vh.name=view.findViewById(R.id.name); vh.goods=view.findViewById(R.id.goods); break; case 1: view=View.inflate(MainActivity.this,R.layout.texteview2,null); vh.name2=view.findViewById(R.id.name2); vh.img=view.findViewById(R.id.img); break; default:break; } view.setTag(vh); }else{ vh=(ViewHolder)view.getTag(); } switch (type){ case textview1: vh.name.setText("商品1"+list.get(i).toString()); vh.goods.setText("香蕉"+list.get(i).toString()); break; case textview2: vh.name2.setText("商品2"+list.get(i).toString()); vh.img.setBackgroundResource(R.mipmap.ic_launcher); break; } return view; } @Override public int getViewTypeCount() { return 2; } @Override public int getItemViewType(int position) { int p=position%6; if(p==0){ return textview1; }else if(p<3){ return textview2; }else{ return textview1; } } class ViewHolder{ TextView name,name2,goods; ImageView img; } } }