Android RecyclerView實現下拉式清單功能_Android

來源:互聯網
上載者:User

現在市面上的很多的應用,都帶有下拉式清單的功能,將所有選項都放在下拉式清單中,當使用者點擊選擇的時候,彈出所有的選項,使用者選擇一項後,下拉式清單自動隱藏,很多下拉式清單都是用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);     }   } } 

效果圖如下:

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.