为了在我使用RecyclerView
的列表中显示我的字符串,我的字符串很大且是波斯语因此对于美丽的UI我想要证明它们是合理的。为此,我使用了Text-Justify library。现在我有一个漂亮的合理文本。
但由于DocumentView
的计算量很大(简单来说:一个合理的TextView
),我的RecyclerView
并不能顺利滚动并且有滞后现象。所以我决定在显示RecyclerView
之前进行这些计算,而是显示进度。所以我将setAdapter
放在AsyncTask
中并从我的字符串创建DocumentView
但是在运行时我得到的错误是我不允许在另一个线程而不是主线程中添加视图。
如果我将DocumentView
的宽度和高度设置为somthing,我的RecyclerView
滚动非常漂亮。如何计算另一个线程中的DocumentView宽度和高度,以便在计算宽度和高度时向用户显示进度。
private class myAdapter extends RecyclerView.Adapter<ItemViewHolder> {
private Context context;
private List<Item> items;
private ArrayList<DocumentView> justifiedBodies;
public ItemAdapter(Context context, List<Item> items){
this.items = items;
this.context = context;
justifiedBodies = new ArrayList<>();
for(Item item:Items){
Spanned span = Html.fromHtml(item.getBody());
justifiedBodies.add(createDocumentView(span, DocumentView.FORMATTED_TEXT));
}
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.my_layout, parent, false);
return new ItemViewHolder(itemView);
}
@Override
public void onBindViewHolder(final ItemViewHolder holder, int position) {
final Item item = items.get(position);
DocumentView justifiedDocumentView = justifiedBodies.get(position);
if(justifiedDocumentView.getParent() != null){
((ViewGroup)justifiedDocumentView.getParent()).removeView(justifiedDocumentView);
}
holder.justifiedBody.removeAllViews();
holder.justifiedBody.addView(justifiedDocumentView);
}
@Override
public int getItemCount() {
return items.size();
}
}
public DocumentView createDocumentView(Spanned article, int type) {
final DocumentView documentView = new DocumentView(getActivity(), type);
documentView.getDocumentLayoutParams().setAntialias(true);
documentView.getDocumentLayoutParams().setHyphenated(false);
documentView.getDocumentLayoutParams().setMaxLines(100);
documentView.getDocumentLayoutParams().setOffsetX(10);
documentView.getDocumentLayoutParams().setOffsetY(10);
documentView.getDocumentLayoutParams().setTextSubPixel(true);
documentView.getDocumentLayoutParams().setWordSpacingMultiplier(5.0f);
documentView.getDocumentLayoutParams().setTextColor(0xff000000);
documentView.getDocumentLayoutParams().setTextTypeface(ResourceManager.iranTF);
documentView.getDocumentLayoutParams().setTextSize(23);
documentView.getDocumentLayoutParams().setTextAlignment(TextAlignment.JUSTIFIED);
documentView.getDocumentLayoutParams().setInsetPaddingLeft(30f);
documentView.getDocumentLayoutParams().setInsetPaddingRight(30f);
documentView.getDocumentLayoutParams().setInsetPaddingTop(30f);
documentView.getDocumentLayoutParams().setInsetPaddingBottom(30f);
documentView.getDocumentLayoutParams().setLineHeightMultiplier(1f);
documentView.getDocumentLayoutParams().setReverse(true);
documentView.setText(article);
// I want to calculate width and height and write them here
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(CALCULATED_WIDTH,
CALCULATED_HEIGHT);
documentView.setLayoutParams(layoutParams);
documentView.setCacheConfig(DocumentView.CacheConfig.AUTO_QUALITY);
return documentView;
}
public static class ItemViewHolder extends RecyclerView.ViewHolder
{
protected FrameLayout justifiedBody;
public ItemViewHolder(View itemView) {
super(itemView);
justifiedBody = (FrameLayout) itemView.findViewById(R.id.justified_body);
}
}
这是我失败的AsyncTask:
class uiDecore extends AsyncTask<String, String, String>{
private ItemAdapter itemAdapter;
private ArrayList<Item> items;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressView.start();
}
@Override
protected String doInBackground(String... params) {
// init Cards
items = DataSrc.getSelectedItem(); // query from my database
recyclerView = (RecyclerView) getActivity().findViewById(R.id.cards_recyclerview);
llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
// I don't allow this because I'm not in main thread!
recyclerView.setLayoutManager(llm);
itemAdapter = new ItemAdapter(getActivity(), items);
// in run time i get error becuse I allocate a new DocumentView
recyclerView.setAdapter(itemAdapter);
recyclerView.setHasFixedSize(true);
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
progressView.stop();
}
}
我在我片段的OnActivityCreated
中调用了uiDecore.execute。