安卓基础--ListView

2017-06-26  本文已影响47人  小白猿

本文是我学习安卓的笔记的一部分,查看详细完整笔记请参阅

ListView

先简单介绍一下概念,然后举个🌰


基本使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>
</LinearLayout>
 private  String[] dataSource = {
            "1", "2","3","2",  "4",
            "2", "5", "6", "7", "8", 
            "9",  "10", "11","12",
            "13"
    };
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ll_main_layout);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_expandable_list_item_1,dataSource);
        ListView listView = (ListView)findViewById(R.id.list_view);
        listView.setAdapter(adapter);
    }
}

对Adapter的详解


**自定义item **

大致思路

              /*****************自定义布局*****************/ 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation = "horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/list_item_image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/list_item_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:layout_margin="20dp"/>

</LinearLayout>

              /***************** Fruit类*****************/ 
public class Fruit {
    public String title;
    public int imageName;

    Fruit(String title, int imageName) {
        this.title = title;
        this.imageName = imageName;

    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getImageName() {
        return imageName;
    }



    public void setImageName(int imageName) {
                 this.imageName = imageName;
    }


/*****************自定义Adapter*****************/ 
public class FruitAdapter extends ArrayAdapter<Fruit> {

    private int resourceId;

    public FruitAdapter(Context context, int textViewResourceId, List<Fruit> object) {
        super(context, textViewResourceId,object);
        resourceId = textViewResourceId;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        // 获取fruit 实例
        Fruit fruit = getItem(position);
        //
        View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);

        ImageView fruitImage = (ImageView) view.findViewById(R.id.list_item_image_view);

        TextView fruitName = (TextView) view.findViewById(R.id.list_item_text_view);

        fruitImage.setImageResource(fruit.getImageName());

        fruitName.setText(fruit.getTitle());

        return  view;
    }
}

/*****************************具体引用****************************/
 // 新建Fruit
    private List<Fruit> fruitList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ll_main_layout);
        // 初始化数据源
        initFruitList();
        FruitAdapter adapter = new FruitAdapter(MainActivity.this,R.layout.ll_cust_item, fruitList);
        ListView listView = (ListView)findViewById(R.id.list_view);
        listView.setAdapter(adapter);


    }

    private void initFruitList() {
        for(int i = 0; i < 10; i++) {
            Fruit apple = new Fruit("水货" + String.valueOf(i),R.drawable.f3);
            fruitList.add(apple);
        }

    }

代码优化
优化大致两点

具体的代码

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

      View view;
      ViewHolder viewHolder;

        // 获取fruit 实例
        Fruit fruit = getItem(position);

        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.imageView = (ImageView) view.findViewById(R.id.list_item_image_view);
            viewHolder.textView = (TextView) view.findViewById(R.id.list_item_text_view);
            //将viewHolder保存在view中,便于复用
            view.setTag(viewHolder);
        } else  {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();

        }

        viewHolder.imageView.setImageResource(fruit.getImageName());

        viewHolder.textView.setText(fruit.getTitle());

        return  view;
    }

    class ViewHolder {
        ImageView imageView;
        TextView textView;
    }

item点击
思路:在Activity 使用ListView的地方给其添加监听,此监听与Button有些不同

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ll_main_layout);
        // 初始化数据源
        initFruitList();
        FruitAdapter adapter = new FruitAdapter(MainActivity.this,R.layout.ll_cust_item, fruitList);
        ListView listView = (ListView)findViewById(R.id.list_view);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String fruitName = fruitList.get(i).getTitle();
                Toast.makeText(MainActivity.this,fruitName,Toast.LENGTH_SHORT).show();
            }
        });
上一篇 下一篇

猜你喜欢

热点阅读