我在recyclerview中创建了一个字符串文件,但它不接受html代码。 我怎么能输入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);
我的字符串文件;
<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>
编辑: 我设置我的适配器,但它给出了一个错误(无法解决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) {
}
答案 0 :(得分:0)
在您的活动中,使用recyclerView创建一个FlowerData列表。如下:
private List<FlowerData> mFlowerList = new ArrayList<>();
使用FlowerData对象填充它,如下所示:
mFlowerList.add(new FlowerData("Rose", getString(R.string.html),
R.drawable.rose));
将此列表传递给您的adpater,如下所示:
MyAdapter myAdapter = new MyAdapter(this, mFlowerList, itemClickListener);
将适配器设置为recyclerView:
recyclerView.setAdapter(languageAdapter);
创建一个界面来处理对recyclerView项目的点击(为此创建一个单独的文件):
public interface OnMyItemClickListener {
void onItemClick();
}
在您的活动中创建此侦听器的实例:
private OnMyItemClickListener itemClickListener = new OnMyItemClickListener() {
@Override
public void onItemClick() {
// do your fuctionality on itemClick
}
};
现在,您的适配器应如下所示:
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;
}
}
}