今天看到同事使用的是我没见过的DialogFragment这个控件, 比以往看到的都挺好用.
使用方法如下:
FragmentManager fragmentManager = getSupportFragmentManager();
RegisterDialogFragment signDialogFragment = RegisterDialogFragment.newInstance(appDesc);
signDialogFragment.setOnConfirmListener(new RegisterDialogFragment.OnConfirmListener() {
@Override
public void onConfirm() {
downloadBinder.startDownload(url);
}
});
signDialogFragment.show(fragmentManager, "RegisterDialogFragment");
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatDialog;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.jt.base.R;
/**
* A simple {@link Fragment} subclass.
*/
public class RegisterDialogFragment extends AppCompatDialogFragment {
public RegisterDialogFragment() {
// Required empty public constructor
}
private String content;
public static RegisterDialogFragment newInstance(String type) {
RegisterDialogFragment registerDialogFragment = new RegisterDialogFragment();
Bundle bundle = new Bundle();
bundle.putString("type", type);
registerDialogFragment.setArguments(bundle);
return registerDialogFragment;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_register_dialog, null);
if (getArguments() != null) {
content = getArguments().getString("type");
}
initView(contentView);
AppCompatDialog appCompatDialog = new AppCompatDialog(getContext(), R.style.DialogNoTitle);
appCompatDialog.setContentView(contentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
appCompatDialog.setCanceledOnTouchOutside(false);
return appCompatDialog;
}
private void initView(View view) {
ImageView image = (ImageView) view.findViewById(R.id.dialog_btn);
TextView text = (TextView) view.findViewById(R.id.dialog_text);
text.setText(content);
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDialog().dismiss();
if (onConfirmListener != null) {
onConfirmListener.onConfirm();
}
}
});
view.findViewById(R.id.dialog_close).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDialog().dismiss();
}
});
}
private OnConfirmListener onConfirmListener;
public void setOnConfirmListener(OnConfirmListener onConfirmListener) {
this.onConfirmListener = onConfirmListener;
}
public interface OnConfirmListener {
void onConfirm();
}
}
然后我觉得这个也写得很好的:
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatDialog;
import android.support.v7.app.AppCompatDialogFragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import com.jt.base.R;
import com.jtvr.adapter.MovieDetailAdapter.PutMoneyDialogAdapter;
import com.jtvr.avtivity.MyActivity.MemberCatogory;
import com.jtvr.utils.SharedPreferencesUtils;
import java.util.ArrayList;
import java.util.List;
/**
* Created by yy on 2017/5/18.
* 打赏
*/
public class PutMoneyDialogFragment extends AppCompatDialogFragment {
public PutMoneyDialogFragment() {
}
private String mName;
private int mNum;
private PutMoneyDialogAdapter adapter;
private List list;
public static PutMoneyDialogFragment newInstance(String name) {
PutMoneyDialogFragment putMoneyDialogFragment = new PutMoneyDialogFragment();
Bundle bundle = new Bundle();
bundle.putString("name", name);
putMoneyDialogFragment.setArguments(bundle);
return putMoneyDialogFragment;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_putmoney_dialog, null);
if (getArguments() != null) {
mName = getArguments().getString("name");
}
initData();
initView(contentView);
AppCompatDialog appCompatDialog = new AppCompatDialog(getContext(), R.style.DialogNoTitle);
appCompatDialog.setContentView(contentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
appCompatDialog.setCanceledOnTouchOutside(false);
return appCompatDialog;
}
public void dismiss() {
getDialog().dismiss();
}
private void initData() {
list = new ArrayList<>();
list.add("5");
list.add("10");
list.add("20");
list.add("50");
list.add("100");
list.add("520");
}
private void initView(View view) {
TextView name = (TextView) view.findViewById(R.id.putMoney_dialog_name);
TextView num = (TextView) view.findViewById(R.id.putMoney_dialog_num);
final EditText editText = (EditText) view.findViewById(R.id.putMoney_dialog_money);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.putMoney_dialog_recycler);
view.findViewById(R.id.putMoney_dialog_put).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onConfirmListener != null) {
if (mNum != 0) {
if ("".equals(editText.getText().toString())) {
onConfirmListener.onConfirm(mNum);
} else {
onConfirmListener.onConfirm(Integer.parseInt(editText.getText().toString()));
}
} else {
if ("".equals(editText.getText().toString())) {
onConfirmListener.onConfirm(5);
} else {
onConfirmListener.onConfirm(Integer.parseInt(editText.getText().toString()));
}
}
}
}
});
adapter = new PutMoneyDialogAdapter(list, getContext());
recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
recyclerView.setAdapter(adapter);
name.setText(Html.fromHtml(String.format(getString(R.string.put_money_dialog_name), "#333333", "#f44336", mName)));
num.setText("您目前的火种余额:" + SharedPreferencesUtils.get(getContext(), "fires", "0").toString() + "颗");
view.findViewById(R.id.putMoney_dialog_close).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
adapter.setOnItemClickListener(new PutMoneyDialogAdapter.OnRecyclerViewItemClickListener() {
@Override
public void onItemClick(View view, int position) {
mNum = Integer.parseInt(list.get(position));
}
});
view.findViewById(R.id.putMoney_dialog_buy).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getContext(), MemberCatogory.class));
dismiss();
}
});
}
private OnConfirmListener onConfirmListener;
public void setOnConfirmListener(OnConfirmListener onConfirmListener) {
this.onConfirmListener = onConfirmListener;
}
public interface OnConfirmListener {
void onConfirm(int num);
}
}
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.jt.base.R;
import java.util.List;
/**
* Created by yy on 2017/5/18.
*/
public class PutMoneyDialogAdapter extends RecyclerView.Adapter {
private List list;
private OnRecyclerViewItemClickListener mOnItemClickListener;
private int layoutPosition;
private Context context;
public PutMoneyDialogAdapter(List list, Context context) {
this.list = list;
this.context = context;
}
public interface OnRecyclerViewItemClickListener {
void onItemClick(View view, int position);
}
public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
this.mOnItemClickListener = listener;
}
@Override
public PutMoneyDialogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new PutMoneyDialogViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.put_money_item, parent, false));
}
@Override
public void onBindViewHolder(final PutMoneyDialogViewHolder holder, int position) {
holder.num.setText(list.get(position) + "火种");
holder.num.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取当前点击的位置
layoutPosition = holder.getLayoutPosition();
notifyDataSetChanged();
mOnItemClickListener.onItemClick(v, layoutPosition);
}
});
//更改状态
if (position == layoutPosition) {
holder.num.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.shape_vip_sel));
holder.image.setVisibility(View.VISIBLE);
} else {
holder.num.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.shape_vip));
holder.image.setVisibility(View.INVISIBLE);
}
}
@Override
public int getItemCount() {
return list != null ? list.size() : 0;
}
class PutMoneyDialogViewHolder extends RecyclerView.ViewHolder {
private TextView num;
private ImageView image;
public PutMoneyDialogViewHolder(View itemView) {
super(itemView);
num = (TextView) itemView.findViewById(R.id.putMoney_item_num);
image = (ImageView) itemView.findViewById(R.id.putMoney_item_image);
}
}
}