Android RecyclerView實(shí)現(xiàn)下拉列表功能
現(xiàn)在市面上的很多的應(yīng)用,都帶有下拉列表的功能,將所有選項(xiàng)都放在下拉列表中,當(dāng)用戶點(diǎn)擊選擇的時(shí)候,彈出所有的選項(xiàng),用戶選擇一項(xiàng)后,下拉列表自動(dòng)隱藏,很多下拉列表都是用ListView + PopupWindow來實(shí)現(xiàn)的,由于Google推出了替代ListView的RecyclerView,所以簡單實(shí)現(xiàn)一下:
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(); } }); //初始化數(shù)據(jù) 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); // 設(shè)置外部可以被點(diǎn)擊 mPopupWindow.setBackgroundDrawable(new BitmapDrawable()); mPopupWindow.setFocusable(true); // 使PopupWindow可以獲得焦點(diǎn) // 顯示在輸入框的左下角 mPopupWindow.showAsDropDown(mNumberEditText, 2, -5); } /** * 初始化RecyclerView,模仿ListView下拉列表的效果 */ private void initRecyclerView(){ mRecyclerView = new RecyclerView(this); //設(shè)置布局管理器 mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.setBackgroundResource(R.drawable.background); //設(shè)置Adapter mRecyclerViewAdapter = new RecyclerViewAdapter(this, mNumberList); mRecyclerView.setAdapter(mRecyclerViewAdapter); //設(shè)置點(diǎn)擊事件 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)); // 如果設(shè)置了回調(diào),則設(shè)置點(diǎn)擊事件 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); } } }
效果圖如下:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中RecyclerView實(shí)現(xiàn)多級折疊列表效果(二)
- Android中RecyclerView實(shí)現(xiàn)多級折疊列表效果(TreeRecyclerView)
- RecyclerView實(shí)現(xiàn)常見的列表菜單
- Android單個(gè)RecyclerView實(shí)現(xiàn)列表嵌套的效果
- Android使用RecyclerView實(shí)現(xiàn)自定義列表、點(diǎn)擊事件以及下拉刷新
- Android RecyclerView實(shí)現(xiàn)數(shù)據(jù)列表展示效果
- Android使用RecyclerView實(shí)現(xiàn)列表數(shù)據(jù)選擇操作
- android實(shí)現(xiàn)RecyclerView列表單選功能
- Android基于RecyclerView實(shí)現(xiàn)高亮搜索列表
- 使用RecyclerView實(shí)現(xiàn)水平列表
相關(guān)文章
Android?Studio實(shí)現(xiàn)簡單頁面跳轉(zhuǎn)的詳細(xì)教程
這篇文章主要給大家介紹了關(guān)于Android?Studio實(shí)現(xiàn)簡單頁面跳轉(zhuǎn)的詳細(xì)教程,文中通過圖文介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Android?Studio具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-01-01Android?Material組件庫日期選擇和時(shí)間選擇器的使用方法
這篇文章主要介紹了Android?Material組件庫(日期選擇和時(shí)間選擇器)基本使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-11-11全面解析Android系統(tǒng)指紋啟動(dòng)流程
這篇文章主要介紹了全面解析Android系統(tǒng)指紋啟動(dòng)流程,對Android啟動(dòng)原理感興趣的同學(xué)可以參考下2021-04-04Android線程中設(shè)置控件的值提示報(bào)錯(cuò)的解決方法
這篇文章主要介紹了Android線程中設(shè)置控件的值提示報(bào)錯(cuò)的解決方法,實(shí)例分析了textview報(bào)錯(cuò)的原因以及Handler設(shè)置來解決錯(cuò)誤的實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-06-06android4.0混淆XmlPullParser報(bào)錯(cuò)原因分析解決
今天,用android4.0在proguard-project.txt中加入 -libraryjars libs/ksoap2-android-assembly-2.6.0-jar-with-dependencies.jar這句話后,混淆時(shí)報(bào)上面的錯(cuò)誤,下面與大家分享下具體的解決方法2013-06-06Android用Fragment創(chuàng)建選項(xiàng)卡
這篇文章主要為大家詳細(xì)介紹了Android用Fragment創(chuàng)建選項(xiàng)卡的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10在Ubuntu下搭建Android開發(fā)環(huán)境
對一個(gè)程序猿來說,裝好系統(tǒng)之后的第一件事,一定是搭建開發(fā)環(huán)境,已經(jīng)安裝各種開發(fā)工具,以便之后能方便順利地進(jìn)行程序的開發(fā)。簡單的介紹下在Ubuntu環(huán)境下搭建Android開發(fā)環(huán)境,雖然基本上和在Windows下沒有太大差別,但有些細(xì)節(jié)上還是很值得注意的。2014-07-07