您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
安卓彈窗dialog中實現listview
发布时间:2021-06-25 22:33:30编辑:雪饮阅读()
那麽關於安卓中的彈窗,一般的實現都是很簡單的,但是有時候我們需要用到自定義彈窗,而又更甚至如彈窗中使用listview,那麽對於彈窗來説,他其實也類似一個activity,只是它的表現形式并非完全叠加在我們當前界面而已,傳統的activity是完全叠加(類比)。
// style引用style样式
public MyDialog(Context context, int width, int height, View layout, int style) {
super(context, style);
setContentView(layout);
Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.gravity = Gravity.CENTER;
window.setAttributes(params);
}
}
這裏我們傳遞了第五個參數style以一個佈局文件,其實現如:values/styles.xml:
而上面的show方法就是讓該自定義dialog進行顯示的。 其自定義佈局文件video_trans_video_dialog_select.xml如:
可見這裏有一個listview,那麽同樣的我們需要獲取到這個listview並給它進行數據適配器
這個數據適配器裏面比較重點有兩處,且看這個數據適配器如何實現的:
這裏第一個重點就是如:
mMyDialog.setCancelable(true);
mMyDialog.show();
<resources>
<style name="DialogTheme" parent="@android:style/Theme.Dialog">
<!-- 边框 -->
<item name="android:windowFrame">@null</item>
<!-- 是否浮现在activity之上 -->
<item name="android:windowIsFloating">true</item>
<!-- 半透明 -->
<item name="android:windowIsTranslucent">true</item>
<!-- 无标题 -->
<item name="android:windowNoTitle">true</item>
<item name="android:background">@android:color/transparent</item>
<!-- 背景透明 -->
<item name="android:windowBackground">@android:color/transparent</item>
<!-- 模糊 -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 遮罩层 -->
<item name="android:backgroundDimAmount">0.5</item>
</style>
</resources>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FFFFFF" >
<RelativeLayout
android:layout_width="360dp"
android:layout_height="480dp"
android:background="@drawable/dialog_bg"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="20dp"
android:text="视频选择"
android:textAlignment="center"
/>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/videolist"
>
</ListView>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>數據適配器
mListView.setAdapter(new MyAdatper());
public int getCount() {
return VideoList.size();
}
public Object getItem(int position) {
return VideoList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
VideoInfo videoInfo=VideoList.get(position);
LayoutInflater inflater=LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.video_trans_video_dialog_select_listview, null);
ImageView iv=view.findViewById(R.id.iv);
loadCover(iv,videoInfo.getAbsolutePath(), parent.getContext());
TextView tv = view.findViewById(R.id.tv);
tv.setText(videoInfo.getBrevityName());
View.OnClickListener listener=new View.OnClickListener(){
@Override
public void onClick(View v)
{
toMp4(videoInfo);
mMyDialog.cancel();
}
};
view.setOnClickListener(listener);
return view;
}
} 沒錯,在單純一個activity中實現listview的時候增壓泵,from中的這個上下文就直接是例如ActivityVideoTrans.this,而在我們dialog中這個上下文就需要通過parent.getContext()進行獲取。
第二個重點就是在dialog中的listview中某個item要進行點擊之後我想要取消/關閉該dialog時候我們可以通過如:
View.OnClickListener listener=new View.OnClickListener(){
@Override
public void onClick(View v)
{
toMp4(videoInfo);
mMyDialog.cancel();
}
};這裏cancel是dialog的取消或者説是關閉的方法。
那麽最後就是我們這裏加載的這個dialog中的listview的佈局
View view = inflater.inflate(R.layout.video_trans_video_dialog_select_listview, null);
其實現如:video_trans_video_dialog_select_listview.xml:
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF"
>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:id="@+id/iv"
/>
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="20dp"
/>
</LinearLayout>那麽最後的效果如:
关键字词:dialog,listview