RecyclerView实现横向滚动
Fruit Class
public class Fruit { private int fruitImageId; //图片的ID private String fruitName; //水果名字 public Fruit(String fruitName,int fruitImageId) { //构造函数进行初始化 ,用于创建新的对象的value this.fruitImageId = fruitImageId; this.fruitName = fruitName; } public int getFruitImageId() { //获得对象(水果)ID(例如R.id.fruitID) return fruitImageId; } public String getFruitName() { //获得对象的名字(字符串类型) return fruitName; } }
FruitAdapter Class
public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder>{
List<Fruit> mfruitList; public FruitAdapter(List<Fruit> mfruitList) { //对水果列表集合进行初始化 , 构造函数 this.mfruitList = mfruitList; //把要展示的数据源传进来,并赋值给mfruitList,后续的工作都基于这个数据源 } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { //创建HoldView的实例,并把实例返回 View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit,parent,false); //将fruit的布局加载进来 ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { Fruit fruit = mfruitList.get(position); //通过positon,获得当前对象Fruit的实例 holder.fruitImage.setImageResource(fruit.getFruitImageId()); //设置实例的图片 holder.fruitName.setText(fruit.getFruitName()); //设置实例的名字 } @Override public int getItemCount() { return mfruitList.size();//获得对象的数量,返回数据源的长度. } static class ViewHolder extends RecyclerView.ViewHolder { ImageView fruitImage; TextView fruitName; public ViewHolder(View View) { //view是RecyclerView子项的最外层布局,通过view findViewById()方法,
//就能找到外层的fruit_image和fruit_name的id super(View); fruitImage = (ImageView) View.findViewById(R.id.fruit_image); fruitName = (TextView) View.findViewById(R.id.fruit_name); } }
}MainActivity Class
public class MainActivity extends AppCompatActivity { List<Fruit> fruitlist = new ArrayList<Fruit>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initFruits(); //初始化水果数据 RecyclerView recyclerView = (RecyclerView) findViewById(recycler_view); LinearLayoutManager layoutManager = new LinearLayoutManager(this); //线性布局的意思,实现ListView功能
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL ); //设置布局横向排列recyclerView.setLayoutManager(layoutManager); //layoutmanager用于指定recyclerview的布局方式 FruitAdapter adapter = new FruitAdapter(fruitlist);//将水果的数据传入到FruitAdapter中 recyclerView.setAdapter(adapter); //完成适配器设置,建立关联 } void initFruits(){ for(int i=0;i<2;i++){ Fruit apple = new Fruit("Apple",R.drawable.apple); fruitlist.add(apple); Fruit banana = new Fruit("Banana",R.drawable.banana); fruitlist.add(banana); Fruit cherry = new Fruit("Cherry",R.drawable.cherry); fruitlist.add(cherry); Fruit grape = new Fruit("Grape",R.drawable.grape); fruitlist.add(grape); Fruit mango = new Fruit("Mango",R.drawable.mango); fruitlist.add(mango); Fruit orange = new Fruit("Orange",R.drawable.orange); fruitlist.add(orange); Fruit pear = new Fruit("Pear",R.drawable.pear); fruitlist.add(pear); Fruit pineapple = new Fruit("Pineapple",R.drawable.pineapple); fruitlist.add(pineapple); Fruit strawberry = new Fruit("Strawberry",R.drawable.strawberry); fruitlist.add(strawberry); Fruit watermelon = new Fruit("Watermelon",R.drawable.watermelon); fruitlist.add(watermelon); } }}
activity_main.xml
<?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.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout>
fruit.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="100dp" //让每个水果的文字所占的空间一样大,美观 android:layout_height="wrap_content" android:orientation="vertical" //改成垂直方向排列 > <ImageView android:id="@+id/fruit_image" android:src="@drawable/apple" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/fruit_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="苹果" android:layout_gravity="center_horizontal" android:layout_marginTop="10dp" android:textColor="#000" /> </LinearLayout>