Android RecyclerView实现下拉列表功能_Android

现在市面上的很多的应用,都带有下拉列表的功能,将所有选项都放在下拉列表中,当用户点击选择的时候,弹出所有的选项,用户选择一项后,下拉列表自动隐藏,很多下拉列表都是用ListView + PopupWindow来实现的,由于Google推出了替代ListView的RecyclerView,所以简单实现一下:

MainActivity.java

package com.jackie.countdowntimer; 

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.PopupWindow; 

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  private EditText mNumberEditText;
  private ImageButton mSelectImageButton;
  private PopupWindow mPopupWindow;
  private RecyclerView mRecyclerView;
  private RecyclerViewAdapter mRecyclerViewAdapter;
  private List<String> mNumberList; 

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
            .setAction("Action", null).show();
      }
    }); 

    //初始化数据
    mNumberList = new ArrayList<>();
    for (int i = 0; i < 30; i++) {
      mNumberList.add("1000000" + i);
    } 

    mNumberEditText = (EditText) findViewById(R.id.number);
    mSelectImageButton = (ImageButton) findViewById(R.id.select_number);
    mSelectImageButton.setOnClickListener(this);
  } 

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.select_number:
        showSelectNumberPopupWindow();
        break;
    }
  } 

  /**
   * 弹出选择号码的对话框
   */
  private void showSelectNumberPopupWindow() {
    initRecyclerView(); 

    mPopupWindow = new PopupWindow(mRecyclerView, mNumberEditText.getWidth() - 4, 200);
    mPopupWindow.setOutsideTouchable(true);   // 设置外部可以被点击
    mPopupWindow.setBackgroundDrawable(new BitmapDrawable()); 

    mPopupWindow.setFocusable(true);    // 使PopupWindow可以获得焦点
    // 显示在输入框的左下角
    mPopupWindow.showAsDropDown(mNumberEditText, 2, -5);
  } 

  /**
   * 初始化RecyclerView,模仿ListView下拉列表的效果
   */
  private void initRecyclerView(){
    mRecyclerView = new RecyclerView(this);
    //设置布局管理器
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    mRecyclerView.setBackgroundResource(R.drawable.background); 

    //设置Adapter
    mRecyclerViewAdapter = new RecyclerViewAdapter(this, mNumberList);
    mRecyclerView.setAdapter(mRecyclerViewAdapter); 

    //设置点击事件
    mRecyclerViewAdapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClickListener() {
      @Override
      public void onItemClick(View view, int position) {
        mNumberEditText.setText(mNumberList.get(position));
        mNumberEditText.setSelection(mNumberEditText.getText().toString().length());
        mPopupWindow.dismiss();
      }
    }); 

    //添加分割线
    mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
  }
 } 

 RecyclerViewAdapter.java

package com.jackie.countdowntimer; 

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView; 

import java.util.List; 

/**
 * Created by Jackie on 2015/12/18.
 * RecyclerView Adapter
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder> {
  private OnItemClickListener mOnItemClickListener;
  private Context mContext;
  private List<String> mNumberList; 

  public RecyclerViewAdapter(Context context, List<String> numberList) {
    this.mContext = context;
    this.mNumberList = numberList;
  } 

  public interface OnItemClickListener {
    void onItemClick(View view, int position);
  } 

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

  @Override
  public RecyclerViewAdapter.RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerViewHolder holder = new RecyclerViewHolder(LayoutInflater.from(mContext).inflate(R.layout.number_item, parent, false));
    return holder;
  } 

  @Override
  public void onBindViewHolder(final RecyclerViewAdapter.RecyclerViewHolder holder, final int position) {
    holder.numberTextView.setText(mNumberList.get(position));
    // 如果设置了回调,则设置点击事件
    if (mOnItemClickListener != null) {
      holder.numberTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          mOnItemClickListener.onItemClick(holder.itemView, holder.getLayoutPosition());
        }
      });
    } 

    holder.deleteImageButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mNumberList.remove(position);
        //通知刷新
        notifyItemRemoved(position);
      }
    });
  } 

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

  public class RecyclerViewHolder extends RecyclerView.ViewHolder {
    TextView numberTextView;
    ImageButton deleteImageButton; 

    public RecyclerViewHolder(View itemView) {
      super(itemView);
      numberTextView = (TextView) itemView.findViewById(R.id.number_item);
      deleteImageButton = (ImageButton) itemView.findViewById(R.id.number_item_delete);
    }
  }
} 

 DividerItemDecoration.java

package com.jackie.countdowntimer; 

/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * limitations under the License.
 */ 

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View; 

