简体   繁体   English

如何在recyclerview中显示html文本?

[英]How can I show html text in recyclerview?

I created a string file within recyclerview but it doesn't accept html codes. 我在recyclerview中创建了一个字符串文件,但是它不接受html代码。 How could I put in html codes? 如何输入html代码?

   mRecyclerView = findViewById(R.id.recyclerview);
   GridLayoutManager mGridLayoutManager = new GridLayoutManager(MainActivity.this, 2);
  mRecyclerView.setLayoutManager(mGridLayoutManager);

    //  StaggeredGridLayoutManager mStaggeredGridLayoutManager= new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
 //   mRecyclerView.setLayoutManager(mStaggeredGridLayoutManager);
    mFlowerList = new ArrayList<>();
    mFlowerData = new FlowerData("Rose", getString(R.string.html),
            R.drawable.rose);

my string file; 我的字符串文件;

 <string name="html">
    <![CDATA[
    <h1>Main Title</h1>
    <h2>A sub-title</h2>
    <p>This is some html. Look, here\'s an <u>underline</u>.</p>
    <p>Look, this is <em>emphasized.</em> And here\'s some <b>bold</b>.</p>
    <p>This is a UL list:
    <ul>
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
    </ul>
    <p>This is an OL list:
    <ol>
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
    </ol>
    ]]>
</string>

EDİT: I set my adapter like this but it gives an error (cannot resolve metod2setmessage(void)' EDİT:我这样设置适配器,但它给出了一个错误(无法解决metod2setmessage(void)'

I ad adapter but it gives an error: class FlowerViewHolder extends RecyclerView.ViewHolder {
ImageView mImage;
TextView mTitle, tvDescription;
CardView mCardView;
FlowerViewHolder(View itemView) {

    super(itemView);
    mImage = itemView.findViewById(R.id.ivImage);
    mTitle = itemView.findViewById(R.id.tvTitle);
    tvDescription = itemView.findViewById(R.id.tvDescription);

    mCardView = itemView.findViewById(R.id.cardview);
    tvDescription.setMessage(showHtml("Description"));
}
private void showHtml(String description) {

}

In your activity which has recyclerView create a List of FlowerData. 在具有recyclerView的活动中,创建FlowerData列表。 As following: 如下:

private List<FlowerData> mFlowerList = new ArrayList<>();

populate it with FlowerData objects as following: 用FlowerData对象填充它,如下所示:

mFlowerList.add(new FlowerData("Rose", getString(R.string.html),
            R.drawable.rose));

Pass this list to your adpater like this : 将此列表传递给您的adpater,如下所示:

MyAdapter myAdapter = new MyAdapter(this, mFlowerList, itemClickListener);

Set your adapter to recyclerView : 将适配器设置为recyclerView:

recyclerView.setAdapter(languageAdapter);

Create an interface to handle clicks on your recyclerView item (create a seperate file for this): 创建一个界面来处理对您的recyclerView项目的点击(为此创建一个单独的文件):

public interface OnMyItemClickListener {

    void onItemClick();
}

Create an instance of this listener in your activity: 在您的活动中创建此侦听器的实例:

private OnMyItemClickListener itemClickListener = new OnMyItemClickListener() {
        @Override
        public void onItemClick() {
        // do your fuctionality on itemClick

        }
    };

Now, Your adapter should look like following: 现在,您的适配器应如下所示:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private List<FlowerData> mFlowerList;
    private final OnMyItemClickListener  listener;
    private Context context;

    public MyAdapter(Context context, List<FlowerData> mFlowerList, OnMyItemClickListener listener) {
        this.mFlowerList = mFlowerList;
        this.listener = listener;
        this.context = context;
    }

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

        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.textview1, parent, false);

        return new MyViewHolder(itemView);
    }


    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        FlowerData flowerData = mFlowerList.get(position);
        holder.bind(position, flowerData, listener);
    }

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

     class MyViewHolder extends RecyclerView.ViewHolder {

      ImageView mImage;
      TextView mTitle, tvDescription;
      CardView mCardView;

        MyViewHolder(View view) {
            super(view);
            mImage = itemView.findViewById(R.id.ivImage);
            mTitle = itemView.findViewById(R.id.tvTitle);
            tvDescription = itemView.findViewById(R.id.tvDescription);

            mCardView = itemView.findViewById(R.id.cardview);

        }

        public void bind(final int position, final FlowerData flowerData, final OnMyItemClickListener listener) {

            tvDescription.setText(showHtml(flowerData.getString()));


            itemView.setOnClickListener(new View.OnClickListener() {

                @Override public void onClick(View v) {

                    listener.onItemClick();

                }
            });
        }
    @SuppressWarnings("deprecation")
        public static Spanned showHtml(String html) {
        Spanned result;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            result = Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
        } else {
            result = Html.fromHtml(html);
        }
        return result;
    }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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