This article shares the specific code of PopupWindow+RecyclerView to implement the up and down sliding box function for your reference. The specific content is as follows
1. Create a new adapter inherits from
package ; import .; import ; import ; import ; import ; import ; public class FileListAdapter extends <> { private final String TAG = "FileListAdapter"; private List<String> mFileList = null; private OnItemClickListener mOnItemClickListener = null; static class ViewHolder extends { TextView fileNameView; public ViewHolder(View view) { super(view); fileNameView = (TextView) (.file_name); } } public FileListAdapter(List<String> fileList) { = fileList; } //Load the layout of the item Create a ViewHolder instance @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = (()).inflate(,parent,false);//Load the view layout file ViewHolder holder = new ViewHolder(view); return holder; } // Assign the RecyclerView subkey data @Override public void onBindViewHolder(ViewHolder holder, int position) { if(null == holder) { (TAG,"Holder is null"); return; } final String fileName= (position); (TAG,"filename = "+fileName +"filenameview = "+); (fileName); final int tempPosition = position; if(null != mOnItemClickListener) { ( new () { @Override public void onClick(View v) { (tempPosition,fileName); } }); // ( new () { // @Override // public boolean onLongClick(View v) { // (tempPosition,fileName); // return false; // } // }); } } //Return the number of subitems @Override public int getItemCount() { return (); } public interface OnItemClickListener{ void onClickItem( int position,String fileName); // void onLongClickItem( int position,String fileName); } public void setOnItemClickListener(OnItemClickListener onItemClickListener ){ = onItemClickListener; } }
Methods that need to be called in
private void showPopupWindow(){ View view = (this).inflate(.pop_window,null); //Initialize List data mVecFile = getFileName(TEXT_READ); //Initialize RecyclerView RecyclerView recyslerview = (RecyclerView) (.recycler_view); //Create LinearLayoutManager object. Use LinearLayoutManager here means linear layout. LinearLayoutManager layoutmanager = new LinearLayoutManager(this); //Set RecyclerView layout (layoutmanager); //Set Adapter FileListAdapter adapter = new FileListAdapter(mVecFile); (new () { // @Override // public void onLongClickItem(int position,String fileName) { // (,"onLongClick event You clicked the following: "+position+"Items",Toast.LENGTH_SHORT).show();// } @Override public void onClickItem(int position,String fileName) { mTextName = fileName; (mTextName); String mTextPath = TEXT_READ+"/"+mTextName; // File file = new File(path); (, mTextPath, Toast.LENGTH_SHORT).show(); mList = new ArrayList<>(); try { FileReader fr = new FileReader(mTextPath); BufferedReader br = new BufferedReader(fr);//Read the file in line String str = null; while(null != (str = ())) { (str); (TAG,str); } (); (); } catch(FileNotFoundException e) { (); } catch (IOException e){ (); } (TAG,"list size="+()); if(0 != ()) { ((mListIndex)); } else { return; } } }); (adapter); //Solve the adaptation problem of mobile phones above Android 7.0 or above PopupWindow popupWindow = new PopupWindow(view, .MATCH_PARENT,.WRAP_CONTENT){ @Override public void showAsDropDown(View anchor) { if(.SDK_INT >= 24){ Rect visibleFrame = new Rect(); (visibleFrame); int height = ().getDisplayMetrics().heightPixels - ; setHeight(height); } (anchor); } @Override public void showAsDropDown(View anchor, int xoff, int yoff) { if(.SDK_INT >= 24) { Rect rect = new Rect(); (rect); int h = ().getDisplayMetrics().heightPixels - ; setHeight(h); } (anchor, xoff, yoff); } }; ColorDrawable dw = new ColorDrawable(0x10000000); (dw); (true); (true); (mEdiTxtName); }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="@color/colorPrimaryDark" android:layout_margin="1dp"> <TextView android: android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/colorWhite" android:textSize = "30sp"/> </LinearLayout>
4.pop_window.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent"> <. android: android:layout_width="match_parent" android:layout_height="match_parent"> </.> </LinearLayout>
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.