Android RecyclerView实现下拉列表功能

现在市面上的很多的应用,都带有下拉列表的功能,将所有选项都放在下拉列表中,当用户点击选择的时候,弹出所有的选项,用户选择一项后,下拉列表自动隐藏,很多下拉列表都是用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); } } }

效果图如下:

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

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

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

Android RecyclerView实现下拉列表功能_Android

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

Android RecyclerView 上拉加载更多及下拉刷新功能的实现方法_Android

RecyclerView 已经出来很久了,但是在项目中之前都使用的是ListView,最近新的项目上了都大量的使用了RecycleView.尤其是瀑布流的下拉刷新,网上吧啦吧啦没有合适的自己总结了一哈. 先贴图上来看看:     使用RecyclerView实现上拉加载更多和下拉刷新的功能我自己有两种方式: 1.使用系统自带的Android.support.v4.widget.SwipeRefreshLayout这个控价来实现. 2.自定义的里面带有RecyleView的控件. 使用Recycl

Android RecyclerView添加上拉加载更多功能_Android

上一篇文章已经介绍了如何为RecyclerView添加FootView,在此基础上,要添加分页加载的功能其实已经很简单了. 上一篇文章地址:为RecyclerView添加FootView和HeadView 效果:(源码在文章结尾) 实现关键 在上一篇代码的基础上,只需要在onBindViewHolder(ViewHolder holder, int position)函数中添加一定修改就可以了,如下: @Override public void onBindViewHolder(ViewHold

Android RecyclerView 上拉加载更多及下拉刷新功能的实现方法

RecyclerView 已经出来很久了,但是在项目中之前都使用的是ListView,最近新的项目上了都大量的使用了RecycleView.尤其是瀑布流的下拉刷新,网上吧啦吧啦没有合适的自己总结了一哈. 先贴图上来看看: 使用RecyclerView实现上拉加载更多和下拉刷新的功能我自己有两种方式: 1.使用系统自带的Android.support.v4.widget.SwipeRefreshLayout这个控价来实现. 2.自定义的里面带有RecyleView的控件. 使用RecycleVie

Android Recyclerview实现上拉加载更多功能

在项目中使用列表的下拉刷新和上拉加载更多是很常见的功能,下拉刷新我们可以用Android自带的SwipeRefreshLayout这个很好解决.但是上拉加载更多就要去找一些框架了,刚开始的时候我找到一个Mugen的github开源框架,但是有个问题,当页面能够一次加载全部item的时候,上拉加载的功能就失效了. 这是因为当界面一次能够加载完全部item的时候,继续往上拉,Recyclerview的滑动监听,中的onScrolled方法只会在页面加载的时候调用一次,只后就不会被调用了,并且dy=0

Android RecyclerView上拉加载更多功能回弹实现代码

实现原理是使用RecyclerView的OnTouchListener方法监听滑动 在adapter里面增加两项footview 其中date.size为显示的加载条,可以自定义,date.size+1为空白的View,我们设置其高度为0 我们通过LinearLayoutManager的 findLastVisibleItemPosition判断显示的最后一条数据,如果是空白view,表示加载条已经完全展示,松开即可刷新. 回弹效果是通过在滑动时动态改变空白view的高度,达到阻尼效果 ,回弹时

Android RecyclerView仿新闻头条的频道管理功能

需要在build里添加依赖 compile 'com.android.support:recyclerview-v7:25.3.1' 布局文件activity_main <android.support.v7.widget.RecyclerView android:layout_weight="1" android:id="@+id/recyclerView_up" android:layout_width="match_parent" a

android RecyclerView:实现带header的grid

原文:RecyclerView: Grid with header  GridView和ListView有许多的相似之处,不过也有一个显著的不同:没有header和footer.现在它们两者都可以用RecyclerView实现,我想看看如何在grid上添加header. GridLayoutManager 我用GridLayoutManager创建了一个spanCount为2的RecylcerView. 注:spanCount即列数.这里GridLayoutManager的第二个参数就是span

Android仿外卖购物车功能_Android

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