我完成了我的应用并添加了一些功能。广告示例用户可以更改文字大小。所以我已经创建了一个活动,您可以选择文本大小。这是代码:
public class Options extends AppCompatActivity {
RadioButton r1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_options);
RadioGroup rdgroup = (RadioGroup)findViewById(R.id.radioGroup);
r1 = (RadioButton)findViewById(R.id.radioButton);
r1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ClasseDiAppoggio.setSize(10);
}
});
}
}
这是ClasseDiAppoggi类的代码:
public class ClasseDiAppoggio {
private static float size = 20;
public static void setSize(float sizex){
size = sizex;
}
public static float getSize(){
return size;
}
}
以下是适配器的代码:
public class MyAdapter extends ArrayAdapter<Model> {
static View globalView;
private TreeSet mSeparatorsSet = new TreeSet();
public final List<Model> list;
private final Activity context;
public String textToSay;
private int mCurrentTtsPosition = -1;
Boolean testCheck = false;
public MyAdapter(Activity context, List<Model> list) {
super(context, R.layout.row, list);
this.context = context;
this.list = list;
}
@Override
public int getItemViewType(int position) {
if (list.get(position).getVersion() == 0) {
return 0;
}
if (list.get(position).getVersion() == 2) {
return 2;
}
if (list.get(position).getVersion() == 3) {
return 3;
}
if (list.get(position).getVersion() == 4) {
return 4;
}
if (list.get(position).getVersion() == 5) {
return 5;
}
return 1;
}
@Override
public int getViewTypeCount() {
return 6;
}
public void onStopTts(){
mCurrentTtsPosition = -1;
notifyDataSetChanged();
}
static class ViewHolder {
public TextView text;
public TextView text2;
public CheckBox checkbox;
public TextView textSeparator;
public Button btncheck;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (this.getItemViewType(position)== 0){
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
viewHolder = new ViewHolder();
convertView = inflator.inflate(R.layout.title_row, null);
viewHolder.textSeparator = (TextView) convertView.findViewById(R.id.textView);
convertView.setTag(viewHolder);
convertView.setTag(R.id.textView, viewHolder.textSeparator);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textSeparator.setText(list.get(position).getName());
return convertView;
} else if (this.getItemViewType(position)== 2){
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
viewHolder = new ViewHolder();
convertView = inflator.inflate(R.layout.sub_title_row, null);
viewHolder.textSeparator = (TextView) convertView.findViewById(R.id.textView);
convertView.setTag(viewHolder);
convertView.setTag(R.id.textView, viewHolder.textSeparator);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textSeparator.setText(list.get(position).getName());
return convertView;
} else if (this.getItemViewType(position)== 4){
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
viewHolder = new ViewHolder();
convertView = inflator.inflate(R.layout.row_tri_title, null);
viewHolder.text = (TextView) convertView.findViewById(R.id.textView5);
convertView.setTag(viewHolder);
convertView.setTag(R.id.textView5, viewHolder.text);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.text.setText(list.get(position).getName());
return convertView;
} else if (this.getItemViewType(position)== 5){
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
viewHolder = new ViewHolder();
convertView = inflator.inflate(R.layout.row_tri, null);
viewHolder.text = (TextView) convertView.findViewById(R.id.textView3);
convertView.setTag(viewHolder);
convertView.setTag(R.id.textView3, viewHolder.text);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.text.setText(list.get(position).getName());
return convertView;
} else if (this.getItemViewType(position)== 3){
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.button_row, null);
viewHolder = new ViewHolder();
viewHolder.btncheck = (Button)convertView.findViewById(R.id.btncheck);
convertView.setTag(viewHolder);
convertView.setTag(R.id.btncheck, viewHolder.btncheck);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final ViewHolder finalViewHolder = viewHolder;
viewHolder.btncheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (Model i: list) {
if (i.getVersion() == 1) {
if (!i.isSelected()) {
testCheck = false;
}
}
}
if(testCheck){
OneFragment.startSpeak("è stato controllato tutto");
} else {
OneFragment.startSpeak("Manca qualcosa, ricontrolla");
}
}
});
return convertView;
}
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.text2 = (TextView)convertView.findViewById(R.id.label2);
viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.checkBox);
convertView.setTag(viewHolder);
convertView.setTag(R.id.label, viewHolder.text);
convertView.setTag(R.id.checkBox, viewHolder.checkbox);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.text.setTextSize(ClasseDiAppoggio.getSize());
viewHolder.text2.setTextSize(ClasseDiAppoggio.getSize());
viewHolder.checkbox.setTag(position); // This line is important.
viewHolder.text.setText(list.get(position).getName());
viewHolder.text2.setText(list.get(position).getName2());
viewHolder.checkbox.setChecked(list.get(position).isSelected());
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
}
});
final CheckBox chk = (CheckBox)convertView.findViewById(R.id.checkBox);
final View finalConvertView = convertView;
globalView = convertView;
if (position == mCurrentTtsPosition){
chk.setChecked(true);
}
chk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int getPosition = (Integer) chk.getTag();
if (chk.isChecked()) {
testCheck = true;
String textPart1 = " ";
String textPart2 = " ";
String textToSay = " ";
TextView label2 = (TextView) finalConvertView.findViewById(R.id.label2);
TextView label1 = (TextView) finalConvertView.findViewById(R.id.label);
textPart1 = label2.getText().toString();
int doItearator = 1;
if ((getPosition + 1) < list.size()) {
boolean testItem = false;
do {
if ((list.get(getPosition + doItearator).getVersion() == 1)) {
textPart2 = list.get(getPosition + doItearator).getName();
break;
} else {
if ((getPosition + 2) == list.size() || (getPosition + 3) == list.size()) {
break;
}
++doItearator;
}
} while (testItem == false);
}
textToSay = textPart1 +
"\n" + "." +
"\n" + "." +
"\n" + "." +
"\n" + "." +
"\n" + "." +
"\n" + "." +
"\n" + "." +
"\n" + "." +
"\n" + "." +
"\n" + "." +
"\n" + "." +
textPart2;
OneFragment.startSpeak(textToSay);
} else {
testCheck = false;
}
}
});
return convertView;
}
}
所以代码可行,但问题是当我滚动listview时大小会发生变化。因此,代码中的更改应向下滚动并向上滚动,以便我可以看到文本大小的更改。 我该怎么做才能立即做出改变? 谢谢,对不好的英语抱歉。
答案 0 :(得分:1)
尝试应该有效的youView.invalidate()
。
您也可以尝试ListView.invalidateViews()
。这会强制列表视图重绘其项目。更改textSize时调用它。
在您的情况下,您可以在fragment_one的listView.invalidateViews()
中使用onResume()
。或者为了简化,您可以将listView.setAdapter()
转移到onResume()
,然后就可以了