For showing my strings in a list I used RecyclerView
, my strings are big and persian so for beatiful UI I want to show them justified. For that I used Text-Justify library. Now I have a beautiful justified text.
BUT because of heavy calculation of DocumentView
(in simple: a justified TextView
) my RecyclerView
doesn't scroll smoothly and has lagging. So I decided to put these calculation before showing RecyclerView
and instead show a progress. So I put setAdapter
in an AsyncTask
and create DocumentView
from my strings but in runtime I get error that I don't be allowed to add view in another thread instead of main thread.
If I set the width and Height of DocumentView
to somthing constant my RecyclerView
scrolls very beautifully. How Can I calculate DocumentView width and height in another Thread so I can show a progress to user while calculating width and height.
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);
}
}
And this is my AsyncTask which fails:
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();
}
}
I call uiDecore.execute in OnActivityCreated
of my fragment.