常用控件(二) : RecyclerView

来源:互联网 发布:搜狐邮箱 imap 端口 编辑:程序博客网 时间:2024/06/05 11:52


这个控件的使用和Listview差不多,多了一个叫LayoutManager的概念,就是一个让开发者自定义布局

格式的东东,你是要线性布局的,还是要网格布局的,还是要流式布局的。使用RecyclerView控件 ,
我们可以做到listview ,gridview ,流式布局的效果。 功能比较强大,目前还不是很熟,先写上基本用法。
具体的细节功能可以去github上找细节的功能,例如动画。


基本使用注意:

需要导入v7包。

目前没有条目的事件处理功能,需要我们自己来写。也不是很难。

很可惜的是么有下拉刷新和上拉刷新,自动滑动到底部加载功能。

可以拖拽的RecyclerView:

https://github.com/bboyfeiyu/android-tech-frontier/blob/master/issue-18/%E6%8B%96%E6%8B%BDRecyclerView.md


下面是基本的使用介绍:

首先在布局中使用:

<RelativeLayout 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"    android:background="#FFFFFF"    tools:context="com.example.manzuo.MainActivity" >    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="updateItem"         android:padding="5dp"       android:id="@+id/btn"       android:textColor="#000000"        android:text="更新" />       <android.support.v7.widget.RecyclerView        android:id="@+id/recyclerview"         android:divider="#ffff0000"           android:dividerHeight="10dp"           android:layout_below="@+id/btn"        android:layout_width="match_parent"        android:layout_height="match_parent" /></RelativeLayout>



其次是条目文件的定义  :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="#3A3A3A"    tools:context="com.example.manzuo.MainActivity" >    <TextView        android:id="@+id/textview"        android:layout_width="fill_parent"        android:layout_height="wrap_content"                android:background="#FFFFFF"        android:textSize="20sp" />    <!-- 分割线 -->    <TextView        android:layout_width="fill_parent"        android:layout_height="1dp"         android:background="#EEEEEE"/>   <TextView        android:layout_width="fill_parent"        android:layout_height="0.1dp"        android:background="#F9F9F9" /></RelativeLayout>

本例子 模拟一个listview,效果如下:


最后是现在代码中编写:

package com.example.manzuo;import java.util.ArrayList;import java.util.Random;import android.app.Activity;import android.os.Bundle;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.support.v7.widget.RecyclerView.ViewHolder;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnLongClickListener;import android.view.ViewGroup;import android.widget.ListView;import android.widget.RelativeLayout;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {int itemcount = 5;RecyclerView recyclerview;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ArrayList<String> data = new ArrayList<String>();for(int i=0;i<itemcount;i++){data.add(String.valueOf(i));} recyclerview =  (RecyclerView) findViewById(R.id.recyclerview);//线性布局的 布局管理器LinearLayoutManager manager = new LinearLayoutManager(this);manager.setOrientation(LinearLayoutManager.VERTICAL);//manager.setOrientation(LinearLayoutManager.HORIZONTAL);//网格布局 的 布局管理器//GridLayoutManager manager = new GridLayoutManager(this, 4);//manager.setOrientation(GridLayoutManager.HORIZONTAL);//横向滑动的网格布局//manager.setOrientation(GridLayoutManager.VERTICAL); //竖向滑动的网格布局//瀑布流式的布局//参数的意思 是 :10列//LayoutManager manager = new StaggeredGridLayoutManager(10, StaggeredGridLayoutManager.VERTICAL);//LayoutManager manager = new StaggeredGridLayoutManager(10, StaggeredGridLayoutManager.HORIZONTAL);//必须设置的recyclerview.setLayoutManager(manager);Myadapter aMyadapter = new Myadapter(data);aMyadapter.setItemEvent(new MyOnItemEvent() {@Overridepublic void onLongItemClick(View v, int position) {RelativeLayout r = (RelativeLayout) v;TextView textview = (TextView) r.getChildAt(0);Toast.makeText(MainActivity.this, "item long click:"+v +",点击位置:"+position+",内容:"+textview.getText().toString(), 0).show();}@Overridepublic void onItemClick(View v, int position) {RelativeLayout r = (RelativeLayout) v;TextView textview = (TextView) r.getChildAt(0);Toast.makeText(MainActivity.this, "item click:"+v +",点击位置:"+position+",内容:"+textview.getText().toString(), 0).show();}});recyclerview.setAdapter(aMyadapter);}class Myadapter extends RecyclerView.Adapter<MyViewHolder>{ArrayList<String> data ;//事件处理接口MyOnItemEvent itemEvent;public void setItemEvent(MyOnItemEvent itemEvent) {this.itemEvent = itemEvent;}public Myadapter(ArrayList<String> data){this.data = data;}@Overridepublic int getItemCount() {return this.data.size();}/** * 给条目view设置数据 */@Overridepublic void onBindViewHolder(final MyViewHolder holder, final int position) {holder.tv.setText(data.get(position));//单击事件 注册holder.itemView.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {itemEvent.onItemClick(holder.itemView, position);}}); //长按事件 注册holder.itemView.setOnLongClickListener(new OnLongClickListener() {@Overridepublic boolean onLongClick(View v) {itemEvent.onLongItemClick(holder.itemView, position);return false;}});}/** * 创建条目view */@Overridepublic MyViewHolder onCreateViewHolder(ViewGroup parent, final int position) {//创建viewfinal View item = LayoutInflater.from(MainActivity.this).inflate(R.layout.recycleview_item, parent,false);//创建view的缓存holderMyViewHolder holder = new MyViewHolder(item);  return holder;}}public interface MyOnItemEvent{public void onItemClick(View v,int position);public void onLongItemClick(View v,int position);}/** * 封装条目的所有view组件 * @author Administrator * */class MyViewHolder extends ViewHolder{public  TextView tv;public MyViewHolder(View itemView) {super(itemView);tv = (TextView) itemView.findViewById(R.id.textview);}}/** * 更新条数的数据 * @param view */public void updateItem(View view){Myadapter adapter = (Myadapter) recyclerview.getAdapter();adapter.data.addAll(newData());adapter.notifyDataSetChanged();}private ArrayList<String> newData() {ArrayList<String> data = new ArrayList<String>();Random r = new Random();for(int i=0;i<itemcount;i++){int newInt = r.nextInt(100);data.add(String.valueOf(newInt)+"更新后数据");}return data;}}




0 0