簡體   English   中英

如何在沒有上下文的android Holder類中使用Font Awesome?

[英]How to use Font Awesome in android Holder class which does not have a context?

我想在我的應用程序中使用字體真棒。 但我不知道如何使用它而不傳遞上下文作為參數。 這是我的xml文件:

    <Button
        android:layout_below="@id/author"
        android:id="@+id/compare_btn"
        android:layout_toRightOf="@id/cart_btn"
        android:layout_width="45dp"
        android:layout_height="wrap_content"
        android:text="@string/fa_random" />

這是我的持有人類,其中的課程不會擴展活動或片段。

    public class ListRowViewHolder extends RecyclerView.ViewHolder {
        protected NetworkImageView productImageURL;
        protected TextView review;
        protected TextView productName;
        protected RelativeLayout recLayout;
        protected TextView categoryId;
        protected TextView price;
        protected Button wishListBtn, cartBtn, compareBtn;

        public ListRowViewHolder(View view) {
            super(view);
            this.productImageURL = (NetworkImageView) view.findViewById(R.id.thumbnail);
            this.productName = (TextView) view.findViewById(R.id.title);
            this.review = (TextView) view.findViewById(R.id.url);
            this.recLayout = (RelativeLayout) view.findViewById(R.id.recLayout);
            this.categoryId = (TextView) view.findViewById(R.id.subreddit);
            this.price = (TextView) view.findViewById(R.id.author);
            this.wishListBtn=(Button) view.findViewById(R.id.wishlist_btn);
  this.cartBtn=(Button) view.findViewById(R.id.cart_btn);//set Font Awesome here
            this.compareBtn=(Button) view.findViewById(R.id.customer_button);//
            view.setClickable(false);
        }

    }

這是我的Recycler適配器中的一個方法,它根據json數據設置textview的文本

 @Override
    public void onBindViewHolder(final ListRowViewHolder listRowViewHolder, int position) {

        ListItems listItems = listItemsList.get(position);
        listRowViewHolder.itemView.setSelected(focusedItem == position);
        String local ="http://carrottech.com/lcart/media/catalog/product/"+listItems.getProductImageURL().trim();
        System.out.print(local+"sdaaaaaaaaaaaaaaaaaaaaaa");
        listRowViewHolder.getLayoutPosition();
        mImageLoader = MySingleton.getInstance(mContext).getImageLoader();
        listRowViewHolder.productImageURL.setImageUrl(local, mImageLoader);
        listRowViewHolder.productImageURL.setDefaultImageResId(R.drawable.reddit_placeholder);

        listRowViewHolder.productName.setText(Html.fromHtml(listItems.getProductName()));
        listRowViewHolder.categoryId.setText(Html.fromHtml(String.valueOf(listItems.getCategoryId())));
        listRowViewHolder.price.setText(Html.fromHtml(String.valueOf(listItems.getProductPrice())));
        listRowViewHolder.review.setText(Html.fromHtml("Write Review"));
        listRowViewHolder.wishListBtn.setText(Html.fromHtml("&#f001;"));
    }

我一直使用這樣的字體類設置Font awesome:

public class FontManager {

    public static final String ROOT = "fonts/",
            FONTAWESOME = "fonts/fontawesome-webfont.ttf";

    public static Typeface getTypeface(Context context, String font) {
        return Typeface.createFromAsset(context.getAssets(), font);
    }

}

在我以前的活動或片段中

  Typeface iconCalendar = FontManager.getTypeface(getContext(), FontManager.FONTAWESOME);
  jcustomerDobIcon.setTypeface(iconCalendar);

但如果沒有傳遞給FontManager類的上下文怎么辦呢? 請幫忙!

如果沒有傳遞給FontManager類的上下文,該怎么做

使用View.getContext()方法從ListRowViewHolder類的View中獲取Context

就像在onBindViewHolder方法中一樣,將Context傳遞給FontManager類:

Typeface iconCalendar = FontManager.getTypeface(
                         listRowViewHolder.wishListBtn.getContext(),
                                     FontManager.FONTAWESOME); 
listRowViewHolder.wishListBtn.setTypeface(iconCalendar);
listRowViewHolder.wishListBtn.setText(Html.fromHtml("&#f001;"));

嘗試這個,

Typeface m_font = FontManager.getTypeface(
                     listRowViewHolder.productName.getContext(),
                                 FontManager.FONTAWESOME); 
listRowViewHolder.wishListBtn.setTypeface(m_font);

你可以根據自己的需要進行設置。

我在構造函數中有一個上下文,我沒有看到,所以這就是我做到的。 希望它能幫助將來的某個人

 public MyRecyclerAdapter(Context context, List<ListItems> listItemsList) {
        this.listItemsList = listItemsList;
        this.mContext = context;
    }

    @Override
    public ListRowViewHolder onCreateViewHolder(final ViewGroup viewGroup, int position) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, null);
        ListRowViewHolder holder = new ListRowViewHolder(v);


        holder.recLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.d("List Size", Integer.toString(getItemCount()));

                TextView redditUrl = (TextView) v.findViewById(R.id.url);
                String postUrl = redditUrl.getText().toString();
                Log.d("THe URS", postUrl);
                Intent intent = new Intent(mContext, WebViewActivity.class);
                intent.putExtra("url", postUrl);
                mContext.startActivity(intent);
            }
        });

        return holder;
    }


    @Override
    public void onBindViewHolder(final ListRowViewHolder listRowViewHolder, int position) {
        Typeface icon = FontManager.getTypeface(mContext, FontManager.FONTAWESOME);

        ListItems listItems = listItemsList.get(position);
        listRowViewHolder.itemView.setSelected(focusedItem == position);
        String local ="http://carrottech.com/lcart/media/catalog/product/"+listItems.getProductImageURL().trim();
        System.out.print(local+"sdaaaaaaaaaaaaaaaaaaaaaa");
        listRowViewHolder.getLayoutPosition();
        mImageLoader = MySingleton.getInstance(mContext).getImageLoader();
        listRowViewHolder.productImageURL.setImageUrl(local, mImageLoader);
        listRowViewHolder.productImageURL.setDefaultImageResId(R.drawable.reddit_placeholder);

        listRowViewHolder.productName.setText(Html.fromHtml(listItems.getProductName()));
        listRowViewHolder.categoryId.setText(Html.fromHtml(String.valueOf(listItems.getCategoryId())));
        listRowViewHolder.price.setText(Html.fromHtml(String.valueOf(listItems.getProductPrice())));
        listRowViewHolder.review.setText(Html.fromHtml("Write Review"));
        listRowViewHolder.wishListBtn.setTypeface(icon);
        listRowViewHolder.cartBtn.setTypeface(icon);
        listRowViewHolder.compareBtn.setTypeface(icon);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM