布局xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ListLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#FFF"
android:padding="10dp">
<ImageView
android:id="@+id/myImageView"
android:src="@drawable/bblood"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp">
<TextView
android:id="@+id/TextName"
android:text="这里是名称显示"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#000"/>
<TextView
android:id="@+id/Description"
android:text="这里是描述显示"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textColor="#000"/>
</LinearLayout>
</LinearLayout>
list内容适配器(使用了子布局缓存和主布局复用):
package com.example.lenovo.myapplication;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by lenovo on 2018/4/25.
*/
public class ListAdapter extends ArrayAdapter<ListArray> {
private int ListLayoutId;
public ListAdapter(Context context , int textViewResourrceId, List<ListArray> obj){
super(context,textViewResourrceId,obj);
this.ListLayoutId = textViewResourrceId;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ListArray listArray = getItem(position);
View view ;
ViewHolder viewHolder;
if(convertView == null){
view = LayoutInflater.from(getContext()).inflate(ListLayoutId,parent,false);
viewHolder = new ViewHolder();
viewHolder.HmyImageView = (ImageView)view.findViewById(R.id.myImageView);
viewHolder.HtextName = (TextView)view.findViewById(R.id.TextName);
viewHolder.Hdescription = (TextView)view.findViewById(R.id.Description);
view.setTag(viewHolder);
}else {
view = convertView;
viewHolder = (ViewHolder)view.getTag();
}
viewHolder.HmyImageView.setImageResource(listArray.getIconId());
viewHolder.HtextName.setText(listArray.getName());
viewHolder.Hdescription.setText(listArray.getDescription());
return view;
}
class ViewHolder{
ImageView HmyImageView;
TextView HtextName;
TextView Hdescription;
}
}
listView实现activity类:
package com.example.lenovo.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class ListDemoActivity extends AppCompatActivity {
private List<ListArray> listArray = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_demo);
dataImport();
ListAdapter listAdapter = new ListAdapter(ListDemoActivity.this,R.layout.list_layout,listArray);
ListView listView = (ListView)findViewById(R.id.ListView01);
listView.setAdapter(listAdapter);
}
private void dataImport(){
ListArray listArray1 = new ListArray("Blood","血",R.drawable.bblood);
listArray.add(listArray1);
ListArray listArray2 = new ListArray("BloodRing","血环",R.drawable.bbloodrring);
listArray.add(listArray2);
ListArray listArray3 = new ListArray("ColoredFlowers","五色花",R.drawable.ccoloredfflowers);
listArray.add(listArray3);
ListArray listArray4 = new ListArray("Delight","欢欣",R.drawable.ddelight);
listArray.add(listArray4);
ListArray listArray5 = new ListArray("DrippingFlower","水滴花",R.drawable.ddrippingfflower);
listArray.add(listArray5);
ListArray listArray6 = new ListArray("EyeOfFlame","火焰之眼",R.drawable.eeyeooffflame);
listArray.add(listArray6);
ListArray listArray7 = new ListArray("EyeOfRotation","旋转之眼",R.drawable.eeyeoofrrotation);
listArray.add(listArray7);
ListArray listArray8 = new ListArray("Flame","火焰",R.drawable.fflame);
listArray.add(listArray8);
ListArray listArray9 = new ListArray("FourPetals","四叶花",R.drawable.ffourppetals);
listArray.add(listArray9);
ListArray listArray10 = new ListArray("Gradient","渐变",R.drawable.ggradient);
listArray.add(listArray10);
ListArray listArray11 = new ListArray("GreenLeaf","绿叶",R.drawable.ggreenlleaf);
listArray.add(listArray11);
ListArray listArray12 = new ListArray("IronAngle","铁角花",R.drawable.iironaangle);
listArray.add(listArray12);
ListArray listArray13 = new ListArray("IronThorns","绿叶",R.drawable.iirontthorns);
listArray.add(listArray13);
ListArray listArray14 = new ListArray("lookUp","向上看",R.drawable.llookuup);
listArray.add(listArray14);
ListArray listArray15 = new ListArray("Rotatingleaves","旋叶",R.drawable.rrotatingleaves);
listArray.add(listArray15);
ListArray listArray16 = new ListArray("ShadowFlow","暗影流",R.drawable.sshadowfflow);
listArray.add(listArray16);
ListArray listArray17 = new ListArray("TechnologyEye","科技眼",R.drawable.tthreelleaves);
listArray.add(listArray17);
ListArray listArray18 = new ListArray("ThreeLeaves","三叶",R.drawable.iirontthorns);
listArray.add(listArray18);
}
}
本文来自博客园,作者:观心静 ,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/9708633.html
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。