RecyclerView的使用(3种视图切换)

大家可以通过导入support-v7对其进行使用

 compile 'com.android.support:recyclerview-v7:23.2.1-alpha1'

这里实现ListView,GridView,Staggered(瀑布流)的切换
只是简单的实现,还没有添加和删除功能。

示例图1
RecyclerView的使用(3种视图切换)_第1张图片
示例图2
RecyclerView的使用(3种视图切换)_第2张图片
示例图3
RecyclerView的使用(3种视图切换)_第3张图片

Activity

/**
 * Created by zzb on 2016/3/30.
 */
public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecycleview;
    private int[] imgs={R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,
            R.drawable.a5,R.drawable.a6};
    private String[] mDatas = {
            "十几年相思加两斤黄酒,才把这句喜欢说出口。",
            "无人与我立黄昏,无人问我粥可温",
            "与这个世界交手多年,你是否光彩依旧 兴致盎然?",
            " 瀑布的水逆流而上 \n" +
                    "蒲公英种子从远处飘回,聚成伞的模样, \n" +
                    "太阳从西边升起,\n" +
                    "落向东方。  ",
            "那时我们有梦,\n" +
                    "关于文学, \n" +
                    "关于爱情, \n" +
                    "关于穿越世界的旅行。\n" +
                    "如今我们深夜饮酒, \n" +
                    "杯子碰到一起, \n" +
                    "都是梦破碎的声音。\n" +
                    "\n" +
                    "——北岛《波兰来客》 ",
            "很怀念我们刚认识那会儿,大家都有些拘谨和真诚。 ",
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        initviews();
        mRecycleview.setAdapter(new SimpleAdapter(this, mDatas, imgs));
        //设置recycleview的布局管理
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
       mRecycleview.setLayoutManager(linearLayoutManager);  
        mRecycleview.setItemAnimator(new DefaultItemAnimator());

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    private void initviews() {
        mRecycleview= (RecyclerView) findViewById(R.id.recyclerview);
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id)
         {
             case R.id.action_settings:
                break;
             case R.id.listview:
                 mRecycleview.setLayoutManager(new LinearLayoutManager(this));
                 break;
             case R.id.gridview:
             //列数
                 mRecycleview.setLayoutManager(new GridLayoutManager(this,2));
                 break;
             case R.id.stagger:
             //列数,垂直方向
                 mRecycleview.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL));
                 break;
             default:break;
        }

        return super.onOptionsItemSelected(item);
    }
}

Adapter

/**
 * Created by zzb on 2016/3/30.
 */
public class SimpleAdapter extends RecyclerView.Adapter<MyViewHolder> {
    private LayoutInflater mInflater;
    private Context mContext;
    private String[] mDatas;
    private int[] imgs;

    public SimpleAdapter(Context context, String[] datas,int[] pic) {
        this.mContext = context;
        this.mDatas = datas;
        this.imgs=pic;
        mInflater = LayoutInflater.from(context);

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_textview, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    /**
     *
     * @param holder
     * 专门用来绑定ViewHolder里的控件和数据源中position位置的数据。
     */
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.mImageView.setImageResource(imgs[position]);
        holder.tv.setText(mDatas[position]);
    }

    @Override
    public int getItemCount() {
        return mDatas==null ? 0 : mDatas.length;
    }
}

 class MyViewHolder extends RecyclerView.ViewHolder {
    TextView tv;
    CardView cd;
     ImageView mImageView;
    public MyViewHolder(View itemView) {
        super(itemView);
        tv = (TextView) itemView.findViewById(R.id.tv_text);
        cd = (CardView) itemView.findViewById(R.id.cv_item);
        mImageView= (ImageView) itemView.findViewById(R.id.iv_pic);
    }
}

Item_view.xml

如果遇到CardView 提示没有,那就手动添加,类似RecycleView的

 compile 'com.android.support:cardview-v7:23.2.1'

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_margin="8dp"
    android:id="@+id/cv_item"
    android:foreground="?android:attr/selectableItemBackground"
    card_view:cardCornerRadius="4dp"
    card_view:cardBackgroundColor="#795548"
    card_view:cardElevation="4dp"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <ImageView
            android:id="@+id/iv_pic"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_weight="1"
            />
        <TextView
            android:id="@+id/tv_text"
            android:padding="20dp"
            android:textColor="#ffffff"
            android:textAlignment="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    LinearLayout>

android.support.v7.widget.CardView>

RecycleView.xml:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.it.zzb.recycledemo.MainActivity"
    tools:showIn="@layout/activity_main">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >android.support.v7.widget.RecyclerView>
RelativeLayout>

menu.xml:

"http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.it.zzb.recycledemo.MainActivity">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />
    <item
        android:id="@+id/listview"
        android:orderInCategory="100"
        android:title="listtview"
        app:showAsAction="never" />
    <item
        android:id="@+id/gridview"
        android:orderInCategory="100"
        android:title="gridview"
        app:showAsAction="never" />

    <item
        android:id="@+id/stagger"
        android:orderInCategory="100"
        android:title="staggered"
        app:showAsAction="never" />

参考资料

接下来可以参考这个实现添加、删除

Android RecyclerView 使用完全解析 体验艺术般的控件

你可能感兴趣的:(android)