Android實(shí)現(xiàn)多級(jí)列表中的新建功能
本文實(shí)例為大家分享了Android實(shí)現(xiàn)多級(jí)列表中的新建功能,供大家參考,具體內(nèi)容如下
多級(jí)列表的頁(yè)面實(shí)現(xiàn)比較簡(jiǎn)單,所以把新建的功能拿出來(lái)了。
窗口代碼
/** * 新建一個(gè)第一級(jí)列表的條目 * 1.選擇圖片和附件都用Intent.ACTION_GET_CONTENT實(shí)現(xiàn) * 2.打開(kāi)文件用Intent.ACTION_VIEW實(shí)現(xiàn) * 3.回傳的URI需要轉(zhuǎn)化成真實(shí)路徑 * 4.提交數(shù)據(jù)之后需要刷新列表 */ public class SectionNewActivity extends AppCompatActivity implements View.OnClickListener { private static final String TAG = "SectionNewActivity"; @BindView(R.id.tv_title_middle) TextView title; @BindView(R.id.title_left) ImageView back; @BindView(R.id.edit_tv) TextView edit; @BindView(R.id.filter_tv) TextView filter; @BindView(R.id.section_new_logo) ImageView sectionLogo; @BindView(R.id.section_new_manager) TextView sectionManager; @BindView(R.id.section_new_title) TextView sectionTitle; @BindView(R.id.section_new_desc) TextView sectionDesc; @BindView(R.id.tv_upload_attach) TextView selectAttach; @BindView(R.id.lv_attach) ListView mListView; private Context mContext; private List<ClsAttachMent> mAttachList; private AttachmentListAdapter mAdapter; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_section_new); ButterKnife.bind(this); initView(); initData(); initListener(); } private void initData() { mContext = this; //初始化數(shù)據(jù)源 mAttachList = new ArrayList<>(); mAdapter = new AttachmentListAdapter(mAttachList, mContext); mListView.setAdapter(mAdapter); } private void initView() { title.setText("新建板塊"); edit.setVisibility(View.VISIBLE); edit.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_send_black_24dp, 0, 0, 0); } private void initListener() { back.setOnClickListener(this); edit.setOnClickListener(this); filter.setOnClickListener(this); sectionLogo.setOnClickListener(this); sectionManager.setOnClickListener(this); selectAttach.setOnClickListener(this); //點(diǎn)擊附件列表?xiàng)l目的刪除按鈕,刪除對(duì)應(yīng)附件 mAdapter.setmCallback((view, position) -> { mAttachList.remove(position); mAdapter.notifyDataSetChanged(); }); //點(diǎn)擊附件列表彈出打開(kāi)方式 mListView.setOnItemClickListener((parent, view, position, id) -> { ClsAttachMent clsAttachMent = mAttachList.get(position); Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse(clsAttachMent.getUri()), "*/*"); startActivity(intent); }); } @Override public void onClick(View v) { if (v.getId() == R.id.title_left) { finish(); } if (v.getId() == R.id.edit_tv) { submit(); } if (v.getId() == R.id.section_new_logo) { //打開(kāi)手機(jī)原生的文件管理器,并且選取內(nèi)容 Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); //文件類(lèi)型為圖片 intent.setType("image/*"); startActivityForResult(intent, 16352); } if (v.getId() == R.id.section_new_manager) { Intent intent = new Intent(mContext, UserSelectActivity.class); startActivityForResult(intent, 12345); } if (v.getId() == R.id.tv_upload_attach) { //上傳的附件數(shù)量不能超過(guò)4個(gè) if (mAttachList.size() < 4) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("*/*"); startActivityForResult(intent, 12367); if (mAttachList.size() == 0) { Toast.makeText(mContext, R.string.upload_warning, Toast.LENGTH_LONG).show(); } } else { Toast.makeText(mContext, "附件數(shù)量已達(dá)上限!", Toast.LENGTH_SHORT).show(); } } } private void submit() { Toast.makeText(mContext, "在此處調(diào)用接口!", Toast.LENGTH_SHORT).show(); finish(); } @Override //requestCode要對(duì)應(yīng)上,resultCode都為默認(rèn)值RESULT_OK protected void onActivityResult(int requestCode, int resultCode, Intent data) { //選擇圖片完成之后使用glide加載到控件上,此處有時(shí)需要把圖片上傳給后臺(tái) //提交數(shù)據(jù)的時(shí)候傳圖片在后臺(tái)的路徑 if (requestCode == 16352 && resultCode == RESULT_OK) { Glide.with(mContext).load(data.getData()).into(sectionLogo); } //打開(kāi)選擇用戶(hù)的頁(yè)面,根據(jù)傳的參數(shù)不同頁(yè)面也不同,默認(rèn)是單選頁(yè)面 if (requestCode == 12345 && resultCode == RESULT_OK) { ClsNormalUser user = data.getParcelableExtra("user"); sectionManager.setText(user.getCName()); } //遍歷已經(jīng)上傳的附件列表,如果已經(jīng)存在就彈出提示 if (requestCode == 12367 && resultCode == RESULT_OK) { String uri = data.getData().toString(); if (mAttachList.size() > 0) { for (int i = 0; i < mAttachList.size(); i++) { if (uri.equals(mAttachList.get(i).getUri())) { Toast.makeText(mContext, "請(qǐng)選擇不同文件!", Toast.LENGTH_SHORT).show(); break; } if (i == mAttachList.size() - 1) { addAttach(data); break; } } } else { addAttach(data); } } super.onActivityResult(requestCode, resultCode, data); } private void addAttach(Intent data) { //這里使用第三方庫(kù)ucrop的getPath方法,也可以自己實(shí)現(xiàn)uri轉(zhuǎn)換為path File file = new File(getPath(mContext, data.getData())); ClsAttachMent clsAttachMent = new ClsAttachMent(); String name = file.getName(); String type = name.split("\\.")[1]; String size = file.length() + ""; clsAttachMent.setSize(size); clsAttachMent.setFilename(name); clsAttachMent.setUri(data.getData().toString()); //這里需要調(diào)用上傳接口 uploadFile(file.getPath()); mAttachList.add(clsAttachMent); mAdapter.notifyDataSetChanged(); } private void uploadFile(String path) { Toast.makeText(mContext, "在此處調(diào)用接口!", Toast.LENGTH_SHORT).show(); } }
布局文件代碼
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:background="@color/ghostwhite" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <include layout="@layout/title_bar" /> <ImageView android:id="@+id/section_new_logo" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:scaleType="centerCrop" android:background="@color/white" android:src="@drawable/logo" /> <include layout="@layout/layout_item_divider_horizontal" /> <EditText android:id="@+id/section_new_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@null" android:hint="請(qǐng)輸入名稱(chēng)" android:inputType="text" android:textColor="@color/black" /> <include layout="@layout/layout_item_divider_horizontal" /> <EditText android:id="@+id/section_new_desc" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@null" android:gravity="top|start" android:hint="請(qǐng)輸入內(nèi)容" android:inputType="textMultiLine" android:minHeight="100dp" android:textColor="@color/black" /> <include layout="@layout/layout_item_divider_horizontal" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="選擇版主:" android:textColor="@color/black" android:textSize="18sp" /> <TextView android:id="@+id/section_new_manager" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:drawableEnd="@drawable/ic_arrow_drop_down_black_24dp" android:text="default user" android:textColor="@color/black" android:textSize="18sp" /> </LinearLayout> <include layout="@layout/layout_item_divider_horizontal" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:background="@color/white" android:orientation="horizontal"> <TextView android:layout_width="200dp" android:layout_height="wrap_content" android:layout_marginStart="20dp" android:gravity="center_vertical" android:minHeight="30dp" android:text="文件名:" android:textColor="@color/black" /> <TextView android:layout_width="100dp" android:layout_height="30dp" android:gravity="center_vertical" android:text="文件大?。? android:textColor="@color/black" /> <TextView android:layout_width="wrap_content" android:layout_height="30dp" android:gravity="center_vertical" android:text="操作:" android:textColor="@color/black" /> </LinearLayout> <ListView android:id="@+id/lv_attach" android:layout_width="match_parent" android:layout_height="160dp" android:divider="@null" /> <include layout="@layout/layout_item_divider_horizontal" /> <TextView android:id="@+id/tv_upload_attach" style="@style/Widget.AppCompat.Button" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_gravity="end" android:text="添加附件" android:textColor="@color/black" android:textSize="16sp" /> </LinearLayout> </ScrollView>
適配器代碼
public class AttachmentListAdapter extends BaseAdapter { private List<ClsAttachMent> mList; private LayoutInflater mInflater; private Callback mCallback; //自定義回調(diào)接口,用于傳值 public interface Callback { void onClick(View view, int position); } public AttachmentListAdapter(List<ClsAttachMent> attachments, Context mContext) { this.mList = attachments; mInflater = LayoutInflater.from(mContext); } public void setmCallback(Callback mCallback) { this.mCallback = mCallback; } @Override public int getCount() { return mList.size(); } @Override public ClsAttachMent getItem(int position) { return mList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ClsAttachMent clsAttachMent = mList.get(position); ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.item_attchment_list, null); holder.delete = convertView.findViewById(R.id.attachment_delete); holder.name = convertView.findViewById(R.id.attachment_name); holder.size = convertView.findViewById(R.id.attachment_size); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.name.setText(clsAttachMent.getFilename()); long length = Long.parseLong(clsAttachMent.getSize()); holder.size.setText(length / 1024 + "KB"); //將position放在tag里面 holder.delete.setTag(position); holder.delete.setOnClickListener(v -> { //觸發(fā)點(diǎn)擊事件的時(shí)候?qū)osition回傳 mCallback.onClick(v, (Integer) v.getTag()); }); return convertView; } private class ViewHolder { TextView name; TextView size; TextView delete; } }
效果如圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)多級(jí)樹(shù)形選擇列表
- Android 繪制多級(jí)樹(shù)形選擇列表實(shí)例代碼
- Android中RecyclerView實(shí)現(xiàn)多級(jí)折疊列表效果(二)
- Android中RecyclerView實(shí)現(xiàn)多級(jí)折疊列表效果(TreeRecyclerView)
- Android UI 之實(shí)現(xiàn)多級(jí)樹(shù)形列表TreeView示例
- Android仿美團(tuán)淘寶實(shí)現(xiàn)多級(jí)下拉列表菜單功能
- Android多級(jí)樹(shù)形列表控件
相關(guān)文章
Android Studio配置(Android Studio4.1為例)
這篇文章主要介紹了Android Studio配置(Android Studio4.1為例),文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Android studio設(shè)計(jì)簡(jiǎn)易計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Android studio設(shè)計(jì)簡(jiǎn)易計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04Android 推送原理(Android Push Notification)詳解
這篇文章主要介紹了Android 推送原理(Android Push Notification)詳解的相關(guān)資料,這里對(duì)Android 推送的原理做了簡(jiǎn)單的介紹,需要的朋友可以參考下2016-11-11Android Webview的postUrl與loadUrl加載頁(yè)面實(shí)例
這篇文章主要介紹了Android Webview的postUrl與loadUrl加載頁(yè)面實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03Android提高之BroadcastReceiver實(shí)例詳解
這篇文章主要介紹了Android的BroadcastReceiver用法,在Android的項(xiàng)目開(kāi)發(fā)中是比較實(shí)用的功能,需要的朋友可以參考下2014-08-08使用kotlin實(shí)現(xiàn)MVP的方式(簡(jiǎn)單好用)
這篇文章主要介紹了使用kotlin實(shí)現(xiàn)MVP的方式(簡(jiǎn)單好用),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03