RecyclerView

MainActivity

package com.example.cjy.recyclerviewdemo;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
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.View;
import android.widget.Button;
import java.util.ArrayList;


public class RecyclerViewActivity extends Activity implements View.OnClickListener {

    private Button btn_add;
    private Button btn_delete;
    private Button btn_list;
    private Button btn_grid;
    private Button btn_flow;
    private RecyclerView recyclerView;
    private ArrayList<String> datas;
    private MyRecyclerViewAdapter adapter;


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


        //準備數據集合
        datas = new ArrayList<>();
        //準備數據集合
        for (int i= 0;i<100;i++){
            datas.add("Content_"+i);
        }

        //設置recyclerView適配器
        adapter = new MyRecyclerViewAdapter(RecyclerViewActivity.this,datas);
        recyclerView.setAdapter(adapter);
        //layoutManager
        recyclerView.setLayoutManager(new LinearLayoutManager(RecyclerViewActivity.this,LinearLayoutManager.VERTICAL,false));
//        recyclerView.scrollToPosition(datas.size()-1);



        //設置點擊某條的監聽





    }

    private void initView() {
        btn_add = (Button)findViewById(R.id.btn_add);
        btn_delete = (Button)findViewById(R.id.btn_delete);
        btn_list = (Button)findViewById(R.id.btn_list);
        btn_grid = (Button)findViewById(R.id.btn_grid);
        btn_flow = (Button)findViewById(R.id.btn_flow);
        recyclerView = (RecyclerView)findViewById(R.id.recyclerView);

        //設置點擊事件
        btn_add.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
        btn_list.setOnClickListener(this);
        btn_grid.setOnClickListener(this);
        btn_flow.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_add:
                adapter.adddata(0,"New_content");
                break;
            case R.id.btn_delete:
                adapter.removedata(0);
                break;
            //設置  listView 運行效果
            case R.id.btn_list:
                recyclerView.setLayoutManager(new LinearLayoutManager(RecyclerViewActivity.this,LinearLayoutManager.VERTICAL,false));

                break;
            //設置 gridView 運行效果
            case R.id.btn_grid:
                recyclerView.setLayoutManager(new GridLayoutManager(
                        RecyclerViewActivity.this,3,
                        GridLayoutManager.VERTICAL,false));

                break;
            case R.id.btn_flow:
                //設置瀑布流運行效果
                recyclerView.setLayoutManager(new StaggeredGridLayoutManager(
                        2, StaggeredGridLayoutManager.VERTICAL));

                break;
            default:
                break;

        }
    }
}

MyRecyclerViewAdapter

package com.example.cjy.recyclerviewdemo;

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.TextView;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * Created by cjy on 2017/1/6.
 */

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder> {


    private final Context context;
    private  ArrayList<String> datas;

    public MyRecyclerViewAdapter(Context context, ArrayList<String> datas) {


        this.context = context;
        this.datas = datas;

    }


    //相當於 getView 方法中創建View和ViewHolder
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView =View.inflate(context,R.layout.item_recyclerview,null);
        return new MyViewHolder(itemView);
    }

    //數據和View綁定
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        //根據位置得到對應的數據
        String data = datas.get(position);
        holder.tv_title.setText(data);

    }


    //得到總條數
    @Override
    public int getItemCount() {
        return datas.size();
    }

    //添加數據
    public void adddata(int i, String new_content) {

        datas.add(i,new_content);

        //刷新適配器
        notifyItemInserted(i);

    }

    public void removedata(int i) {
        datas.remove(i);
        //刷新適配器
        notifyItemRemoved(i);

    }


    class MyViewHolder extends RecyclerView.ViewHolder{

        private ImageView iv_icon;
        private TextView tv_title;
        public MyViewHolder(View itemView) {
            super(itemView);
            iv_icon = (ImageView)itemView.findViewById(R.id.ic_icon);
            tv_title = (TextView)itemView.findViewById(R.id.tv_title);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context,"data=="+datas.get(getLayoutPosition()),Toast.LENGTH_SHORT).show();
                }
            });

        }
    }


    //點擊RecyclerView 某條監聽
    public  interface  onItemClickListener{


        //當點
        public void onItemClick(View view,String data);


    }
}

activity.xml

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

    <TextView
        android:background="#2fbbea"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="RecyclerView"
        android:gravity="center"
        android:textSize="20sp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_add"
            android:textAllCaps="false"
            android:text="添加"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/btn_delete"
            android:textAllCaps="false"
            android:text="刪除"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/btn_list"
            android:textAllCaps="false"
            android:text="List"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/btn_grid"
            android:textAllCaps="false"
            android:text="Grid"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/btn_flow"
            android:textAllCaps="false"
            android:text="Flow"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

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

    </android.support.v7.widget.RecyclerView>e

</LinearLayout>

item_recyclerview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="5dp"
    android:background="#ffffff"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:padding="5dp"
        android:gravity="center"
        android:background="#22000000"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/ic_icon"
            android:background="@mipmap/ic_launcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <TextView
            android:layout_marginLeft="3dp"
            android:id="@+id/tv_title"
            android:text="Context"
            android:textColor="#000000"
            android:textAllCaps="false"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</RelativeLayout>

這裏寫圖片描述

發佈了44 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章