Android realizes the new function in multi level list

Time:2021-1-23

In this article, we share the new functions in the multi-level list implemented by Android for your reference. The specific contents are as follows

Multi level list page implementation is relatively simple, so the new function out.

Window code

/**
 *Create a new entry in the first level list
 *1. Choose both pictures and accessories Intent.ACTION_ GET_ Content implementation
 *2. Open the file with Intent.ACTION_ Implementation of view
 *3. The returned URI needs to be converted into a real path
 *4. Refresh the list after submitting data
 */

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;

  //Initialize data source
  mAttachList = new ArrayList<>();
  mAdapter = new AttachmentListAdapter(mAttachList, mContext);
  mListView.setAdapter(mAdapter);
 }

 private void initView() {
  title.setText (the "new board");

  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);

  //Click the delete button of the attachment list entry to delete the corresponding attachment
  mAdapter.setmCallback((view, position) -> {
   mAttachList.remove(position);
   mAdapter.notifyDataSetChanged();
  });


  //Click the attachment list to open it
  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) {
   //Open the native file manager of the mobile phone, and select the content
   Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
   intent.addCategory(Intent.CATEGORY_OPENABLE);
   //The file type is picture
   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) {

   //The number of uploaded attachments cannot exceed 4
   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, "the number of attachments has reached the maximum! ",  Toast.LENGTH_ SHORT).show();
   }
  }
 }

 private void submit() {
  Toast.makeText (mcontext, "call interface here! ",  Toast.LENGTH_ SHORT).show();
  finish();
 }

 @Override
 //The result code should be the default value of result_ OK
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {

  //After selecting the picture, use glide to load it to the control. Sometimes you need to upload the picture to the background
  //When submitting data, transfer the image to the background path
  if (requestCode == 16352 && resultCode == RESULT_OK) {
   Glide.with(mContext).load(data.getData()).into(sectionLogo);
  }

  //Open the page of user selection, and the page will be different according to the parameters. The default is single selection page
  if (requestCode == 12345 && resultCode == RESULT_OK) {
   ClsNormalUser user = data.getParcelableExtra("user");
   sectionManager.setText(user.getCName());
  }

  //Traverse the list of uploaded attachments, and pop up a prompt if it already exists
  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, "please select a different file! ",  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) {
  //Here, we use the getpath method of ucrop, a third-party library. We can also convert URI to path by ourselves
  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());

  //Here you need to call the upload interface
  uploadFile(file.getPath());

  mAttachList.add(clsAttachMent);
  mAdapter.notifyDataSetChanged();
 }

 private void uploadFile(String path) {
  Toast.makeText (mcontext, "call interface here! ",  Toast.LENGTH_ SHORT).show();
 }
}

Layout file code

<?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= "Please enter a name"
   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= "Please input content"
   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= Select Moderator:
    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= File name:
    android:textColor="@color/black" />

   <TextView
    android:layout_width="100dp"
    android:layout_height="30dp"
    android:gravity="center_vertical"
    android:text= File size:
    android:textColor="@color/black" />

   <TextView
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:gravity="center_vertical"
    android:text= Action:
    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= "Add attachment"
   android:textColor="@color/black"
   android:textSize="16sp" />
 </LinearLayout>
</ScrollView>

adapter code

public class AttachmentListAdapter extends BaseAdapter {

 private List<ClsAttachMent> mList;
 private LayoutInflater mInflater;
 private Callback mCallback;

 //Custom callback interface for value transfer
 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");

  //Put position in tag
  holder.delete.setTag(position);

  holder.delete.setOnClickListener(v -> {
   //When the click event is triggered, the position will be returned
   mCallback.onClick(v, (Integer) v.getTag());
  });
  return convertView;
 }

 private class ViewHolder {

  TextView name;
  TextView size;
  TextView delete;
 }
}

The effect is as follows:

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.