我试图在我的应用程序中实现动态文本大小选项。出于某种原因,回收商只是在我的卡片视图中随机更改文本大小,而不是将所有文本设置为所需的大小。当我滚动列表时,顶部的卡片视图文本将正确更改,但接下来的 3-4 将保持默认并随机向下列表另一个卡片视图文本将正确显示。当我向上滚动列表时,正确显示的卡片视图将随机更改。
主要活动....
// Dark Mode Menu
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mDrawer.openDrawer(GravityCompat.START);
return true;
case R.id.menu_night_mode_day:
setNightMode(AppCompatDelegate.MODE_NIGHT_NO);
break;
case R.id.menu_night_mode_night:
setNightMode(AppCompatDelegate.MODE_NIGHT_YES);
break;
case R.id.menu_night_mode_auto:
setNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
break;
// Text Size Options
case R.id.menu_text_size_small:
setTextSize(18);
break;
case R.id.menu_text_size_medium:
setTextSize(20);
break;
case R.id.menu_text_size_large:
setTextSize(22);
break;
}
return super.onOptionsItemSelected(item);
}
// Dark Mode Menu
private void setNightMode(@AppCompatDelegate.NightMode int nightMode) {
AppCompatDelegate.setDefaultNightMode(nightMode);
if (Build.VERSION.SDK_INT >= 11) {
recreate();
}
}
// Dynamic text size
private void setTextSize(int textSize) {
TextView description = (TextView) findViewById(R.id.cardview_description);
description.setTextSize(textSize);
saveToPreferences(this, "THE_TEXT_SIZE", "" + textSize);
}
我的适配器....
public class MyPageAdapter extends Adapter<MyPageHolder> {
public List<MenuPageItems> datas;
private Activity activity;
public String dynamicTextSize;
public MyPageAdapter(Activity activity){
datas = new ArrayList<>();
this.activity = activity;
}
public void add(MenuPageItems dataModel){
datas.add(dataModel);
}
public void add(MenuPageItems dataModel, int position){
datas.add(position, dataModel);
}
public void addAll(List<MenuPageItems> menuPageItems){
datas.addAll(menuPageItems);
}
@Override
public MyPageHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
return createViewHolder(v, viewType);
}
@Override
public void onBindViewHolder(MyPageHolder holder, int position) {
holder.bind(datas.get(position), activity, position);
dynamicTextSize = "20";
}
@Override
public int getItemCount() {
return datas.size();
}
@Override
public int getItemViewType(int position){
return datas.get(position).getViewResId();
}
public int searchViewTypePosition(int viewType){
int i = 0;
boolean found = false;
while(i < datas.size() && !found){
if(datas.get(i).getViewResId() == viewType){
found = true;
i--;
}
i++;
}
return i;
}
public MyPageHolder createViewHolder(View v, int viewType){
return datas.get(searchViewTypePosition(viewType)).createViewHolder(v, activity, this);
}
}
持有者....
public abstract class MyPageHolder extends RecyclerView.ViewHolder{
protected final Activity activity;
protected MyPageAdapter adapter;
public TextView txtTitle, txtDescription, txtTheContent;
public ImageView imgImage;
public View view;
public MyPageHolder(View v, Activity activity, MyPageAdapter adapter) {
super(v);
this.activity = activity;
this.adapter = adapter;
imgImage = (ImageView) v.findViewById(R.id.cardview_image);
txtTitle = (TextView) v.findViewById(R.id.cardview_title);
txtDescription = (TextView) v.findViewById(R.id.cardview_description);
view = (CardView) v.findViewById(R.id.card_view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/*/ this is where the magic happens when clicked /*/
}
});
}
public void bind(MenuPageItems dataModel, Activity activity, final int position) {
final MenuPageItems m = (MenuPageItems)dataModel;
imgImage.setImageResource(m.image);
txtTitle.setText(m.title);
txtDescription.setText(m.description);
//txtTheContent.setText(m.theContent);
view.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v){
Intent cvIntent = new Intent(view.getContext(), EndpageActivity.class);
// header image to pass to endpage activity
cvIntent.putExtra("endpageHeader", m.image);
// text to pass to endpage activity
cvIntent.putExtra("endpageTitle", m.title);
cvIntent.putExtra("endpageTheContent", m.theContent);
view.getContext().startActivity(cvIntent);
}
});
}
}
我是否需要向我的适配器或查看器添加一些内容以正确更新所有文本?