RecycleView的使用+自定义监听事件

最近使用了RecycleView,发下这个控件十分好用,替代了listView和GridView,包括适配器都很方便。

效果如下:

具体使用如下所示:

1 compile ‘com.android.support:recyclerview-v7:25.3.1‘

activity_main.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"
    tools:context="com.bway.recycleviewtest.MainActivity">

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

</LinearLayout>

item.xml

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

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/txt_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>

  

MainActivity

package com.bway.recycleviewtest;

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

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

public class MainActivity extends AppCompatActivity {

    private List<MyImage> mList;

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

        RecyclerView mRecycler = (RecyclerView) findViewById(R.id.recycler);
        /**
         * listView
         */
        LinearLayoutManager manager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        mRecycler.setLayoutManager(manager);
        HomeAdapter adapter = new HomeAdapter(this,mList);
        mRecycler.setAdapter(adapter);
        /**
         * GridView
         */
       /* GridLayoutManager manager1 = new GridLayoutManager(this,3,GridLayoutManager.VERTICAL,false);
        mRecycler.setLayoutManager(manager1);
        HomeAdapter adapter = new HomeAdapter(this,mList);
        mRecycler.setAdapter(adapter);*/
        /**
         * 瀑布流
         */
       /* StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        mRecycler.setLayoutManager(manager);
        HomeAdapter adapter = new HomeAdapter(this,mList);*/

        /**
         * 设置自定义的条目点击事件
         */
        adapter.setOnItemClickLisitenter(new RecycleViewLisitenter.onItemClickLisitenter() {
            @Override
            public void onItemClick(View v, int position) {
                Toast.makeText(MainActivity.this,position+"",Toast.LENGTH_SHORT).show();
            }
        });
        /**
         * 设置自定义的条目长按监听
         */
        adapter.setOnItemLongClickLisitenter(new RecycleViewLisitenter.onItemLongClickLisitenter() {
            @Override
            public void onItemLongClick(View v, int position) {
                Toast.makeText(MainActivity.this,position+"long",Toast.LENGTH_SHORT).show();
            }
        });
        mRecycler.setAdapter(adapter);

    }

    /**
     * 初始化数据
     */
    private void initData() {
        mList = new ArrayList<>();
        mList.add(new MyImage("第一张","https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1497864641&di=7a7a381bca514d830aea92d66a7f9024&src=http://pic.58pic.com/58pic/13/61/00/61a58PICtPr_1024.jpg"));
        mList.add(new MyImage("第二张","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1497874818536&di=6ddb63c3e66ac32feb5762d8d5c1fd42&imgtype=0&src=http%3A%2F%2Fpic.58pic.com%2F58pic%2F13%2F60%2F97%2F48Q58PIC92r_1024.jpg"));
        mList.add(new MyImage("第三张","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1497874818535&di=9d786693e181801447e49c37bc2bc627&imgtype=0&src=http%3A%2F%2Fi2.sanwen.net%2Fdoc%2F1608%2F704-160PQ43458.png"));
        mList.add(new MyImage("第四张","https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1497864813&di=64e84994c2f8864a69dab604d6f4d3a0&src=http://p4.gexing.com/qqpifu/20120821/1212/50330ab3e811f.jpg"));
        mList.add(new MyImage("第五张","http://up.qqjia.com/z/18/tu19139_7.jpg"));

    }
}

  

HomeAdapter
package com.bway.recycleviewtest;

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 com.bumptech.glide.Glide;

import java.util.List;

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> implements View.OnClickListener, View.OnLongClickListener {

    Context context;
    List<MyImage> mList;
    private RecycleViewLisitenter.onItemClickLisitenter onItem;
    private RecycleViewLisitenter.onItemLongClickLisitenter onLongItem;

    public HomeAdapter(Context context, List<MyImage> mList) {
        this.context = context;
        this.mList = mList;
    }

    public void setOnItemLongClickLisitenter(RecycleViewLisitenter.onItemLongClickLisitenter onLongItem){
        this.onLongItem = onLongItem;
    }
    public void setOnItemClickLisitenter(RecycleViewLisitenter.onItemClickLisitenter onItem){
        this.onItem = onItem;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = View.inflate(context, R.layout.item, null);
        view.setOnClickListener(this);
        view.setOnLongClickListener(this);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        /**
         * 给itemView设置tag
         */
        holder.itemView.setTag(position);
        MyImage myImage = mList.get(position);
        Glide.with(context).load(myImage.getUrl()).into(holder.mImage);
        holder.mTxt.setText(myImage.getTitle());

    }

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

    /**
     * item点击事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        if(onItem!=null){
            onItem.onItemClick(v, (Integer) v.getTag());
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if(onLongItem!=null){
            onLongItem.onItemLongClick(v, (Integer) v.getTag());
        }
        return true;
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        ImageView mImage;
        TextView mTxt;

        public ViewHolder(View itemView) {
            super(itemView);
            mImage = (ImageView) itemView.findViewById(R.id.image);
            mTxt = (TextView) itemView.findViewById(R.id.txt_title);
        }
    }
}

 RecycleViewLisitenter(自定义条目点击事件)

package com.bway.recycleviewtest;

import android.view.View;

public class RecycleViewLisitenter {

    /**
     * RecycleView的条目点击监听
     */
    public interface onItemClickLisitenter{
        void onItemClick(View v,int position);
    };
    /**
     * RecycleView的条目长按点击监听
     */
    public interface onItemLongClickLisitenter{
        void onItemLongClick(View v,int position);
    };
}

  

时间: 2024-11-04 22:18:24

RecycleView的使用+自定义监听事件的相关文章

Android 四种绑定监听事件的方式

1. 直接通过id查找后,绑定匿名内部类作为事件监听类.代码如下: Button loginButton = (Button) findViewById(R.id.tologin1); btn1.setOnclickListener(new OnclickListener(){ public void onClick(View v){ // 要执行的操作 } }); 这种方法有好也有不好,好的是比较直观方便,不好的是,如果按钮多了,代码看起来比较乱. 2. 实现点击事件的接口,然后一个个按钮地去

JS过渡结束监听事件及使用自定义事件解决兼容问题的方法

1.JS过渡结束监听事件 2.自定义事件解决兼容问题 原文地址:https://www.cnblogs.com/liu-web-bew/p/9345478.html

Android中Preference的使用以及监听事件分析

> 在Android系统源码中,绝大多数应用程序的UI布局采用了Preference的布局结构,而不是我们平时在模拟器中构建应用程序时使用的View布局结构,例如,Setting模块中布局.当然,凡事都有例外,FMRadio应用程序中则使用了View布局结构(可能是该应用程序是marvel公司提供的,如果由google公司做,那可说不准).归根到底,Preference布局结构和View的布局结构本质上还是大同小异,Preference的优点在于布局界面的可控性和高效率以及可存储值的简洁性(每个

miniui 给表格行添加监听事件的几种方法以及点击某列列名数据不能排序的问题

最近在使用miniui框架做开发,在做表格行的点击监听事件中发现了几个属性,都可以起到监听效果但是执行的结果却大有不同.好了废话不多说,直接上代码. <div id="pageGrid" class="mini-datagrid" style="width: 100%; height: 100%; border:0;" url="${ctx}/tAXINFO/queryRepeat" autoload="tru

SpringBoot基于注解切面监听事件

创建监听器三步骤: 1.事件(event)可以封装和传递监听器中要处理的参数,如对象或字符串,并作为监听器中监听的目标. 2.监听器(listener)具体根据事件发生的业务处理模块,这里可以接收处理事件中封装的对象或字符串. 3.事件发布者(publisher)事件发生的触发者. 代码展示: pom.xml <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspe

JAVAscript学习笔记 js句柄监听事件 第四节 (原创) 参考js使用表

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>句柄添加监听事件</title> <script type="text/javascript" src="tzy.js"></script> </head> <body>

浅谈postMessage多页面监听事件

最近做了一个Echarts和Highcharts多图多页面连动的效果,就用到postMessage 如下介绍: 最开始在最外围的页面也就是所有页面的父级页面添加postMessage监听事件以便监听下面子级页面的动态,代码: window.parent.addEventListener('message',function(e){ if(e.source != window.parent) return; var names = localStorage.getItem("toName"

ios ---键盘的监听事件

//在view将要出现的时候重载viewWillAppear方法添加通知 监听事件 keyboardWillShow:  keyboardWillHide: - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:

js html 交互监听事件学习

事件处理程序(handler): HTML事件处理程序: <input type="button" value="Click Here" onclick="showMessage();" /> <script type="text/javascript"> function showMessage() { alert('Clicked!'); } JavaScript指定事件处理程序: <inpu