在Eclipse中使用RecyclerView(二)
上一篇博客里介绍了如何在Eclipse中引入jar包,这里再具体介绍下怎么使用。
我们先看下效果图:
效果和飞猪、携程买机票页面顶部效果类似(下面这是飞猪顶部效果)
接下来我们看代码。
package com.li.recyclerviewdemo;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
public class MainActivity extends Activity {
private RecyclerView rvView;
private LinearLayoutManager layoutManager;
private RvAdapter adapter;
private List<TestBeans> beanList = new ArrayList<TestBeans>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getDatas();
initView();
}
private void initView() {
rvView = (RecyclerView) findViewById(R.id.rvView);
layoutManager = new LinearLayoutManager(this);
//设置横向展示
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
rvView.setLayoutManager(layoutManager);
//设置adapter
adapter = new RvAdapter(this, beanList);
rvView.setAdapter(adapter);
}
/**
* 测试数据
*/
private void getDatas() {
Calendar cal = Calendar.getInstance(Locale.CHINA);
SimpleDateFormat sdfMd = new SimpleDateFormat("MM-dd");
String[] dayNames = getResources().getStringArray(R.array.week_day);
for (int i = 0; i < 15; i++) {
cal.add(Calendar.DAY_OF_MONTH, 1);
Date date = cal.getTime();
String dateMd = sdfMd.format(date);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (dayOfWeek < 0) {
dayOfWeek = 0;
}
String day = dayNames[dayOfWeek];
TestBeans bean = new TestBeans();
bean.setDateMd(dateMd);
bean.setDateDay(day);
bean.setPrice("¥" + (i+1) * 100);
beanList.add(bean);
}
}
}
activity_main.xml布局文件很简单
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}"
android:orientation="vertical" >
<android.support.v7.widget.RecyclerView
android:id="@+id/rvView"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@color/grey"
android:gravity="center"
android:layout_gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_default"
android:text="我是文本内容" />
</LinearLayout>
测试的TestBeans.java有三个属性:日期、星期和价格,代码如下:
package com.li.recyclerviewdemo;
public class TestBeans {
private String dateMd;//日期
private String dateDay;//星期
private String price;//价格
public String getDateMd() {
return dateMd;
}
public void setDateMd(String dateMd) {
this.dateMd = dateMd;
}
public String getDateDay() {
return dateDay;
}
public void setDateDay(String dateDay) {
this.dateDay = dateDay;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
相对来说,adapter会复杂一点点,需要继承RecyclerView.Adapter<T>
,泛型T是adapter中自定义的ViewHolder,具体代码如下:
package com.li.recyclerviewdemo;
import java.util.List;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class RvAdapter extends RecyclerView.Adapter<RvAdapter.ViewHolder> {
private Context context;
private List<TestBeans> datas;
public RvAdapter(Context context, List<TestBeans> datas) {
super();
this.context = context;
this.datas = datas;
}
@Override
public int getItemCount() {
return datas == null ? 0 : datas.size();
}
@Override
public void onBindViewHolder(final ViewHolder holder, int pos) {
TestBeans bean = datas.get(pos);
holder.tvDate.setText(bean.getDateMd());
holder.tvDay.setText(bean.getDateDay());
holder.tvPrice.setText(bean.getPrice());
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
View v = LayoutInflater.from(arg0.getContext()).inflate(
R.layout.rv_adapter_item, arg0, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView tvDate;
TextView tvDay;
TextView tvPrice;
public ViewHolder(View itemView) {
super(itemView);
tvDate = (TextView) itemView.findViewById(R.id.tvDate);
tvDay = (TextView) itemView.findViewById(R.id.tvDay);
tvPrice = (TextView) itemView.findViewById(R.id.tvPrice);
}
}
}
adapter里面item的布局文件:
<?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"
android:gravity="center"
android:layout_gravity="center"
android:paddingLeft="@dimen/margin_default"
android:paddingRight="@dimen/margin_default"
android:orientation="vertical" >
<TextView
android:id="@+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text=""
android:textSize="13dp" />
<TextView
android:id="@+id/tvDay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text=""
android:textSize="10dp" />
<TextView
android:id="@+id/tvPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text=""
android:textSize="12dp" />
</LinearLayout>
主要代码就这么多,使用起来还是很方便的。