溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PopupWindow+RecyclerView實(shí)現(xiàn)上下滑動框功能

發(fā)布時(shí)間:2020-09-05 15:17:19 來源:腳本之家 閱讀:207 作者:pkx1993 欄目:移動開發(fā)

本文實(shí)例為大家分享了PopupWindow+RecyclerView實(shí)現(xiàn)上下滑動框功能的具體代碼,供大家參考,具體內(nèi)容如下

1.新建一個(gè)適配器繼承自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 的布局 創(chuàng)建ViewHolder實(shí)例
  @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子項(xiàng)數(shù)據(jù)進(jìn)行賦值
  @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;
//        }
//      });
    }
  }
 
  //返回子項(xiàng)個(gè)數(shù)
  @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中需要調(diào)用的方法

private void showPopupWindow(){
    View view = LayoutInflater.from(this).inflate(R.layout.pop_window,null);
    //初始化List數(shù)據(jù)
    mVecFile = getFileName(TEXT_READ);
    //初始化RecyclerView
    RecyclerView recyslerview = (RecyclerView) view.findViewById(R.id.recycler_view);
    //創(chuàng)建LinearLayoutManager 對象 這里使用 LinearLayoutManager 是線性布局的意思
    LinearLayoutManager layoutmanager = new LinearLayoutManager(this);
    //設(shè)置RecyclerView 布局
    recyslerview.setLayoutManager(layoutmanager);
    //設(shè)置Adapter
    FileListAdapter adapter = new FileListAdapter(mVecFile);
 
    adapter.setOnItemClickListener(new FileListAdapter.OnItemClickListener() {
//      @Override
//      public void onLongClickItem(int position,String fileName) {
//        Toast.makeText(AudioRecordActivity.this,"onLongClick事件    您點(diǎn)擊了第:"+position+"個(gè)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以上手機(jī)的適配問題
    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>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI