PopupWindow+RecyclerView实现上下滑动框功能
本文实例为大家分享了PopupWindow+RecyclerView实现上下滑动框功能的具体代码,供大家参考,具体内容如下
1.新建一个适配器继承自RecyclerView.Adapter
package aud.hik.com.audiorecordtool; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.List; public class FileListAdapter extends RecyclerView.Adapter<FileListAdapter.ViewHolder> { private final String TAG = "FileListAdapter"; private List<String> mFileList = null; private OnItemClickListener mOnItemClickListener = null; static class ViewHolder extends RecyclerView.ViewHolder{ TextView fileNameView; public ViewHolder(View view) { super(view); fileNameView = (TextView) view.findViewById(R.id.file_name); } } public FileListAdapter(List<String> fileList) { this.mFileList = fileList; } //加载item 的布局 创建ViewHolder实例 @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);//加载view布局文件 ViewHolder holder = new ViewHolder(view); return holder; } //对RecyclerView子项数据进行赋值 @Override public void onBindViewHolder(ViewHolder holder, int position) { if(null == holder) { MyLog.LOGE(TAG,"Holder is null"); return; } final String fileName= mFileList.get(position); MyLog.LOGI(TAG,"filename = "+fileName +"filenameview = "+holder.fileNameView); holder.fileNameView.setText(fileName); final int tempPosition = position; if(null != mOnItemClickListener) { holder.itemView.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { mOnItemClickListener.onClickItem(tempPosition,fileName); } }); // holder.itemView.setOnLongClickListener( new View.OnLongClickListener() { // @Override // public boolean onLongClick(View v) { // mOnItemClickListener.onLongClick(tempPosition,fileName); // return false; // } // }); } } //返回子项个数 @Override public int getItemCount() { return mFileList.size(); } public interface OnItemClickListener{ void onClickItem( int position,String fileName); // void onLongClickItem( int position,String fileName); } public void setOnItemClickListener(OnItemClickListener onItemClickListener ){ this.mOnItemClickListener = onItemClickListener; } }
2.mainactivity中需要调用的方法
private void showPopupWindow(){ View view = LayoutInflater.from(this).inflate(R.layout.pop_window,null); //初始化List数据 mVecFile = getFileName(TEXT_READ); //初始化RecyclerView RecyclerView recyslerview = (RecyclerView) view.findViewById(R.id.recycler_view); //创建LinearLayoutManager 对象 这里使用 LinearLayoutManager 是线性布局的意思 LinearLayoutManager layoutmanager = new LinearLayoutManager(this); //设置RecyclerView 布局 recyslerview.setLayoutManager(layoutmanager); //设置Adapter FileListAdapter adapter = new FileListAdapter(mVecFile); adapter.setOnItemClickListener(new FileListAdapter.OnItemClickListener() { // @Override // public void onLongClickItem(int position,String fileName) { // Toast.makeText(AudioRecordActivity.this,"onLongClick事件 您点击了第:"+position+"个Item",Toast.LENGTH_SHORT).show(); // } @Override public void onClickItem(int position,String fileName) { mTextName = fileName; mEdiTxtName.setText(mTextName); String mTextPath = TEXT_READ+"/"+mTextName; // File file = new File(path); Toast.makeText(AudioRecordActivity.this, mTextPath, Toast.LENGTH_SHORT).show(); mList = new ArrayList<>(); try { FileReader fr = new FileReader(mTextPath); BufferedReader br = new BufferedReader(fr);//以行的方式读取文件 String str = null; while(null != (str = br.readLine())) { mList.add(str); MyLog.LOGI(TAG,str); } fr.close(); br.close(); } catch(FileNotFoundException e) { e.printStackTrace(); } catch (IOException e){ e.printStackTrace(); } MyLog.LOGI(TAG,"list size="+mList.size()); if(0 != mList.size()) { mTextView.setText(mList.get(mListIndex)); } else { return; } } }); recyslerview.setAdapter(adapter); //解决android7.0以上手机的适配问题 PopupWindow popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT){ @Override public void showAsDropDown(View anchor) { if(Build.VERSION.SDK_INT >= 24){ Rect visibleFrame = new Rect(); anchor.getGlobalVisibleRect(visibleFrame); int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom; setHeight(height); } super.showAsDropDown(anchor); } @Override public void showAsDropDown(View anchor, int xoff, int yoff) { if(Build.VERSION.SDK_INT >= 24) { Rect rect = new Rect(); anchor.getGlobalVisibleRect(rect); int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom; setHeight(h); } super.showAsDropDown(anchor, xoff, yoff); } }; ColorDrawable dw = new ColorDrawable(0x10000000); popupWindow.setBackgroundDrawable(dw); popupWindow.setOutsideTouchable(true); popupWindow.setFocusable(true); popupWindow.showAsDropDown(mEdiTxtName); }
3.item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="@color/colorPrimaryDark" android:layout_margin="1dp"> <TextView android:id="@+id/file_name" 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:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)