RecyclerView Adapter 删除滚动条上的填充内容
RecyclerViewAdapter removed fill content on scroll
我将 RecyclerViewAdapter 用于我的 json adaper.i 以编程方式在 RecyclerViewAdapter 中创建一些单选按钮和复选框以及编辑文本,但是当我检查一些单选按钮或输入编辑文本并滚动超过 1 个屏幕时回来我所有的填充内容都被删除了;
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
public MyRecyclerViewAdapter(Context context, ArrayList<HashMap<String, Object>> hash_all) {
}
@Override
public int getItemCount() {
return hash_all.size();
}
@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
hash_all.size();
hash_get = hash_all.get(position);
LinearLayoutCompat.LayoutParams params = new LinearLayoutCompat.LayoutParams(
LinearLayoutCompat.LayoutParams.MATCH_PARENT,
LinearLayoutCompat.LayoutParams.WRAP_CONTENT
);
params.setMargins(0, 0, 0, 0);
String s = hash_get.get("ModelShowOption").toString();
String toggle = "";
String direc = "";
String type = "";
String model = "";
String rows = "";
String resize = "";
String[] split = s.split("\|");
String[] split2 = new String[0];
for (int i = 0; i < split.length; i++) {
String s2 = split[i];
split2 = s2.split(":");
if (split2[0].equals("isToggleButton")){
toggle=split2[1];
}else if (split2[0].equals("type")){
type=split2[1];
}else if (split2[0].equals("direction")){
direc=split2[1];
}else if (split2[0].equals("model")){
model=split2[1];
}else if (split2[0].equals("rows")){
rows=split2[1];
}else if (split2[0].equals("resize")){
resize=split2[1];
}
}
String title = hash_get.get("title").toString();
String myValue =hash_get.get("MyValue").toString();
String[] splitValue = myValue.split("\|");
RadioGroup[] rg;
RadioButton[] rb;
if (toggle.equals("true") && holder.row_list_title.getText().equals("")){
holder.row_list_placeholder.setVisibility(View.GONE);
holder.row_list_buttn.setVisibility(View.GONE);
String multiValue = hash_get.get("MultiValue").toString();
String[] splitMultiValue = multiValue.split(":");
if (splitMultiValue[1].equals("1")){
int p = 0;
rg = new RadioGroup[1];
rb = new RadioButton[splitValue.length];
rg[0] = new RadioGroup(holder.row_list_placeholder.getContext());
rg[0].setOrientation(RadioGroup.VERTICAL);
for (int j = 0; j < splitValue.length; j++) {
String myValue2 = splitValue[j];
String[] splitValue2 = myValue2.split("=");
rb[j] = new RadioButton(getApplicationContext());
rg[p].addView(rb[j]);
rb[j].setId(j);
rb[j].setText(splitValue2[0]);
}
holder.linearLayoutCheckBox.addView(rg[p]);
p++;
holder.row_list_title.setText(title);
if (rg[0].getCheckedRadioButtonId() == -1) {
} else {
for (int r = 0; r < splitValue.length; r++) {
if (rb[r].isChecked()) {
RadioButton id = (RadioButton) findViewById(rb[r].getId());
String radioText = id.getText().toString();
}
}
}
}else{
holder.row_list_title.setText(title);
CheckBox[] ch;
final int[] checkd = {0};
List<String> checkvl = new ArrayList<>();
ch = new CheckBox[splitValue.length];
for (int j = 0; j < splitValue.length; j++) {
String myValue2 = splitValue[j];
String[] splitValue2 = myValue2.split("=");
ch[j] = new CheckBox(getApplicationContext());
holder.linearLayoutCheckBox.addView(ch[j]);
ch[j].setId(j);
ch[j].setText(splitValue2[0]);
}
for (int r = 0; r < splitValue.length; r++) {
int finalR = r;
int finalR1 = r;
ch[r].setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) {
if (checkd[0] < Integer.parseInt(splitMultiValue[1])){
//checkvl.add(ch[finalR1].getText().toString());
//hash_all_string.get(position).remove("value");
//hash_all_string.get(position).put("value", String.valueOf(checkvl));
//CheckBox id = findViewById(ch[finalR].getId());
//String CheckBoxText = id.getText().toString();
//Log.e("CheckBoxString", CheckBoxText);
checkd[0] = checkd[0] +1;
}else{
ch[finalR].setChecked(false);
}
} else {
ch[finalR].setChecked(false);
checkd[0] = checkd[0] -1;
//checkvl.remove(ch[finalR1].getText().toString());
//hash_all_string.get(position).remove("value");
//hash_all_string.get(position).put("value", String.valueOf(checkvl));
}
});
}
}
}else{
if (myValue.equals("NULL")){
holder.row_list_title.setText(title);
holder.row_list_placeholder.setVisibility(View.VISIBLE);
holder.row_list_buttn.setVisibility(View.GONE);
holder.row_list_placeholder.setHint(hash_get.get("placeholder").toString());
if (type.equals("Text")){
holder.row_list_placeholder.setInputType(InputType.TYPE_CLASS_TEXT);
}else if(type.equals("Int")){
holder.row_list_placeholder.setInputType(InputType.TYPE_CLASS_NUMBER);
}else if(type.equals("Float")){
holder.row_list_placeholder.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
}
}else {
holder.row_list_placeholder.setVisibility(View.GONE);
holder.row_list_buttn.setVisibility(View.VISIBLE);
holder.row_list_title.setText(title);
int selct = 0;
for (int j = 0; j < hash_all2.size(); j++) {
if (hash_all2.get(j).get("selected").equals("true")) {
selct = selct+1;
}
}
if (selct>0){
holder.row_list_buttn.setBackgroundColor(getResources().getColor(R.color.purple_500));
holder.row_list_buttn.setText("انتخاب شد");
}else{
holder.row_list_buttn.setText("انتخاب کنید");
holder.row_list_buttn.setBackgroundColor(getResources().getColor(R.color.teal_200));
}
String multiValue = hash_get.get("MultiValue").toString();
String[] splitMultiValue = multiValue.split(":");
holder.row_list_buttn.setOnClickListener(v -> {
if (hash_all2.size() == 0) {
for (int j = 0; j < splitValue.length; j++) {
HashMap<String, String> hash_add2 = new HashMap<>();
String myValue2 = splitValue[j];
String[] splitValue2 = myValue2.split("=");
hash_add2.put("Id", String.valueOf(j));
hash_add2.put("name", String.valueOf(splitValue2[0]));
hash_add2.put("value", String.valueOf(splitValue2[1]));
hash_add2.put("selected", "false");
hash_add2.put("max", splitMultiValue[1]);
hash_all2.add(hash_add2);
}
adapt.notifyDataSetChanged();
}
alertDialogSelect(position);
});
}
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.row_form, parent, false);
return new ViewHolder(view);
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView row_list_title;
EditText row_list_placeholder;
LinearLayout linearLayoutCheckBox;
Button row_list_buttn;
ViewHolder(View itemView) {
super(itemView);
row_list_title = itemView.findViewById(R.id.row_list_title);
row_list_placeholder = itemView.findViewById(R.id.row_list_placeholder);
linearLayoutCheckBox = itemView.findViewById(R.id.mainrow);
row_list_buttn = itemView.findViewById(R.id.row_list_buttn);
}
}
}
请help.i尝试了堆栈中的许多代码但没有工作
holder.setIsRecyclable(假);
setHasStableIds(真);
这对我不起作用
在 RecyclerView 中移除视图的滚动状态并通过滚动创建新视图时,
您必须在 radioButton 或 edittext 内容更改时更改您的数据,并在您的模型中设置新数据。
我将 RecyclerViewAdapter 用于我的 json adaper.i 以编程方式在 RecyclerViewAdapter 中创建一些单选按钮和复选框以及编辑文本,但是当我检查一些单选按钮或输入编辑文本并滚动超过 1 个屏幕时回来我所有的填充内容都被删除了;
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
public MyRecyclerViewAdapter(Context context, ArrayList<HashMap<String, Object>> hash_all) {
}
@Override
public int getItemCount() {
return hash_all.size();
}
@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
hash_all.size();
hash_get = hash_all.get(position);
LinearLayoutCompat.LayoutParams params = new LinearLayoutCompat.LayoutParams(
LinearLayoutCompat.LayoutParams.MATCH_PARENT,
LinearLayoutCompat.LayoutParams.WRAP_CONTENT
);
params.setMargins(0, 0, 0, 0);
String s = hash_get.get("ModelShowOption").toString();
String toggle = "";
String direc = "";
String type = "";
String model = "";
String rows = "";
String resize = "";
String[] split = s.split("\|");
String[] split2 = new String[0];
for (int i = 0; i < split.length; i++) {
String s2 = split[i];
split2 = s2.split(":");
if (split2[0].equals("isToggleButton")){
toggle=split2[1];
}else if (split2[0].equals("type")){
type=split2[1];
}else if (split2[0].equals("direction")){
direc=split2[1];
}else if (split2[0].equals("model")){
model=split2[1];
}else if (split2[0].equals("rows")){
rows=split2[1];
}else if (split2[0].equals("resize")){
resize=split2[1];
}
}
String title = hash_get.get("title").toString();
String myValue =hash_get.get("MyValue").toString();
String[] splitValue = myValue.split("\|");
RadioGroup[] rg;
RadioButton[] rb;
if (toggle.equals("true") && holder.row_list_title.getText().equals("")){
holder.row_list_placeholder.setVisibility(View.GONE);
holder.row_list_buttn.setVisibility(View.GONE);
String multiValue = hash_get.get("MultiValue").toString();
String[] splitMultiValue = multiValue.split(":");
if (splitMultiValue[1].equals("1")){
int p = 0;
rg = new RadioGroup[1];
rb = new RadioButton[splitValue.length];
rg[0] = new RadioGroup(holder.row_list_placeholder.getContext());
rg[0].setOrientation(RadioGroup.VERTICAL);
for (int j = 0; j < splitValue.length; j++) {
String myValue2 = splitValue[j];
String[] splitValue2 = myValue2.split("=");
rb[j] = new RadioButton(getApplicationContext());
rg[p].addView(rb[j]);
rb[j].setId(j);
rb[j].setText(splitValue2[0]);
}
holder.linearLayoutCheckBox.addView(rg[p]);
p++;
holder.row_list_title.setText(title);
if (rg[0].getCheckedRadioButtonId() == -1) {
} else {
for (int r = 0; r < splitValue.length; r++) {
if (rb[r].isChecked()) {
RadioButton id = (RadioButton) findViewById(rb[r].getId());
String radioText = id.getText().toString();
}
}
}
}else{
holder.row_list_title.setText(title);
CheckBox[] ch;
final int[] checkd = {0};
List<String> checkvl = new ArrayList<>();
ch = new CheckBox[splitValue.length];
for (int j = 0; j < splitValue.length; j++) {
String myValue2 = splitValue[j];
String[] splitValue2 = myValue2.split("=");
ch[j] = new CheckBox(getApplicationContext());
holder.linearLayoutCheckBox.addView(ch[j]);
ch[j].setId(j);
ch[j].setText(splitValue2[0]);
}
for (int r = 0; r < splitValue.length; r++) {
int finalR = r;
int finalR1 = r;
ch[r].setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) {
if (checkd[0] < Integer.parseInt(splitMultiValue[1])){
//checkvl.add(ch[finalR1].getText().toString());
//hash_all_string.get(position).remove("value");
//hash_all_string.get(position).put("value", String.valueOf(checkvl));
//CheckBox id = findViewById(ch[finalR].getId());
//String CheckBoxText = id.getText().toString();
//Log.e("CheckBoxString", CheckBoxText);
checkd[0] = checkd[0] +1;
}else{
ch[finalR].setChecked(false);
}
} else {
ch[finalR].setChecked(false);
checkd[0] = checkd[0] -1;
//checkvl.remove(ch[finalR1].getText().toString());
//hash_all_string.get(position).remove("value");
//hash_all_string.get(position).put("value", String.valueOf(checkvl));
}
});
}
}
}else{
if (myValue.equals("NULL")){
holder.row_list_title.setText(title);
holder.row_list_placeholder.setVisibility(View.VISIBLE);
holder.row_list_buttn.setVisibility(View.GONE);
holder.row_list_placeholder.setHint(hash_get.get("placeholder").toString());
if (type.equals("Text")){
holder.row_list_placeholder.setInputType(InputType.TYPE_CLASS_TEXT);
}else if(type.equals("Int")){
holder.row_list_placeholder.setInputType(InputType.TYPE_CLASS_NUMBER);
}else if(type.equals("Float")){
holder.row_list_placeholder.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
}
}else {
holder.row_list_placeholder.setVisibility(View.GONE);
holder.row_list_buttn.setVisibility(View.VISIBLE);
holder.row_list_title.setText(title);
int selct = 0;
for (int j = 0; j < hash_all2.size(); j++) {
if (hash_all2.get(j).get("selected").equals("true")) {
selct = selct+1;
}
}
if (selct>0){
holder.row_list_buttn.setBackgroundColor(getResources().getColor(R.color.purple_500));
holder.row_list_buttn.setText("انتخاب شد");
}else{
holder.row_list_buttn.setText("انتخاب کنید");
holder.row_list_buttn.setBackgroundColor(getResources().getColor(R.color.teal_200));
}
String multiValue = hash_get.get("MultiValue").toString();
String[] splitMultiValue = multiValue.split(":");
holder.row_list_buttn.setOnClickListener(v -> {
if (hash_all2.size() == 0) {
for (int j = 0; j < splitValue.length; j++) {
HashMap<String, String> hash_add2 = new HashMap<>();
String myValue2 = splitValue[j];
String[] splitValue2 = myValue2.split("=");
hash_add2.put("Id", String.valueOf(j));
hash_add2.put("name", String.valueOf(splitValue2[0]));
hash_add2.put("value", String.valueOf(splitValue2[1]));
hash_add2.put("selected", "false");
hash_add2.put("max", splitMultiValue[1]);
hash_all2.add(hash_add2);
}
adapt.notifyDataSetChanged();
}
alertDialogSelect(position);
});
}
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.row_form, parent, false);
return new ViewHolder(view);
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView row_list_title;
EditText row_list_placeholder;
LinearLayout linearLayoutCheckBox;
Button row_list_buttn;
ViewHolder(View itemView) {
super(itemView);
row_list_title = itemView.findViewById(R.id.row_list_title);
row_list_placeholder = itemView.findViewById(R.id.row_list_placeholder);
linearLayoutCheckBox = itemView.findViewById(R.id.mainrow);
row_list_buttn = itemView.findViewById(R.id.row_list_buttn);
}
}
}
请help.i尝试了堆栈中的许多代码但没有工作
holder.setIsRecyclable(假); setHasStableIds(真);
这对我不起作用
在 RecyclerView 中移除视图的滚动状态并通过滚动创建新视图时,
您必须在 radioButton 或 edittext 内容更改时更改您的数据,并在您的模型中设置新数据。