PopupWindow+RecyclerView realizes the function of sliding box up and down

  • 2021-09-20 21:29:46
  • OfStack

This article example for everyone to share PopupWindow + RecyclerView to achieve up and down sliding box function of the specific code, for your reference, the specific content is as follows

1. Create a new adapter that inherits from 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;
  }
 
  // Loading item  Layout of   Create ViewHolder Instances 
  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);// Loading view Layout file 
    ViewHolder holder = new ViewHolder(view);
    return holder;
  }
 
  // Right RecyclerView Subitem data assignment 
  @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;
//        }
//      });
    }
  }
 
  // Returns the number of subitems 
  @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. Methods to be called in mainactivity


private void showPopupWindow(){
    View view = LayoutInflater.from(this).inflate(R.layout.pop_window,null);
    // Initialization List Data 
    mVecFile = getFileName(TEXT_READ);
    // Initialization RecyclerView
    RecyclerView recyslerview = (RecyclerView) view.findViewById(R.id.recycler_view);
    // Create LinearLayoutManager  Object   Use here  LinearLayoutManager  It means linear layout 
    LinearLayoutManager layoutmanager = new LinearLayoutManager(this);
    // Settings RecyclerView  Layout 
    recyslerview.setLayoutManager(layoutmanager);
    // Settings Adapter
    FileListAdapter adapter = new FileListAdapter(mVecFile);
 
    adapter.setOnItemClickListener(new FileListAdapter.OnItemClickListener() {
//      @Override
//      public void onLongClickItem(int position,String fileName) {
//        Toast.makeText(AudioRecordActivity.this,"onLongClick Events      You have clicked on: "+position+" A 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);// Read a file as a line 
          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);
    
    // Solve android7.0 The adaptation problem of the above mobile phones 
    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>

Related articles: