android 列表的dialog
阿新 • • 發佈:2019-01-02
1.編寫列表佈局
<?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="vertical"> <android.support.v7.widget.RecyclerView android:id="@+id/rv_dialog" android:layout_width="280dp" android:layout_height="wrap_content"/> </LinearLayout>
2.建立自定義dialog package com.dejun.commonsdk.dialog; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.DialogFragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.dejun.commonsdk.R; import com.dejun.commonsdk.adapter.ListDialogAdapter; import com.dejun.commonsdk.base.BaseRvAdapter; import com.dejun.commonsdk.wight.ListDividerItemDecoration; import java.util.ArrayList; /** * Author:DoctorWei * Time:2018/12/8 13:57 * Description:退出dialog * email:[email protected] */ public class ListDialog extends DialogFragment{ public static final String DIALOG_LIST="dialog_list"; private RecyclerView mRvDialog; public static ListDialog creatConfirmDialog(Bundle bundle){ ListDialog confirmDialog=new ListDialog(); if (bundle!=null){ confirmDialog.setArguments(bundle); } return confirmDialog; } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.list_dialog_layout,null); mRvDialog=view.findViewById(R.id.rv_dialog); return view; } @Override public void onStart() { super.onStart(); Bundle arguments = getArguments(); ArrayList<String> stringArrayList = arguments.getStringArrayList(DIALOG_LIST); if (stringArrayList!=null&&!stringArrayList.isEmpty()){ //設定資料 ListDialogAdapter listDialogAdapter=new ListDialogAdapter(stringArrayList,getActivity(),R.layout.list_dialog_item); mRvDialog.setLayoutManager(new LinearLayoutManager(getActivity())); mRvDialog.setAdapter(listDialogAdapter); listDialogAdapter.setOnItemClickListener(new BaseRvAdapter.OnItemClickListener<String>() { @Override public void onItemClick(String s, int position) { if (onItemClickListener!=null){ onItemClickListener.onItemClick(position); } dismiss(); } }); } } @Override public void show(FragmentManager manager, String tag) {//解決dialog顯示程式退到後臺的崩潰 try { FragmentTransaction ft = manager.beginTransaction(); ft.add(this, tag); ft.commitAllowingStateLoss(); } catch (Exception e) { e.printStackTrace(); } } @Override public void dismiss() {//解決activity被銷燬dialog的狀態異常 super.dismissAllowingStateLoss(); } public void setOnClickListener(OnItemClickListener onItemClickListener){ this.onItemClickListener=onItemClickListener; } public OnItemClickListener onItemClickListener; public interface OnItemClickListener{ void onItemClick(int position); } } 3.使用
ListDialog listDialog=new ListDialog(); Bundle bundle=new Bundle(); ArrayList<String> list=new ArrayList<>(); list.add("複製"); list.add("貼上"); bundle.putStringArrayList(ListDialog.DIALOG_LIST,list); listDialog.setArguments(bundle); listDialog.show(getSupportFragmentManager(),"ThirdActivity"); listDialog.setOnClickListener(new ListDialog.OnItemClickListener() { @Override public void onItemClick(int position) { Logger.d(position); } });