前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PopupWindow+RecyclerView实现上下滑动框功能

PopupWindow+RecyclerView实现上下滑动框功能

作者头像
砸漏
发布2020-11-05 15:18:41
2.2K0
发布2020-11-05 15:18:41
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

本文实例为大家分享了PopupWindow+RecyclerView实现上下滑动框功能的具体代码,供大家参考,具体内容如下

1.新建一个适配器继承自RecyclerView.Adapter

代码语言:javascript
复制
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中需要调用的方法

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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 

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档