前言:毕业设计没有需求,自己想需求,没什么太多想法,突然想起来有个渤大表白墙这个功能也不错,随便写一下。
具体就是一个弹窗插入数据,获取数据,在ListView中显示适配,下拉刷新。
public class ConfessionFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {
@BindView(R.id.msg_love_list)
ListView msgListView;
@BindView(R.id.refresh_love)
SwipeRefreshLayout refreshLove;
List mMsgLoveList = new ArrayList();
MsgLoveAdapter mMsgLoveAdapter;
public static Fragment getInstance() {
Fragment fragment = new ConfessionFragment();
return fragment;
}
@Override
protected int contentViewID() {
return R.layout.fragment_confession;
}
@Override
protected void initialize() {
EventBus.getDefault().register(this);
refreshLove.setOnRefreshListener(this);
refreshLove.setColorSchemeColors(getResources().getColor(R.color.color_love));
queryLoveMsg();
}
private void queryLoveMsg() {
BmobQuery msgLoveBmobQuery = new BmobQuery();
msgLoveBmobQuery.order("-createdAt");
msgLoveBmobQuery.findObjects(new FindListener() {
@Override
public void done(List object, BmobException e) {
if (e == null) {
mMsgLoveList = object;
mMsgLoveAdapter = new MsgLoveAdapter(getContext(), R.layout.item_confession, mMsgLoveList);
msgListView.setAdapter(mMsgLoveAdapter);
} else {
ToastUtils.showShort(getContext(), getContext().getString(R.string.query_failure));
}
}
});
}
@Override
public void onRefresh() {
queryLoveMsg();
mMsgLoveAdapter.notifyDataSetChanged();
refreshLove.setRefreshing(false);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(MessageEvent messageEvent) {
switch (messageEvent.getMessage()) {
case ConstantConfig.UPDATE_LOVE:
onRefresh();
break;
default:
}
}
@OnClick({R.id.btn_add_love})
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_add_love:
DialogUtils.addLoveDialog(getContext());
break;
default:
}
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
}
public class MsgLoveAdapter extends ArrayAdapter {
public static final String TAG = "MsgLoveAdapter";
List mMsgLoveList;
private int resourceId;
public MsgLoveAdapter(@NonNull Context context, int resource, @NonNull List objects) {
super(context, resource, objects);
resourceId = resource;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
MsgLove msgLove = getItem(position);
ViewHolder viewHolder;
View view;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, null);
viewHolder = new ViewHolder();
viewHolder.loveTo = view.findViewById(R.id.love_to);
viewHolder.loveFrom = view.findViewById(R.id.love_from);
viewHolder.loveContent = view.findViewById(R.id.love_content);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.loveTo.setText(msgLove.getTo());
viewHolder.loveContent.setText(msgLove.getContent());
viewHolder.loveFrom.setText(msgLove.getFrom());
return view;
}
class ViewHolder {
TextView loveTo;
TextView loveFrom;
TextView loveContent;
SimpleDraweeView loveImg;
}
}
public class DialogUtils {
public static void addLoveDialog(Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.add_love_tip, null);
EditText etLoveTo = view.findViewById(R.id.et_love_to);
EditText etLoveFrom = view.findViewById(R.id.et_love_from);
EditText etLoveSays = view.findViewById(R.id.et_love_says);
Button btnLovePublish = view.findViewById(R.id.btn_love_publish);
Button btnLoveCancel = view.findViewById(R.id.btn_love_cancel);
final Dialog dialog = new AlertDialog.Builder(context)
.setView(view)
.setCancelable(true)
.create();
btnLovePublish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (TextUtils.isEmpty(etLoveTo.getText().toString()) || TextUtils.isEmpty(etLoveFrom.getText().toString())
|| TextUtils.isEmpty(etLoveSays.getText().toString())){
ToastUtils.showShort(context,context.getString(R.string.tip_empty));
return;
}
MsgLove msgLove = new MsgLove();
MyUser myUser = MyUser.getCurrentUser(MyUser.class);
msgLove.setUsername(myUser.getUsername());
msgLove.setTo(etLoveTo.getText().toString());
msgLove.setFrom(etLoveFrom.getText().toString());
msgLove.setContent(etLoveSays.getText().toString());
msgLove.save(new SaveListener() {
@Override
public void done(String s, BmobException e) {
if (e == null) {
ToastUtils.showShort(context,context.getString(R.string.love_send));
EventBus.getDefault().post(new MessageEvent(ConstantConfig.UPDATE_LOVE));
dialog.dismiss();
} else {
ToastUtils.showShort(context,context.getString(R.string.love_fail));
}
}
});
}
});
btnLoveCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
}
作者:江南233