/**
 * This class is from the v7 samples of the Android SDK. It's not by me!
 * <p/>
 * See the license above for details.
 */
public class DividerItemDecoration extends RecyclerView.ItemDecoration { 

  private static final int[] ATTRS = new int[]{
      android.R.attr.listDivider
  }; 

  public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL; 

  public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL; 

  private Drawable mDivider; 

  private int mOrientation; 

  public DividerItemDecoration(Context context, int orientation) {
    final TypedArray a = context.obtainStyledAttributes(ATTRS);
    mDivider = a.getDrawable(0);
    a.recycle();
    setOrientation(orientation);
  } 

  public void setOrientation(int orientation) {
    if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
      throw new IllegalArgumentException("invalid orientation");
    }
    mOrientation = orientation;
  } 

  @Override
  public void onDraw(Canvas c, RecyclerView parent) {
    if (mOrientation == VERTICAL_LIST) {
      drawVertical(c, parent);
    } else {
      drawHorizontal(c, parent);
    }
  } 

  public void drawVertical(Canvas c, RecyclerView parent) {
    final int left = parent.getPaddingLeft();
    final int right = parent.getWidth() - parent.getPaddingRight(); 

    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
      final View child = parent.getChildAt(i);
      android.support.v7.widget.RecyclerView v = new android.support.v7.widget.RecyclerView(parent.getContext());
      final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
      final int top = child.getBottom() + params.bottomMargin;
      final int bottom = top + mDivider.getIntrinsicHeight();
      mDivider.setBounds(left, top, right, bottom);
      mDivider.draw(c);
    }
  } 

  public void drawHorizontal(Canvas c, RecyclerView parent) {
    final int top = parent.getPaddingTop();
    final int bottom = parent.getHeight() - parent.getPaddingBottom(); 

    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
      final View child = parent.getChildAt(i);
      final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
      final int left = child.getRight() + params.rightMargin;
      final int right = left + mDivider.getIntrinsicHeight();
      mDivider.setBounds(left, top, right, bottom);
      mDivider.draw(c);
    }
  } 

  @Override
  public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
    if (mOrientation == VERTICAL_LIST) {
      outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
    } else {
      outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
    }
  }
} 

效果图如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索android
RecyclerView下拉
recyclerview下拉刷新、recyclerview下拉回弹、recyclerview下拉加载、recyclerview上拉下拉、recyclerview下拉动画,以便于您获取更多的相关知识。

时间: 2024-10-03 22:38:42

Android RecyclerView实现下拉列表功能_Android的相关文章

Android RecyclerView实现下拉列表功能

现在市面上的很多的应用,都带有下拉列表的功能,将所有选项都放在下拉列表中,当用户点击选择的时候,弹出所有的选项,用户选择一项后,下拉列表自动隐藏,很多下拉列表都是用ListView + PopupWindow来实现的,由于Google推出了替代ListView的RecyclerView,所以简单实现一下: MainActivity.java package com.jackie.countdowntimer; import android.graphics.drawable.BitmapDraw

Android仿外卖购物车功能_Android

