간단한 데이터를 보여주는 View라서 ListView로 구현하였는데,
스크롤 하면 데이터가 없어지는 이슈가 있었다. ViewHolder패턴으로 할까, ListViewRecyclerView로 구현할까 고민하다가
RecyclerView로 구현하였다. (블로그들의 이유 때문에..)
또 새로운 사실을 알았다!! 역시 Android 기본 공부& 구현을 많이 해야게따!!
[ListViewAdapter.java]
package com.zodiac.aries.screen.registerdoc;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import com.zodiac.aries.R;
import com.zodiac.aries.databinding.LayoutItemSelectModuleBinding;
import com.zodiac.aries.screen.registerdoc.data.Module;
import java.util.ArrayList;
import java.util.List;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStoreOwner;
/**
* @author khji
* <p>
* 모듈 추가/수정 다이얼로그 - lvRegisterDocModule 에서 사용하는 Adapter
*/
public class SelectModuleAdapter extends BaseAdapter {
private static final String TAG = SelectModuleAdapter.class.getSimpleName();
private LayoutItemSelectModuleBinding mBinding;
private LayoutInflater layoutInflater;
private ModuleViewModel moduleViewModel;
public void setModuleList(List<Module> moduleList) {
this.moduleList = moduleList;
}
private List<Module> moduleList;
public SelectModuleAdapter(Activity activity) {
this.layoutInflater = LayoutInflater.from(activity);
this.moduleViewModel = new ViewModelProvider((ViewModelStoreOwner) activity).get(ModuleViewModel.class);
this.moduleList = new ArrayList<>();
}
public SelectModuleAdapter(Activity activity, List<Module> moduleList) {
this.layoutInflater = LayoutInflater.from(activity);
this.moduleViewModel = new ViewModelProvider((ViewModelStoreOwner) activity).get(ModuleViewModel.class);
this.moduleList = moduleList;
}
@Override
public int getCount() {
return moduleList.size();
}
@Override
public Module getItem(int position) {
return moduleList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
mBinding = DataBindingUtil.inflate(layoutInflater, R.layout.layout_item_select_module, parent, false);
mBinding.setAdapter(this);
mBinding.setViewModel(moduleViewModel);
Module moduleInfo = moduleList.get(position);
mBinding.selectModuleItemName.setText(moduleInfo.getName() + " v" + moduleInfo.getVersion());
mBinding.selectModuleItemNum.setText(String.valueOf(moduleInfo.getCopy()));
CheckBox cbModuleItem = mBinding.selectModuleItemCheck;
Button btnModuleItemMinus = mBinding.selectModuleItemNumMinus;
Button btnModuleItemPlus = mBinding.selectModuleItemNumPlus;
TextView tvSelectModuleItemNum = mBinding.selectModuleItemNum;
cbModuleItem.setTag(position);
boolean isRequireYn = moduleInfo.isRequireYn();
cbModuleItem.setChecked(isRequireYn);
cbModuleItem.setClickable(!isRequireYn);
cbModuleItem.setEnabled(!isRequireYn);
cbModuleItem.setOnCheckedChangeListener(((buttonView, isChecked) -> {
if (!isChecked) {
tvSelectModuleItemNum.setText(String.valueOf(0));
} else {
tvSelectModuleItemNum.setText(String.valueOf(1));
}
}));
// if (selectedSeqList != null && selectedSeqList.size() > 0) {
// for (String moduleSeq : selectedSeqList) {
// if (moduleInfo.getModuleSeq().equals(moduleSeq)) {
// cbModuleItem.setChecked(true);
// cbModuleItem.setClickable(false);
// mBinding.selectModuleItemNum.setText(String.valueOf(1));
// }
// }
// }
btnModuleItemMinus.setTag(position);
btnModuleItemMinus.setOnClickListener(view -> changeCopyNum(cbModuleItem, tvSelectModuleItemNum, isRequireYn, false));
btnModuleItemPlus.setTag(position);
btnModuleItemPlus.setOnClickListener(view -> changeCopyNum(cbModuleItem, tvSelectModuleItemNum, isRequireYn, true));
return mBinding.getRoot();
}
public void changeCopyNum(CheckBox cbModuleItem, TextView tvSelectModuleItemNum, boolean requireYn, boolean isPlus) {
int getCopy = Integer.parseInt(tvSelectModuleItemNum.getText().toString());
int change = getCopy + ((isPlus) ? 1 : -1);
int limit = (requireYn) ? 1 : 0;
int copy = (change < limit) ? limit : change;
cbModuleItem.setChecked(copy > 0);
tvSelectModuleItemNum.setText(String.valueOf(copy));
}
}
- 참고
https://woovictory.github.io/2019/01/03/Android-Diff-of-ListView-and-RecyclerView/
http://dudmy.net/android/2017/06/23/consider-of-recyclerview/
반응형
'Platform > Android' 카테고리의 다른 글
[다운로드] (0) | 2020.06.02 |
---|---|
[Zxing] (0) | 2020.06.02 |
[Android Studio/Eclipse] keymap-단축키 (0) | 2020.05.13 |
[debug] Room Database / SharedPreferences 디버깅 (0) | 2020.04.14 |
[Error] Room - cannot find implementation for 패키지명. (0) | 2020.04.14 |