Recyclerview的简单使用

//先导入依赖
implementation 'com.android.support:recyclerview-v7:26.1.0'


package com.example.recyclerview_test;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    /**
     * 01
     */
    private Button mBtn01;
    /**
     * 01
     */
    private Button mBtn02;
    /**
     * 01
     */
    private Button mBtn03;
    /**
     * 01
     */
    private Button mBtn04;
    /**
     * 01
     */
    private Button mBtn05;
    /**
     * 01
     */
       List<String>  lists=new ArrayList<>();
    private RecyclerView rlv;
    private recyclerviewAdapter adapter;

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

    private void shuju() {
        for (int i = 0; i <100 ; i++) {
            lists.add("条目"+i);
        }
        rlv.setLayoutManager(new LinearLayoutManager(this, OrientationHelper.VERTICAL,false));
        adapter = new recyclerviewAdapter(this,lists);
        rlv.setAdapter(adapter);
        adapter.SetOnItemClickListener(new recyclerviewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                /*lists.remove(position);
                adapter.notifyDataSetChanged();*/
                Toast.makeText(MainActivity.this,"颠倒了",Toast.LENGTH_SHORT).show();
            }
        });
 
//添加自定义的动画,淡入效果
rlv.setItemAnimator(new DefaultItemAnimator());
//添加设置recyclerview条目的中间的分割线            //参数1:上下文                    //参数2:分割线方向
rlv.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
} private void initView() { mBtn01 = (Button) findViewById(R.id. btn01); mBtn01.setOnClickListener( this); mBtn02 = (Button) findViewById(R.id. btn02); mBtn02.setOnClickListener( this); mBtn03 = (Button) findViewById(R.id. btn03); mBtn03.setOnClickListener( this); mBtn04 = (Button) findViewById(R.id. btn04); mBtn04.setOnClickListener( this); mBtn05 = (Button) findViewById(R.id. btn05); mBtn05.setOnClickListener( this); rlv = findViewById(R.id. rlv); }
 
 
//HORIZONTAL代表横着

//VERTICAL代表竖着
 

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//添加一个条目
            case R.id.btn01:
                adapter.add(0,"王彭坤最帅");
                   rlv.scrollToPosition(0);
                break;
//删除第一条的条目
            case R.id.btn02:
                adapter.remv(0 );

                break;
//List  布局
            case R.id.btn03:
                rlv.setLayoutManager(new LinearLayoutManager(this, OrientationHelper.HORIZONTAL,false));
                break;
 
//Gird布局

                           

            case R.id.btn04:

  rlv.setLayoutManager(new GridLayoutManager (this,2,GridLayoutManager.VERTICAL,false)); break;

//瀑布流布局
            case R.id.btn05:
                rlv.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.HORIZONTAL));

                break;

        }
    }

    /**
     * Created by 浮生丶 on 2018/5/23 0023.
     */



}

//适配器的写法

package com.example.recyclerview_test;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

/**
 * Created by 浮生丶 on 2018/5/23 0023.
 */

public class recyclerviewAdapter   extends RecyclerView.Adapter {
    private Context context;
     private    List<String> lists;
    private  OnItemClickListener onItemClickListener;

    public recyclerviewAdapter(Context context, List<String> lists) {
        this.context = context;
        this.lists = lists;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LinearLayout.inflate(context, R.layout.item, null);
       MyViewHolder vh = new MyViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        MyViewHolder vh= (MyViewHolder) holder;
           vh.tv.setText(lists.get(position).toString());
           vh.itemView.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                         if (onItemClickListener!=null){
                             onItemClickListener.onItemClick(position);
                         }
               }
           });
    }

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

    private class MyViewHolder extends  RecyclerView.ViewHolder {

        private final ImageView iv;
        private final TextView tv;

        public MyViewHolder(View itemView) {
            super(itemView);
            iv = itemView.findViewById(R.id.iv);
            tv = itemView.findViewById(R.id.tv);
        }
    }
    public   interface OnItemClickListener {
        void onItemClick(int position);

    }
    public void SetOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }

    public  void  add( int position, String data){
        lists.add(position,data);
             notifyItemInserted(position);
    }
    public  void  remv( int position){

        notifyItemRemoved(position);
    }


}




//布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context="com.example.recyclerview_test.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
         android:orientation="horizontal"
        >
        <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:text="添加"
        android:id="@+id/btn01"
        android:layout_height="match_parent" />
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="删除"
            android:id="@+id/btn02"
            android:layout_height="match_parent" />
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="List"
            android:id="@+id/btn03"
            android:layout_height="match_parent" />
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="Grid"
            android:id="@+id/btn04"
            android:layout_height="match_parent" />
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="flow"
            android:id="@+id/btn05"
            android:layout_height="match_parent" />
    </LinearLayout>
<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:id="@+id/rlv"
    android:layout_weight="9"
    android:layout_height="0dp"></android.support.v7.widget.RecyclerView>
</LinearLayout>




//条目布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        app:srcCompat="@mipmap/ic_launcher_round" />
    <TextView
        android:layout_width="0dp"
        android:id="@+id/tv"
        android:layout_height="wrap_content"
        android:text="adsdfgkjdfsds"
        android:layout_weight="8"/>
</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值