RecyclerView

main布局

<?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"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.dash.project_1511a.MainActivity">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:text="列表"
            android:onClick="lieBiao"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />

        <Button
            android:text="表格"
            android:onClick="biaoGe"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />

        <Button
            android:text="瀑布流"
            android:onClick="waterWall"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <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>

</LinearLayout>

内容布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/item_text"
        android:layout_width="match_parent"
        android:layout_gravity="center_horizontal"
        android:gravity="center"
        android:layout_height="wrap_content" />

</LinearLayout>

MainActivity

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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.view.ViewGroup;
import android.widget.Toast;

import com.dash.project_1511a.inter.OnItemListner;

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

/**
 * 1.展示列表
 * 2.展示gridview表格
 * 3.瀑布流
 */
public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private MyListAdapter myListAdapter;

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

        recyclerView = findViewById(R.id.recycler_view);

        //数据
        final List<String> list = new ArrayList<>();
        for (int i = 0;i<1000;i++){
            list.add("这是条目"+i);
        }

        //必须制定布局管理器
        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this, OrientationHelper.HORIZONTAL,true));

        //设置是适配器
        myListAdapter = new MyListAdapter(MainActivity.this, list);

        recyclerView.setAdapter(myListAdapter);


        //条目的点击事件和长按事件需要自己实现
        myListAdapter.setOnItemListner(new OnItemListner() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(MainActivity.this,"点击了:"+list.get(position),Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(int position) {
                Toast.makeText(MainActivity.this,"长按了:"+list.get(position),Toast.LENGTH_SHORT).show();

            }
        });

    }

    /**
     * 点击事件....listView效果默认的是竖直的,,,可以是横向的
     * @param view
     */
    public void lieBiao(View view) {
        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
    }

    public void biaoGe(View view) {


        //recyclerView.setLayoutManager(new GridLayoutManager(MainActivity.this,3));

        recyclerView.setLayoutManager(new GridLayoutManager(this,3,OrientationHelper.HORIZONTAL,false));
    }

    /**
     * 瀑布流
     * @param view
     */
    public void waterWall(View view) {

        //竖直的瀑布流效果
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,OrientationHelper.VERTICAL));


    }
}

MyListAdapter


import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.dash.project_1511a.holder.MyListHolder;
import com.dash.project_1511a.inter.OnItemListner;

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

/**
 * Created by Dash on 2017/12/25.
 */
public class MyListAdapter extends RecyclerView.Adapter<MyListHolder> {

    private List<String> list;
    private Context context;
    private List<Integer> heightList = new ArrayList<>();
    private OnItemListner onItemListner;

    public MyListAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;

        //创建随机数
        for (int i = 0;i<list.size();i++){
            heightList.add((int) (100+Math.random()*200));
        }
    }

    /**
     * onCreateViewHolder 创建一个viewHolder的时候调用的方法....需要在这个方法里面创建viewHolder
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public MyListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //条目的视图
        View view = View.inflate(context,R.layout.list_item_layout,null);

        //根据展示的条目的视图 创建viewHolder
        MyListHolder myListHolder = new MyListHolder(view);

        return myListHolder;
    }

    /**
     * onBindViewHolder....绑定之后是做一些赋值的操作,,,,textView设置文本,imageView设置图片,button,checkBox设置监听事件
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(MyListHolder holder, final int position) {


        ViewGroup.LayoutParams layoutParams = holder.textView.getLayoutParams();
        layoutParams.height = heightList.get(position);
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        holder.textView.setLayoutParams(layoutParams);

        //设置随机的背景颜色
        holder.textView.setBackgroundColor(Color.rgb((int) (100+ Math.random()*155),(int) (100+ Math.random()*155),(int) (100+ Math.random()*155)));

        //设置文本
        holder.textView.setText(list.get(position));


        //设置监听事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //触发的是监听事件的执行,,,而不是直接做处理
                onItemListner.onItemClick(position);
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {

                onItemListner.onItemLongClick(position);
                //解决点击和长按的冲突
                return true;
            }
        });
    }

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

    /**
     * 创建的是设置事件的方法
     * @param onItemListner
     */
    public void setOnItemListner(OnItemListner onItemListner) {
        this.onItemListner = onItemListner;
    }
}

OnItemListner

public interface OnItemListner {

    void onItemClick(int position);
    void onItemLongClick(int position);

}

MyListHolder



import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;



public class MyListHolder extends RecyclerView.ViewHolder {

    public TextView textView;

    public MyListHolder(View itemView) {
        super(itemView);


        //holder里面是需要拿到视图中的控件
        textView = itemView.findViewById(R.id.item_text);

    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值