Android realizes reusable selection page

Time:2021-1-25

This example for you to share the Android implementation of reusable selection page specific code, for your reference, the specific content is as follows

Window code

/**
 *According to the ismultipleenable sent from the previous page, judge whether you can select more than one
 *1. Select an entry on the radio page and return it directly
 *2. Submit after selecting the multiple choice page
 *3. Refresh the list after submitting data
 */
public class UserSelectActivity extends AppCompatActivity implements View.OnClickListener {

  private static final String TAG = "UserSelectActivity";

  @BindView(R.id.tv_title_middle)
  TextView title;
  @BindView(R.id.title_left)
  ImageView back;
  @BindView(R.id.normal_user_listview)
  ListView mListView;
  @BindView(R.id.edit_tv)
  TextView edit;
  @BindView(R.id.filter_tv)
  TextView filter;

  private Context mContext;
  private boolean isMultipleEnable;
  private UserSelectAdapter mAdapter;
  private List<ClsNormalUser> mClsNormalUserList;

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_normal_user_list);
    //Initialize butterknife
    ButterKnife.bind(this);

    initView();
    initData();
    initListener();
  }

  private void initData() {
    mContext = this;

    //The corresponding view is generated by the value from the source page. The default is single selection
    isMultipleEnable = getIntent().getBooleanExtra("isMultipleEnable", false);

    //Initialize data source
    mClsNormalUserList = new ArrayList<>();
    mAdapter = new UserSelectAdapter(mClsNormalUserList, mContext);
    mListView.setAdapter(mAdapter);

    getOfflineData(20);

    //The submit button and check box should be displayed on the multiple choice page
    if (isMultipleEnable) {
      edit.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_send_black_24dp, 0, 0, 0);
      edit.setVisibility(View.VISIBLE);

      for (ClsNormalUser user : mClsNormalUserList) {
        user.setIsCheckBoxVisible(true);
      }
    }
  }

  //Generate simulation data
  private void getOfflineData(int num) {
    List<ClsNormalUser> clsNormalUserList = new ArrayList<>();
    for (int i = 0; i < num; i++) {
      ClsNormalUser clsNormalUser = new ClsNormalUser();
      clsNormalUser.setUserID("userID " + i);
      clsNormalUser.setWorkNumber("workNumber " + i);
      clsNormalUser.setUserType("userType " + i);
      clsNormalUser.setOrgName("orgName " + i);
      clsNormalUser.setCName("cName " + i);
      clsNormalUser.setSysUserName("sysUserName " + i);
      clsNormalUser.setOrgID("orgID " + i);
      clsNormalUser.setWorkPos("workPos " + i);
      clsNormalUserList.add(clsNormalUser);
    }
    //After data filling, the adapter should refresh the view at the same time
    mClsNormalUserList.addAll(clsNormalUserList);
    mAdapter.notifyDataSetChanged();
  }

  private void initView() {
    title.setText ("select user");
  }

  private void initListener() {
    edit.setOnClickListener(this);

    mListView.setOnItemClickListener((parent, view, position, id) -> {
      ClsNormalUser clsNormalUser = mClsNormalUserList.get(position);
      if (isMultipleEnable) {
        //Click to select the current item in multiple selection status
        if (clsNormalUser.getIsCheckBoxVisible()) {
          clsNormalUser.setIsChecked(!clsNormalUser.getIsChecked());
          mAdapter.notifyDataSetChanged();
        }
      } else {
        //In radio mode, click to end the current page and return the value
        Intent intent = new Intent();
        //The entity class should implement the serializable interface Parcelable
        intent.putExtra("user", clsNormalUser);
        setResult(RESULT_OK, intent);
        finish();
      }
    });
  }


  @Override
  public void onClick(View v) {

    //Click the submit button to return the selected value and end the current page
    if (v.getId() == R.id.edit_tv) {
      ArrayList<ClsNormalUser> normalUserList = new ArrayList<>();
      for (ClsNormalUser normalUser : mClsNormalUserList) {
        if (normalUser.getIsChecked()) {
          normalUserList.add(normalUser);
        }
      }
      Intent intent = new Intent();
      //The entity class should implement the serializable interface Parcelable
      intent.putParcelableArrayListExtra("users", normalUserList);
      setResult(RESULT_OK, intent);
      finish();
    }
  }
}

adapter code

public class UserSelectAdapter extends BaseAdapter {

  private List<ClsNormalUser> mList;
  private LayoutInflater mInflater;

  public UserSelectAdapter(List<ClsNormalUser> list, Context mContext) {
    mList = list;
    mInflater = LayoutInflater.from(mContext);
  }

  @Override
  public int getCount() {
    return mList.size();
  }

  @Override
  public ClsNormalUser getItem(int position) {
    return mList.get(position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @Override
  public View getView(final int position, View convertView, ViewGroup root) {
    ItemHolder holder;
    ClsNormalUser clsNormalUser = mList.get(position);

    if (convertView == null) {
      convertView = mInflater.inflate(R.layout.item_alluser_sys, null);
      holder = new ItemHolder();
      holder.id = convertView.findViewById(R.id.user_id);
      holder.name = convertView.findViewById(R.id.user_name);
      holder.org = convertView.findViewById(R.id.user_org);

      holder.checkBox = convertView.findViewById(R.id.user_checkbox);
      convertView.setTag(holder);
    } else {
      holder = (ItemHolder) convertView.getTag();
    }

    holder.id.setText(clsNormalUser.getUserID());
    holder.name.setText(clsNormalUser.getCName());
    holder.org.setText(clsNormalUser.getOrgName());

    //Show checkbox
    if (clsNormalUser.getIsCheckBoxVisible()) {
      holder.checkBox.setVisibility(View.VISIBLE);
    } else {
      holder.checkBox.setVisibility(View.GONE);
    }

    //Is the entry selected
    if (clsNormalUser.getIsChecked()) {
      holder.checkBox.setImageResource(R.drawable.ic_check_box_black_24dp);
    } else {
      holder.checkBox.setImageResource(R.drawable.ic_check_box_outline_blank_black_24dp);
    }

    return convertView;
  }

  class ItemHolder {
    TextView id;
    TextView name;
    TextView org;
    ImageView checkBox;
  }
}

The effect is shown in the figure

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

Recommended Today

How to Build a Cybersecurity Career

Original text:How to Build a Cybersecurity Career How to build the cause of network security Normative guidelines for building a successful career in the field of information security fromDaniel miesslerstayinformation safetyCreated / updated: December 17, 2019 I’ve been doing itinformation safety(now many people call it network security) it’s been about 20 years, and I’ve spent […]