我的Adaper规范

android开发中要用到Adapter的地方很多。

那么,要怎么写才能使Adapter看起来比较的清爽呢?

下面是我个人比较喜欢的方式。大神就可以飘过了,希望对初学者有点帮助。

/**
 * Created by ubuntu on 13-6-4.
 * 一个封装良好的Adapter示例
 */
public class PersonAdapter extends BaseAdapter{

    private List<Person> persons;

    private LayoutInflater inflater;

    public PersonAdapter(Activity activity, List<Person> persons){
        this.persons = persons;
        if (this.persons==null){
            this.persons = new ArrayList<Person>();
        }
        inflater = activity.getLayoutInflater();
    }

    @Override
    public int getCount() {
        return persons.size();
    }

    @Override
    public Person getItem(int i) {
        return persons.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ViewHolder viewHolder;
        if (view == null){
            view = inflater.inflate(R.layout.persion_item, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) view.getTag();
        }
        // 绑定model中的数据到View中
        viewHolder.bindData(persons.get(i));
        return view;
    }

    private class ViewHolder{
        public ViewHolder(View view){
            if (view == null){
                throw new IllegalArgumentException("view cannot be null");
            }
            // 在此查找控件
            tv_id = (TextView) view.findViewById(R.id.tv_id);
            tv_name = (TextView) view.findViewById(R.id.tv_name);
            tv_age = (TextView) view.findViewById(R.id.tv_age);
        }

        /** 提供方法用于绑定View-Model */
        public void bindData(Person person){
            tv_id.setText(String.valueOf(person.id));
            tv_name.setText(person.name);
            tv_age.setText(String.valueOf(person.age));
        }

        public TextView tv_id;
        public TextView tv_name;
        public TextView tv_age;
    }
}

Person实体保持就是一个简单的实体:

public class Person {
    public int id;
    public String name;
    public int age;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、好的,定义一个书籍类: ``` public class Book { private String title; private int image; public Book(String title, int image) { this.title = title; this.image = image; } public String getTitle() { return title; } public int getImage() { return image; } } ``` 2、创建一个Activity: ``` public class BookListActivity extends AppCompatActivity { private RecyclerView recyclerView; private BookListAdapter adapter; private List<Book> bookList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_book_list); recyclerView = findViewById(R.id.recyclerView); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); bookList = new ArrayList<>(); createBookList(); adapter = new BookListAdapter(bookList); recyclerView.setAdapter(adapter); } private void createBookList() { for (int i = 1; i <= 20; i++) { String title = "Book " + i; int image = R.drawable.book_cover; Book book = new Book(title, image); bookList.add(book); } } } ``` 3、在activity中创建一个方法来添加20本书的信息: 我在上面的代码中已经添加了该方法,名字就是createBookList(),在这个方法里面就可以创建20本书的信息。 4、定义RecyclerView每项所用的布局,布局用CardView来显示书籍信息。在res/layout目录下创建一个新的布局文件book_item.xml: ``` <?xml version="1.0" encoding="utf-8"?> <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" app:cardCornerRadius="8dp" app:cardElevation="2dp" app:cardUseCompatPadding="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/imageView" android:layout_width="96dp" android:layout_height="96dp" android:padding="8dp" android:src="@drawable/book_cover" android:scaleType="centerCrop"/> <TextView android:id="@+id/titleTextView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="20sp" android:textStyle="bold" android:textColor="@color/black" android:padding="16dp" /> </LinearLayout> </androidx.cardview.widget.CardView> ``` 这个布局文件包含一个LinearLayout,里面有一个ImageView显示书籍的图片,一个TextView显示书籍的标题。这个布局文件中使用了CardView来把每一项(即一个书籍)进行包含,这样每个书籍的信息就看起来非常整洁漂亮。 最后,在Adaper中去实现我们定义的布局文件book_item.xml,实现代码如下: ``` public class BookListAdapter extends RecyclerView.Adapter<BookListAdapter.BookViewHolder> { private List<Book> bookList; public class BookViewHolder extends RecyclerView.ViewHolder { public TextView titleTextView; public ImageView imageView; public BookViewHolder(View view) { super(view); titleTextView = view.findViewById(R.id.titleTextView); imageView = view.findViewById(R.id.imageView); } } public BookListAdapter(List<Book> bookList) { this.bookList = bookList; } @Override public BookViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.book_item, parent, false); return new BookViewHolder(itemView); } @Override public void onBindViewHolder(BookViewHolder holder, int position) { Book book = bookList.get(position); holder.titleTextView.setText(book.getTitle()); holder.imageView.setImageResource(book.getImage()); } @Override public int getItemCount() { return bookList.size(); } } ``` 这里使用一个ViewHolder来实现RecyclerView的重用,同时在onBindViewHolder()方法里面把每一项的信息进行展示。 以上就是实现RecyclerView展示书籍信息所需的全部代码,可以根据需求进行调整和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值