码迷,mamicode.com
首页 > 其他好文 > 详细

【框架】recyclerView

时间:2016-05-03 17:56:56      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

 

<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>
    
<ImageView
    android:id="@+id/iv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<TextView
    android:id="@+id/username"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/add_data"
        android:title="addData"/>
</menu>
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private LayoutInflater inflater;
    private Context context;
    private List<UserEntity> list;
    //2.声明一个接口变量
    private OnItemClickListener onItemClickListener;

    public MyAdapter(Context context, List<UserEntity> list) {
        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    //创建ViewHolder,类似于BaseAdapter类中的getView方法的if(){}else{}中的代码
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.recycler_view_item, null);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    //绑定数据,类似于getView方法中赋值的代码
    //建议点击事件不要写在这里
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        UserEntity userEntity = list.get(position);
        final MyViewHolder holder1 = (MyViewHolder) holder;
        holder1.username.setText(userEntity.getUsername());
        holder1.userface.setImageResource(userEntity.getImg());
    }

    //返回item的总数
    @Override
    public int getItemCount() {
        return list.size();
    }

    //3.初始化接口变量
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    //1.定义接口
    public interface OnItemClickListener {
        public void onItemClickListener(int position);

        public void onItemLongClickListener(int position);
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        private ImageView userface;
        private TextView username;

        //参数表示每一个item的布局
        public MyViewHolder(View itemView) {
            super(itemView);
            userface = (ImageView) itemView.findViewById(R.id.iv);
            username = (TextView) itemView.findViewById(R.id.username);
            //给每一个item设置点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //getLayoutPosition表示获取当前所点击item的位置
//                    Toast.makeText(context, "itemViewClick:" + getLayoutPosition(), Toast.LENGTH_SHORT).show();
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClickListener(getLayoutPosition());
                    }
                }
            });
            //给itemView设置一个长按事件
            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
//                    Toast.makeText(context, "itemViewLongClick:" + getLayoutPosition(), Toast.LENGTH_SHORT).show();
                    //长按时删除一个item
//                    list.remove(getLayoutPosition());
                    //并不会刷新整个列表,提高性能,同时带有删除动画
//                    notifyItemRemoved(getLayoutPosition());
                    //调用该方法会将整个列表刷新,性能低,且没有删除动画
//                    notifyDataSetChanged();
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemLongClickListener(getLayoutPosition());
                    }
                    return true;
                }
            });
        }
    }
}
public class MainActivity extends AppCompatActivity {

    private List<UserEntity> list;
    private MyAdapter adapter;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        initData();
        /**************线性布局管理器**************/
        //创建一个线性布局管理器,显示效果类似ListView
//        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(this);
        //创建一个线性布局管理器,第二个参数表示滚动的方向,第三个参数表示是否反转
//        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true);
        //设置布局管理器
//        recyclerView.setLayoutManager(linearLayoutManager2);
        /*********GridLayout布局管理器************/
//        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
//        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2, GridLayoutManager.HORIZONTAL, true);
//        recyclerView.setLayoutManager(gridLayoutManager);
        /************瀑布流布局管理器***********/
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(staggeredGridLayoutManager);
        adapter = new MyAdapter(this, list);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
            //单击事件
            @Override
            public void onItemClickListener(int position) {
                Toast.makeText(MainActivity.this, "position:" + position, Toast.LENGTH_SHORT).show();
            }

            //长按事件
            @Override
            public void onItemLongClickListener(int position) {
                list.remove(position);
                adapter.notifyItemRemoved(position);
            }
        });
        recyclerView.addItemDecoration(new DividerGridItemDecoration(this));
//        recyclerView.addItemDecoration(new DividerItemDecoration(this, StaggeredGridLayoutManager.HORIZONTAL));
        //适用于所有
//        recyclerView.addItemDecoration(new SpacesItemDecoration(2));
    }

    private void initData() {
        list = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            list.add(new UserEntity(R.mipmap.ic_launcher, "lisi:" + i));
        }
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.add_data:
                list.add(0, new UserEntity(R.mipmap.ic_launcher, "张三"));
                adapter.notifyItemInserted(0);
                //手动设置滚动到第0条数据
                recyclerView.scrollToPosition(0);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}

 

【框架】recyclerView

标签:

原文地址:http://www.cnblogs.com/anni-qianqian/p/5455719.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!