ListView实现与点击事件

book_item.xml




    

    
        
        
        

        
    

适配器:BookAdapter.java

package com.lzc.androidtest.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
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 com.lzc.androidtest.R;
import com.lzc.androidtest.model.Book;

import java.util.List;

/**
 * Created by User on 2018/3/26.
 */

public class BookAdapter extends ArrayAdapter {

    private int resourceId;

    public BookAdapter(Context context, int resource, List objects) {
        super(context, resource, objects);
        resourceId = resource;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Book book = getItem(position);
        View view;
        ViewHolder viewHolder;
        if(convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);

            viewHolder = new ViewHolder();

            viewHolder.imageView = (ImageView)view.findViewById(R.id.book_item_image);
            viewHolder.name = (TextView)view.findViewById(R.id.book_item_name);
            viewHolder.author = (TextView)view.findViewById(R.id.book_item_author);
            viewHolder.price = (TextView)view.findViewById(R.id.book_item_price);
            viewHolder.comment = (TextView)view.findViewById(R.id.book_item_comment);

            view.setTag(viewHolder);//将viewHolder存储在view中
        }else{
            view = convertView;
            viewHolder = (ViewHolder)view.getTag(); //重新获取viewHolder
        }

        viewHolder.imageView.setImageResource(book.getImageId());
        viewHolder.name.setText(book.getName());
        viewHolder.author.setText(book.getAuthor());
        viewHolder.price.setText("¥ "+book.getPrice());
        viewHolder.comment.setText(String.valueOf(book.getComment()));

        return view;
    }

    class ViewHolder{
        ImageView imageView;
        TextView name;
        TextView author;
        TextView price;
        TextView comment;
    }
}

activity_list_view.xml




    


 
  

ListViewActivity.java

package com.lzc.androidtest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.lzc.androidtest.adapter.BookAdapter;
import com.lzc.androidtest.model.Book;

import java.util.ArrayList;
import java.util.List;

public class ListViewActivity extends AppCompatActivity {

    private List bookList = new ArrayList<>();
    private BookAdapter adapterListView;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        initData();
    }
    private void initData() {
        for (int i = 0;i<40;i++){
            Book book = new Book(R.mipmap.book,"书名"+(i+1)+",书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名书名v","作者"+(i+1),19.1,10000);
            bookList.add(book);
        }
        adapterListView = new BookAdapter(ListViewActivity.this,R.layout.book_item,bookList);
        listView = (ListView)findViewById(R.id.list_view_book);
        listView.setAdapter(adapterListView);
        //设置listview点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                Book book = bookList.get(i);
                Toast.makeText(ListViewActivity.this,book.toString(),Toast.LENGTH_LONG).show();
            }
        });

    }
}

运行结果

ListView实现与点击事件_第1张图片

点击效果

ListView实现与点击事件_第2张图片



你可能感兴趣的:(Android)