先看看效果图: 知识点分析 效果图来看不复杂内容并没多少,值得介绍一下的知识点也就下面几个吧 - 列表标题悬停 - 左右列表滑动时联动 - 添加商品时的抛物线动画 - 底部弹出购物车清单 - 数据的同步 另外就是实现效果的时候可能会遇到的几个坑... 布局很简单直接进入代码 1:列表标题悬停 现在做项目列表什么的基本抛弃了ListView改用RecyclerView,上篇博客中的标题悬停也是使用了一个RecyclerView的开源项目sticky-headers-recyclerview,不过写

Android RecyclerView使用方法解析_Android

1.简介 RecyclerView是一种新的视图组,目标是为任何基于适配器的视图提供相似的渲染方式.它被作为ListView和GridView控件的继承者,在最新的support-V7版本中提供支持.RecyclerView架构,提供了一种插拔式的体验,高度的解耦,异常的灵活,通过设置它提供的不同LayoutManager,ItemDecoration , ItemAnimator实现令人瞠目的效果. 总之ListView和GridView能做的RecyclerView都能做,并且能实现瀑布流效

Android Camera开发手电筒功能_Android

这是一个简单的运用Android Camera开发手电筒功能,AndroidManifest.xml文件的入口是startapp,这个文件没上传上来,大家可以自己写. flashlight.java package com.android.app; import android.app.Activity; import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.os.Bun

Android实现微信支付功能_Android

开发Android APP微信支付功能,需要完成三个步骤:第一步生成预支付订单.第二步生成微信支付参数.第三步调起微信APP支付.除了需要审核通过的APP应用外,还需要获得微信支付接口权限,然后获取对应的商户号.API密钥,这两者缺一不可,并且在APP微信支付中使用 获得商户号.API密钥 在微信开放平台中查看审核通过的APP应用,是否申请支付功能,若已申请,登录微信支付|商户平台:http://pay.weixin.qq.com 查看对应的商户号.API密钥 >申请微信支付接口 >登录商户平

Android实现返回键功能_Android

记录用户点击的操作历史,使用栈数据结构,频繁的操作栈顶(添加,获取,删除),使用LinkedList 捕获用户的返回键操作,响应返回键,返回上一个界面 MainActivity.java /** * 返回键处理 */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode==KeyEvent.KEYCODE_BACK){ boolean result=MiddleManager.getInstanc

Android RecyclerView 数据绑定实例代码_Android

前言 在上一个项目里有很多很多很多很多的RecyclerView,然后我需要写很多很多很多很多的Adapter和Viewholder--多倒没问题,但是里面有很多重复的代码这就不能忍了!每一个Adapter和ViewHolder其实做的事情非常的像:视图绑定,数据绑定,点击事件分发.还有啥?既然它们做的事情都一样,为啥我们还要傻傻的继续写着重复的代码? 正文 BaseAdapter 通常我们要创建一个RecyclerView.Adapter是怎么做的? 接收一个数据列表 重写getItemCou

Android实现拍照截图功能_Android

本文将向大家展示如何拍照截图. 先看看效果图: 拍照截图有点儿特殊,要知道,现在的Android智能手机的摄像头都是几百万的像素,拍出来的图片都是非常大的.因此,我们不能像对待相册截图一样使用Bitmap小图,无论大图小图都统一使用Uri进行操作. 一.首先准备好需要使用到的Uri: private static final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg";//temp file Uri imageUri =

Android 微信图片分享功能_Android

我们都知道,通过 微信官方 分享sdk 支持图片分享,而且有多种方式.官方链接 可直接查看,不再赘述. 本文要解决的问题是,分享本地带二维码的图片给微信好友和朋友圈.朋友圈图片能够实现长按识别,给微信好友对话框的图片 在 iOS 可以正常识别,但是 Android 端却不能识别 ,为什么? 以下引用网友的回答: 经过分析和功能对比,android wechat app 中有两种图片浏览方式,图片预览,和图片本地发送后的打开查看(这个有识别动作)页面.预览图片功能中,不包含长按手势的识别功能,仔细