
前言:这是Bmob 的经典案例,但是由于现在版本升级哪里也不更新了,同时我辅导员助手这个项目正好有这个功能,拿来用一下,上次去计算机程序设计大赛,评委老师提了个建议,说最后用自己的数据库,不要用公司的,这样相当于数据不在自己手里,我自己以前也腾讯上搭过了一个自己的学生Linux服务器和mysql 也使用过 阿里云mysql ,最后作为一个学生党,还是比较喜欢Bomb,对于学生来说,真的是方便与便利。
创建一个失物招领的实例,继承bmob数据库,方便获取、添加数据等。
先讲 布局RecyclerView
在使用RecyclerView时候,必须指定一个适配器Adapter和一个布局管理器LayoutManager。适配器继承RecyclerView.Adapter类,具体实现类似ListView的适配器,取决于数据信息以及展示的UI。布局管理器用于确定RecyclerView中Item的展示方式以及决定何时复用已经不可见的Item,避免重复创建以及执行高成本的findViewById()方法。
<android.support.v7.widget.RecyclerViewandroid:id="@+id/rl_recyclerview"android:layout_width="match_parent"android:layout_height="match_parent"/>
Item的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""android:id="@+id/ll_item"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#fff"android:orientation="vertical"><TextViewandroid:id="@+id/tv_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginTop="5dp"android:text="@string/t1"android:textColor="#000"android:textSize="15sp"/><TextViewandroid:id="@+id/tv_desc"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginTop="5dp"android:text="@string/con1"android:textColor="#9000"android:textSize="15sp"/><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginTop="10dp"><TextViewandroid:id="@+id/tv_num"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_marginRight="10dp"android:background="#5CBE6C"android:padding="5dp"android:text="16642712339"android:textColor="#fff"/><TextViewandroid:id="@+id/tv_time"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:text="2019-05-21 17:04:11"android:textColor="#9000"/></RelativeLayout><Viewandroid:layout_width="match_parent"android:layout_height="0.5dp"android:layout_marginTop="5dp"android:background="#5000"/>
</LinearLayout>
标准实现步骤如下:
① 创建Adapter:创建一个继承RecyclerView.Adapter<VH>的Adapter类(VH是ViewHolder的类名)
② 创建ViewHolder:在Adapter中创建一个继承RecyclerView.ViewHolder的静态内部类,记为VH。ViewHolder的实现和ListView的ViewHolder实现几乎一样。
③ 在Adapter中实现3个方法:
nCreateViewHolder()
这个方法主要生成为每个Item inflater出一个View,但是该方法返回的是一个ViewHolder。该方法把View直接封装在ViewHolder中,然后我们面向的是ViewHolder这个实例,当然这个ViewHolder需要我们自己去编写。
@Overridepublic LostAndFoundHolder onCreateViewHolder(ViewGroup parent, int viewType) {View view = LayoutInflater.from(mContext).inflate(R.layout.lost_item, parent, false);LostAndFoundHolder lostAndFoundHolder = new LostAndFoundHolder(view);return lostAndFoundHolder;}
onBindViewHolder()
这个方法主要用于适配渲染数据到View中。方法提供给你了一viewHolder而不是原来的convertView。
getItemCount()
这个方法就类似于BaseAdapter的getCount方法了,即总共有多少个条目。
public class LostAndFoundAdapter extends RecyclerView.Adapter<LostAndFoundAdapter.LostAndFoundHolder> {private Context mContext;private List<LostInfomationReq> lostInfosData;private ItemClickListener mItemClickListener;public final static int EDIT_CODE = 998;public final static int DELETE_CODE = 997;public LostAndFoundAdapter(Context context) {this.mContext = context;}public void setData(List<LostInfomationReq> data) {this.lostInfosData = data;}@Overridepublic LostAndFoundHolder onCreateViewHolder(ViewGroup parent, int viewType) {View view = LayoutInflater.from(mContext).inflate(R.layout.lost_item, parent, false);LostAndFoundHolder lostAndFoundHolder = new LostAndFoundHolder(view);return lostAndFoundHolder;}@Overridepublic void onBindViewHolder(final LostAndFoundHolder holder, final int position) {if (lostInfosData != null) {LostInfomationReq lostInfomationReq = (position);holder.title.Title());holder.desc.Desc());holder.phoneNum.PhoneNum());holder.time.CreatedAt());holder.llItem.setOnLongClickListener(new View.OnLongClickListener() {@Overridepublic boolean onLongClick(View v) {//LongClick(position);showWindow(holder, position);return false;}});}}private void showWindow(LostAndFoundHolder holder, final int pos) {//加载布局文件View contentview = LayoutInflater.from(mContext).inflate(R.layout.pop_window_view,null);final PopupWindow popupWindow = new PopupWindow(contentview, LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);//设置焦点popupWindow.setFocusable(true);//触摸框外popupWindow.setOutsideTouchable(true);//点击空白处的时候让PopupWindow消失popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));//设置偏移量popupWindow.showAsDropDown(holder.time, 300, -100);//showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移// showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移//showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移//点击编辑按钮contentview.findViewById(R.id.edit_btn).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//回调给主界面,进行数据操作EditOrDeleteClick(pos, EDIT_CODE);//销毁弹出框popupWindow.dismiss();}});//点击删除按钮contentview.findViewById(R.id.delete_btn).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//回调给主界面,进行数据操作EditOrDeleteClick(pos, DELETE_CODE);//销毁弹出框popupWindow.dismiss();}});}@Overridepublic int getItemCount() {return lostInfosData.size() == 0 ? 0 : lostInfosData.size();}public class LostAndFoundHolder extends RecyclerView.ViewHolder {private TextView title;private TextView desc;private TextView time;private TextView phoneNum;private LinearLayout llItem;public LostAndFoundHolder(View itemView) {super(itemView);title = (TextView) itemView.findViewById(R.id.tv_title);desc = (TextView) itemView.findViewById(R.id.tv_desc);time = (TextView) itemView.findViewById(R.id.tv_time);phoneNum = (TextView) itemView.findViewById(R.id.tv_num);llItem = (LinearLayout) itemView.findViewById(R.id.ll_item);}}//设置长按事件public void setLongClickListener(ItemClickListener clickListener) {this.mItemClickListener = clickListener;}public interface ItemClickListener {void onEditOrDeleteClick(int position, int code);}
}
其中包括 PopupWindow的使用
长按item 出现弹窗
pop_l
<LinearLayout xmlns:android=""android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:id="@+id/edit_btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@color/blue"android:padding="10dp"android:text="添加"android:textColor="#fff"/><TextViewandroid:id="@+id/delete_btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="10dp"android:background="@color/blue"android:text="删除"android:textColor="#fff" />
</LinearLayout>
使用PopupWindow很简单,可以总结为三个步骤:
public class LostFoodFragment extends Fragment implements View.OnClickListener, LostAndFoundAdapter.ItemClickListener {private RecyclerView recyclerView;private LostAndFoundAdapter lostAndFoundAdapter;private List<LostInfomationReq> lostInfomationReqList;private long exitTime = 0;private final static int REQUEST_CODE = 999;public LostFoodFragment() {// Required empty public constructor}@Overridepublic void onCreate(Bundle savedInstanceState) {Create(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragment_lost_food,container,false);initView(view);initData();return view;}private void initView(View view) {recyclerView = (RecyclerView) view.findViewById(R.id.rl_recyclerview);recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));lostAndFoundAdapter = new LostAndFoundAdapter(getActivity());lostAndFoundAdapter.setLongClickListener(this);}private void initData() {BmobQuery<LostInfomationReq> lostInfomationReqBmobQuery = new BmobQuery<>();der("-updatedAt");//排序lostInfomationReqBmobQuery.findObjects(new FindListener<LostInfomationReq>() {@Overridepublic void done(List<LostInfomationReq> list, BmobException e) {if (e == null) {lostInfomationReqList = list;lostAndFoundAdapter.setData(list);recyclerView.setAdapter(lostAndFoundAdapter);} else {showToast("查询数据失败");}}});}private void showToast(String msg) {Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();}@Overridepublic void onClick(View v) {}@Overridepublic void onEditOrDeleteClick(int position, int code) {if (code == LostAndFoundAdapter.EDIT_CODE) {Intent intent = new Intent(getActivity(), Lost_fondActivity.class);Bundle bundle = new Bundle();bundle.putSerializable("editData", (position));intent.putExtras(bundle);startActivityForResult(intent, REQUEST_CODE);} else if (code == LostAndFoundAdapter.DELETE_CODE) {deleteItemData(position);}}private void deleteItemData(final int position) {if (lostInfomationReqList.size() != 0) {LostInfomationReq lostInfomationReq = new LostInfomationReq();lostInfomationReq.(position).getObjectId());lostInfomationReq.delete(new UpdateListener() {@Overridepublic void done(BmobException e) {if (e == null) {ve(position);lostAndFoundAdapter.setData(lostInfomationReqList);ifyDataSetChanged();} else {showToast("删除数据失败");}}});}}@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {switch (requestCode) {case REQUEST_CODE:if (resultCode == RESULT_OK) {refreshData();//数据刷新}break;default:break;}ActivityResult(requestCode, resultCode, data);}/*** 查询数据库中最新的数据* */private void refreshData() {BmobQuery<LostInfomationReq> lostInfomationReqBmobQuery = new BmobQuery<>();der("-updatedAt");//按更新时间排序lostInfomationReqBmobQuery.findObjects(new FindListener<LostInfomationReq>() {@Overridepublic void done(List<LostInfomationReq> list, BmobException e) {if (e == null) {lostInfomationReqList = list;lostAndFoundAdapter.setData(list);ifyDataSetChanged();}}});}
}
RecyclerView实现Item点击事件处理
RecyclerView并没有像ListView那样提供了OnItemClick,OnItemLongClick等事件回调接口,所以,我们需要自己去实现。
定义回调事件接口
public interface ItemClickListener {void onEditOrDeleteClick(int position, int code);}
声明该接口,并提供setter方法
//设置长按事件public void setLongClickListener(ItemClickListener clickListener) {this.mItemClickListener = clickListener;}
实现接口
implements View.OnClickListener, LostAndFoundAdapter.ItemClickListener
实现编辑和删除方法
@Overridepublic void onEditOrDeleteClick(int position, int code) {if (code == LostAndFoundAdapter.EDIT_CODE) {Intent intent = new Intent(getActivity(), Lost_fondActivity.class);Bundle bundle = new Bundle();bundle.putSerializable("editData", (position));intent.putExtras(bundle);startActivityForResult(intent, REQUEST_CODE);} else if (code == LostAndFoundAdapter.DELETE_CODE) {deleteItemData(position);}}private void deleteItemData(final int position) {if (lostInfomationReqList.size() != 0) {LostInfomationReq lostInfomationReq = new LostInfomationReq();lostInfomationReq.(position).getObjectId());lostInfomationReq.delete(new UpdateListener() {@Overridepublic void done(BmobException e) {if (e == null) {ve(position);lostAndFoundAdapter.setData(lostInfomationReqList);ifyDataSetChanged();} else {showToast("删除数据失败");}}});}}
通过参数传递数据判断 点击按钮
//点击编辑按钮contentview.findViewById(R.id.edit_btn).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//回调给主界面,进行数据操作EditOrDeleteClick(pos, EDIT_CODE);//销毁弹出框popupWindow.dismiss();}});//点击删除按钮contentview.findViewById(R.id.delete_btn).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//回调给主界面,进行数据操作EditOrDeleteClick(pos, DELETE_CODE);//销毁弹出框popupWindow.dismiss();}});
这个和前几篇一样,将数据获取和数据插入。写博客的好处,就是梳理思路,理解过程。
失物招领 我来二改了 只有自己发布的可以长按编辑修改/删除 也可以新发布一个新的
实现物品的发布、呈现、修改和删除,用户捡到或丢失物品,打开手机软件,填写物品的招领信息(标题、描述和联系方式);所有用户可以查看到失物和招领的信息列表,如果用户自己发布的消息,长按弹出悬浮窗,可进行编辑和删除。
基于Bmob后端云,添加失物/招领信息到服务器中。查询失物招领所有数据,在列表中显示所有用户发布的失物/招领信息。编辑数据,会将该数据内容直接显示到添加失物招领界面,提交后进行数据更新。删除数据,删除已发布的失物/招领信息。
效果图:
public class LostAndFoundAdapter extends RecyclerView.Adapter<LostAndFoundAdapter.LostAndFoundHolder> {private Context mContext;private List<LostInformation> mLostInformationList;private ItemClickListener mItemClickListener;public final static int EDIT_CODE = 998;public final static int DELETE_CODE = 997;public LostAndFoundAdapter(Context context) {mContext = context;}public void setLostInformationList(List<LostInformation> lostInformationList) {mLostInformationList = lostInformationList;}@NonNull@Overridepublic LostAndFoundHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {View view = LayoutInflater.from(mContext).inflate(R.layout.item_lost, parent, false);LostAndFoundHolder lostAndFoundHolder = new LostAndFoundHolder(view);return lostAndFoundHolder;}@Overridepublic void onBindViewHolder(@NonNull LostAndFoundHolder holder, int position) {if (!mLostInformationList.isEmpty()) {LostInformation lostInformation = (position);holder.title.Title());holder.desc.Desc());lephone.PhoneNum());holder.time.CreatedAt());holder.llItem.setOnLongClickListener(new View.OnLongClickListener() {@Overridepublic boolean onLongClick(View v) {MyUser myUser = CurrentUser(MyUser.class);if (Username().Username())){showWindow(holder, position);return true;}return false;}});}}private void showWindow(LostAndFoundHolder holder, int position) {//加载布局文件View contentView = LayoutInflater.from(mContext).inflate(R.layout.pop_window_view, null);final PopupWindow popupWindow = new PopupWindow(contentView, LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);//设置焦点popupWindow.setFocusable(true);//触摸框外popupWindow.setOutsideTouchable(true);//点击空白处的时候让PopupWindow消失popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));//设置偏移量popupWindow.showAsDropDown(holder.time, 300, -100);//点击编辑按钮contentView.findViewById(R.id.edit_btn).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//回调给主界面,进行数据操作EditOrDeleteClick(position, EDIT_CODE);//销毁弹出框popupWindow.dismiss();}});//点击删除按钮contentView.findViewById(R.id.delete_btn).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//回调给主界面,进行数据操作EditOrDeleteClick(position, DELETE_CODE);//销毁弹出框popupWindow.dismiss();}});}@Overridepublic int getItemCount() {return mLostInformationList.size() == 0 ? 0 : mLostInformationList.size();}//设置长按事件public void setLongClickListener(ItemClickListener clickListener) {this.mItemClickListener = clickListener;}public interface ItemClickListener {void onEditOrDeleteClick(int position, int code);}public class LostAndFoundHolder extends RecyclerView.ViewHolder {private TextView title;private TextView desc;private TextView time;private TextView telephone;private LinearLayout llItem;public LostAndFoundHolder(View itemView) {super(itemView);title = (TextView) itemView.findViewById(R.id.lost_title);desc = (TextView) itemView.findViewById(R.id.lost_desc);time = (TextView) itemView.findViewById(R.id.lost_time);telephone = (TextView) itemView.findViewById(R.id.lost_telephone);llItem = (LinearLayout) itemView.findViewById(R.id.ll_item);}}}
public class LostFoundFragment extends BaseFragment implements LostAndFoundAdapter.ItemClickListener {@BindView(lerview_lost)RecyclerView recyclerViewLost;LostAndFoundAdapter lostAndFoundAdapter;List<LostInformation> mLostInformationList;public static Fragment getInstance() {Fragment fragment = new LostFoundFragment();return fragment;}@Overrideprotected int contentViewID() {return R.layout.fragment_lost_found;}@Overrideprotected void initialize() {Default().register(this);lostAndFoundAdapter = new LostAndFoundAdapter(getContext());lostAndFoundAdapter.setLongClickListener(this);recyclerViewLost.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false));queryData();}private void queryData() {BmobQuery<LostInformation> lostInformationBmobQuery = new BmobQuery<>();der("-updatedAt");//排序lostInformationBmobQuery.findObjects(new FindListener<LostInformation>() {@Overridepublic void done(List<LostInformation> object, BmobException e) {if (e == null) {mLostInformationList = object;lostAndFoundAdapter.setLostInformationList(mLostInformationList);recyclerViewLost.setAdapter(lostAndFoundAdapter);} else {ToastUtils.showShort(getContext(), getString(R.string.tip_query_lost));}}});}@Overridepublic void onEditOrDeleteClick(int position, int code) {if (code == LostAndFoundAdapter.EDIT_CODE) {Bundle bundle = new Bundle();bundle.putSerializable("editData", (position));startActivity(EditLostActivity.class, bundle);}if (code == LostAndFoundAdapter.DELETE_CODE) {deleteItemData(position);}}private void deleteItemData(int position) {if (!mLostInformationList.isEmpty()) {LostInformation lostInformation = new LostInformation();lostInformation.(position).getObjectId());lostInformation.delete(new UpdateListener() {@Overridepublic void done(BmobException e) {if (e == null) {ve(position);lostAndFoundAdapter.setLostInformationList(mLostInformationList);ifyDataSetChanged();} else {ToastUtils.showShort(getContext(), getString(R.string.delete_failure));}}});}}@Subscribe(threadMode = ThreadMode.MAIN)public void onEvent(MessageEvent messageEvent) {switch (Message()) {case ConstantConfig.ADD_LOST_PUSH:case ConstantConfig.UPDATE_LOST_PUSH:refreshData();break;default:}}private void refreshData() {BmobQuery<LostInformation> lostInformationBmobQuery = new BmobQuery<>();der("-updatedAt");//排序lostInformationBmobQuery.findObjects(new FindListener<LostInformation>() {@Overridepublic void done(List<LostInformation> object, BmobException e) {if (e == null) {mLostInformationList = object;lostAndFoundAdapter.setLostInformationList(mLostInformationList);ifyDataSetChanged();} else {ToastUtils.showShort(getContext(), getString(R.string.tip_query_lost));}}});}@OnClick(R.id.btn_add_lost)public void OnClick(View view){startActivity(EditLostActivity.class);}@Overridepublic void onDestroy() {Destroy();Default().unregister(this);}
}
public class EditLostActivity extends BaseActivity {@BindView(R.id.lost_username)TextView lostUsername;@BindView(R.id.lost_et_title)TextInputEditText etLostTitle;@BindView(R.id.lost_et_contact)TextInputEditText etLostContact;@BindView(R.id.lost_et_desc)EditText etLostDesc;private boolean isEditLost = false;LostInformation lostInformation;@Overrideprotected int contentViewID() {return R.layout.activity_edit_lost;}@Overrideprotected void initialize() {setTopTitle(getString(R.string.add_lost), true);setLeftBtnFinish();MyUser myUser = CurrentUser(MyUser.class);lostUsername.Username());Intent intent = getIntent();lostInformation = (LostInformation) SerializableExtra("editData");if (lostInformation != null) {isEditLost = true;setLostText(lostInformation);}}private void setLostText(LostInformation lostInformation) {lostUsername.Username());etLostTitle.Title());etLostContact.PhoneNum());etLostDesc.Desc());}@OnClick(R.id.btn_lost)public void onClick(View view) {switch (Id()) {case R.id.btn_lost:if (isEditLost == true) {updateLostInfo();} else {addLostInfo();}break;default:}}private void addLostInfo() {MyUser myUser = CurrentUser(MyUser.class);LostInformation newLostInformation = new LostInformation();newLostInformation.Username());newLostInformation.Text().toString());newLostInformation.Text().toString());newLostInformation.Text().toString());newLostInformation.save(new SaveListener<String>() {@Overridepublic void done(String s, BmobException e) {if (e == null) {ToastUtils.showShort(EditLostActivity.this, getString(R.string.lost_add_success));Default().post(new MessageEvent(ConstantConfig.ADD_LOST_PUSH));} else {ToastUtils.showShort(EditLostActivity.this, getString(R.string.lost_add_failure));}}});}private void updateLostInfo() {MyUser myUser = CurrentUser(MyUser.class);LostInformation newLostInformation = new LostInformation();newLostInformation.Username());newLostInformation.Text().toString());newLostInformation.Text().toString());newLostInformation.Text().toString());newLostInformation.ObjectId(),new UpdateListener() {@Overridepublic void done(BmobException e) {if (e == null) {ToastUtils.showShort(EditLostActivity.this, getString(R.string.lost_update_success));Default().post(new MessageEvent(ConstantConfig.UPDATE_LOST_PUSH));} else {ToastUtils.showShort(EditLostActivity.this, getString(R.string.lost_update_failure));}}});}
}
本文发布于:2024-03-23 16:14:10,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/1711181655153369.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
| 留言与评论(共有 0 条评论) |