gpt4 book ai didi

Android 从 Activity 更改 RecyclerView 适配器上的 TextView textSize

转载 作者:搜寻专家 更新时间:2023-11-01 08:28:31 24 4
gpt4 key购买 nike

我试图找到如何从 Activity 更改我的 RecyclerView 适配器 textViews,在我的 Activity 中我有两个小部件,例如increment_text_sizedecrement_text_size 它们必须更改适配器 TextView ,

为了实现这一点,我创建了一个简单的 Activity 监听器来管理它们:

Activity :

public interface IonChangeBookContentTextSize {
void incrementTextSize();

void decrementTextSize();
}

public static void setIonChangeBookContentTextSize(IonChangeBookContentTextSize l) {
ionChangeBookContentTextSize = l;
}

点击小部件后,我在适配器上使用这个监听器

Activity :

@OnClick(R.id.decrement_text_size)
public void decrement_text_size(View view) {
if (ionChangeBookContentTextSize != null) {
ionChangeBookContentTextSize.decrementTextSize();
}
}

@OnClick(R.id.increment_text_size)
public void increment_text_size(View view) {
if (ionChangeBookContentTextSize != null) {
ionChangeBookContentTextSize.incrementTextSize();
}
}

现在在适配器中我正在使用这个监听器

public class ShowBookContentsAdapter extends RecyclerView.Adapter<ShowBookContentsAdapter.ShowBookContentsViewHolder> {
private List<Contents> list;
private Context context;
private static final int NOTE = 1;
public static IonChangeBottomViewVisibility ionChangeBottomViewvisibility;
private ShowBookContentsViewHolder holder;
private View view;

public ShowBookContentsAdapter(List<Contents> items, Context mContext, IonChangeBottomViewVisibility mOnChangeBottomViewVisibility) {
list = items;
context = mContext;
ionChangeBottomViewvisibility = mOnChangeBottomViewVisibility;
}

@Override
public ShowBookContentsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
int layout = -1;
switch (viewType) {
case 0:
layout = R.layout.item_book_content_paragraph;
break;
case 1:
layout = R.layout.item_book_content_heading_one;
break;
}

view = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
holder = new ShowBookContentsViewHolder(view);

return holder;
}

@Override
public void onBindViewHolder(ShowBookContentsViewHolder holder, final int position) {
switch (list.get(position).getContentType()) {
case 0:
implementingHeadingParagraphView(holder, position);
break;
case 1:
implementingHeadingOneView(holder, position);
break;
}
}

private void implementingHeadingParagraphView(final ShowBookContentsViewHolder holder, final int position) {
Utils.overrideFonts(context, holder.book_content_paragraph, PersianFontType.SHABNAM);

holder.book_content_paragraph.setText(Html.fromHtml(list.get(position).getContent()));

ActivityShowBookContent.setIonChangeBookContentTextSize(new ActivityShowBookContent.IonChangeBookContentTextSize() {
@Override
public void incrementTextSize() {
holder.book_content_paragraph.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
}

@Override
public void decrementTextSize() {
holder.book_content_paragraph.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
}
});
}

@Override
public int getItemViewType(int position) {
return list.get(position).getContentType();
}

@Override
public int getItemCount() {
return list.size();
}

public int getItemPosition(int itemId) {
return itemPositions.get(itemId);
}

public class ShowBookContentsViewHolder extends RecyclerView.ViewHolder {
@Nullable
@BindView(R.id.book_content_paragraph)
TextView book_content_paragraph;

@Nullable
@BindView(R.id.book_content_heading_one)
TextView book_content_heading_one;

public ShowBookContentsViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
}

将此监听器实现为:

ActivityShowBookContent.setIonChangeBookContentTextSize(new ActivityShowBookContent.IonChangeBookContentTextSize() {
@Override
public void incrementTextSize() {
holder.book_content_paragraph.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
}

@Override
public void decrementTextSize() {
holder.book_content_paragraph.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
}
});

implementingHeadingParagraphView 方法适用于当前位置,不适用于 recyclerview 适配器上的所有行,我该如何解决这个问题?

最佳答案

您不必为此创建监听器。您应该在适配器中保存一个名为 textSize 的字段。然后,在您的 Activity 中随时设置此项。

public class ShowBookContentsAdapter extends RecyclerView.Adapter<ShowBookContentsAdapter.ShowBookContentsViewHolder> {

private int textSize;

// constructor etc.

@Override
public ShowBookContentsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_book_content_paragraph, parent, false);
final ShowBookContentsViewHolder holder new ShowBookContentsViewHolder(view);

return holder;
}

@Override
public void onBindViewHolder(ShowBookContentsViewHolder holder, final int position) {
implementingHeadingParagraphView(holder, position);
}

private void implementingHeadingParagraphView(final ShowBookContentsViewHolder holder, final int position) {
Utils.overrideFonts(context, holder.book_content_paragraph, PersianFontType.SHABNAM);

holder.book_content_paragraph.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);

holder.book_content_paragraph.setText(Html.fromHtml(list.get(position).getContent()));

}

public void setTextSizes(int textSize) {
this.textSize = textSize;
notifyDataSetChanged();
}

//... other adapter methods

public class ShowBookContentsViewHolder extends RecyclerView.ViewHolder {
@Nullable
@BindView(R.id.book_content_paragraph)
TextView book_content_paragraph;

@Nullable
@BindView(R.id.book_content_heading_one)
TextView book_content_heading_one;

public ShowBookContentsViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}

从你的 Activity 中调用它

showBookContentsAdapter.setTextSizes(18);

关于Android 从 Activity 更改 RecyclerView 适配器上的 TextView textSize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42641758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com