recylerview的使用

RecyclerView介绍

RecyclerView是一个低耦合的控件,里面的很多方法要自己实现,比如点击事件等等。 下面的程序介绍RecyclerView的基本使用,实现的功能有界面浏览,添加item,删除item。

下面是程序截图

代码实现

导入recylerview类

compile 'com.android.support:recyclerview-v7:23.0.1'

新建item的布局

在layout里面新建文件item.xml,代码如下,这个代码就是recylerview里每一个小item的布局。

xml version="1.0" encoding="utf-8"?>
 xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
        android:height="100dp"
        android:gravity="center"
        android:textColor="#ffffff"
        android:textSize="20dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp"
        android:layout_margin="2dp"
        android:background="#f2aab6"
        android:id="@+id/data"/>

新建MyAdapter.java

该类实现视图和数据的绑定,并定于点击事件的接口

package com.example.whoami.recylerviewdemo;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

/**
 * Created by whoami on 2016/6/7.
 */
//实现MyAdater的继承方法
public class MyAdater extends RecyclerView.Adapter<MyAdater.ViewHolder>{
    //在构造方法里,加入数据源
    private List<String> mdata;
    private LayoutInflater inflater;
    private OnItemClickListener listener;
    public MyAdater(List<String> mdata){
        this.mdata = mdata;
    }

    //添加
    public void addData(int position,String str){
        mdata.add(position, str);
        notifyItemInserted(position);
    }
    //删除
    public void delete(int position){
        mdata.remove(position);
        notifyItemRemoved(position);
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        inflater = LayoutInflater.from(parent.getContext());
        //R.layout.item是每一个item的小布局
        View view = inflater.inflate(R.layout.item,parent,false);
        return new ViewHolder(view);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        //绑定(添加)数据。
        holder.data.setText(mdata.get(position));

    }

    @Override
    public int getItemCount() {
        return mdata.size();
    }


    //1.创建viewholder这个类,里面主要有一个view参数,和view里面布局的组件
    class ViewHolder extends RecyclerView.ViewHolder{

        private TextView data;
        public ViewHolder(View itemView) {

            super(itemView);
            data = (TextView) itemView.findViewById(R.id.data);

            data.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener!=null){
                        //重要语句!
                        int i = getLayoutPosition();
                        listener.onClick(view,i,mdata.get(i));
                    }
                }
            });
        }
    }
    interface OnItemClickListener{
        void onClick(View v,int position,String data);
    }
}

新建MyItemDecoration.java

这个类主要是实现分割线功能的,这里没有去特别实现它,可以自行了解一下,这里略过

package com.example.whoami.recylerviewdemo;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by whoami on 2016/6/7.
 */
public class MyItemDecoration extends RecyclerView.ItemDecoration{
    //自行了解
    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
    }
}

mainactivity.java代码和布局

代码

package com.example.whoami.recylerviewdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private ArrayList<String> data;
    private RecyclerView recyclerview;
    private MyAdater myAdater;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerview = (RecyclerView) this.findViewById(R.id.recyclerview);
        initData();
        myAdater = new MyAdater(data);

        recyclerview.setAdapter(myAdater);//添加adpter还要下面的代码才可以显示
        //一列,普通的listview布局
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        //3列,卡片式布局
        //recyclerview.setLayoutManager(new GridLayoutManager(this,3));
        //4列,瀑布流布局
        //recyclerview.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.HORIZONTAL));

        //添加分割线的方法
        //recyclerview.addItemDecoration(new MyItemDecoration(content,1));

        //监听器要自己实现,而且使用adpter调用,不是使用recyclerview调用
        myAdater.setOnItemClickListener(new MyAdater.OnItemClickListener() {
            @Override
            public void onClick(View v, int position, String data) {
                Toast.makeText(MainActivity.this, "position="+position, Toast.LENGTH_SHORT).show();
            }
        });
        recyclerview.setItemAnimator(new DefaultItemAnimator());//设置默认动画。


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.mymenu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.menu_add:
                myAdater.addData(1,"add data");
                break;
            case R.id.menu_delete:

                myAdater.delete(0);
                break;
        }
        return true;

    }

    private void initData(){
        data = new ArrayList<String>();
        for(int i=0;i<20;i++){
            data.add("this is data:"+i);
        }
    }
}

布局

xml version="1.0" encoding="utf-8"?>
 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="com.example.whoami.recylerviewdemo.MainActivity">

    
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    

其他xml文件

color.xml

xml version="1.0" encoding="utf-8"?>

     name="colorPrimary">#3F51B5
     name="colorPrimaryDark">#303F9F
     name="colorAccent">#FF4081

string.xml


     name="app_name">RecylerViewDemo
     name="add">添加
     name="delete">删除

res/menu/mymenu.xml

 xmlns:android="http://schemas.android.com/apk/res/android" >

    
        android:id="@+id/menu_add"
        android:orderInCategory="1"
        android:icon="@android:drawable/ic_menu_add"
        android:title="@string/add"/>
    
        android:id="@+id/menu_delete"
        android:orderInCategory="2"
        android:icon="@android:drawable/ic_menu_delete"
        android:title="@string/delete"/>
 

代码参考

视频参考:菜鸟窝

其他参考博客: Hongyang 碧海蓝天

源代码下载

百度云下载地址一

百度云下载地址二,提取码:scu2

你可能感兴趣的:(java,android)