本文執行個體為大家分享了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
複制
以上就是本文的全部内容,希望對大家的學習有所幫助。