简体   繁体   中英

Android change TextView textSize on RecyclerView adapter from Activity

i'm trying to find how can i change my RecyclerView adapter textViews from Activity , in my activity i have two widgets such as increment_text_size and decrement_text_size which they must change adapter textviews,

for achieve to that, i create simple listener on activity to manage them:

Activity:

public interface IonChangeBookContentTextSize {
    void incrementTextSize();

    void decrementTextSize();
}

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

and after click on widgets i use this listener on adapter

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();
    }
}

now in adapter i'm using this listener

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);
        }
    }
}

implementing this listener as :

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);
    }
});

on implementingHeadingParagraphView method work for current position, not for all rows on recyclerview adapter, how can i fix this problem?

You do not have to create a listener for this purpose. You should hold a field named textSize in your adapter. Then, set this whenever you want from your 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);
        }
    }

call this from your activity

showBookContentsAdapter.setTextSizes(18);

You have to call notifydatasetchanged from you activity

1.First, save the font size on constant variable if temporary or use shared preferences if need in whole life cycle of app

  1. Make a method in your activity to save font size

    private void saveFontSize(boolean isFont){ IS_LARGE_FONT= isFont; recyclerView.post(new Runnable(){ adapter.notifyDataSetChanged(); }); }
  2. In your adapter class just check that value in bindholder

    if(IS_LARGE_FONT) { //set large font } else{ // set small font }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM