您好,登錄后才能下訂單哦!
Android中怎么實現可復用的選擇頁面,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
窗口代碼
/** * 根據上一個頁面傳過來的isMultipleEnable判斷是否可以多選 *
單選頁面選中一個條目直接結束并回傳 *
多選頁面選中之后需要提交 *
提交數據之后需要刷新列表 */
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); //初始化ButterKnife ButterKnife.bind(this); initView(); initData(); initListener(); } private void initData() { mContext = this; //通過源頁面傳過來的值來生成對應的視圖,默認是單選 isMultipleEnable = getIntent().getBooleanExtra("isMultipleEnable", false); //初始化數據源 mClsNormalUserList = new ArrayList<>(); mAdapter = new UserSelectAdapter(mClsNormalUserList, mContext); mListView.setAdapter(mAdapter); getOfflineData(20); //多選頁面要顯示提交按鈕和checkbox if (isMultipleEnable) { edit.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_send_black_24dp, 0, 0, 0); edit.setVisibility(View.VISIBLE); for (ClsNormalUser user : mClsNormalUserList) { user.setIsCheckBoxVisible(true); } } } //生成模擬數據 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); } //數據填充后要同時適配器刷新視圖 mClsNormalUserList.addAll(clsNormalUserList); mAdapter.notifyDataSetChanged(); } private void initView() { title.setText("選擇用戶"); } private void initListener() { edit.setOnClickListener(this); mListView.setOnItemClickListener((parent, view, position, id) -> { ClsNormalUser clsNormalUser = mClsNormalUserList.get(position); if (isMultipleEnable) { //多選狀態下點擊選中當前條目 if (clsNormalUser.getIsCheckBoxVisible()) { clsNormalUser.setIsChecked(!clsNormalUser.getIsChecked()); mAdapter.notifyDataSetChanged(); } } else { //單選狀態下點擊結束當前頁并回傳值 Intent intent = new Intent(); //實體類要實現序列化接口Parcelable intent.putExtra("user", clsNormalUser); setResult(RESULT_OK, intent); finish(); } }); } @Override public void onClick(View v) { //點擊提交按鈕將選中的值回傳并結束當前頁 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(); //實體類要實現序列化接口Parcelable intent.putParcelableArrayListExtra("users", normalUserList); setResult(RESULT_OK, intent); finish(); } }}
適配器代碼
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()); //是否顯示checkbox if (clsNormalUser.getIsCheckBoxVisible()) { holder.checkBox.setVisibility(View.VISIBLE); } else { holder.checkBox.setVisibility(View.GONE); } //條目是否選中 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; }}
關于Android中怎么實現可復用的選擇頁面問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。