I am set
MaterialTextView
insideRelativeLayout
and setRelativeLayout
size programmatically different size for every device.Then i have using
ViewTreeObserver
to setsetMaxLines
andsetEllipsize
inMaterialTextView
but i am facing some problem show the text inMaterialTextView
using RecyclerView.Adapter.I am using load more
RecyclerView
i am getting all data then after show text automatically in list and also notify data adapter then show text.not showing text inside
MaterialTextView
in listphone lock on/off then showing data in
MaterialTextView
set recyclerview in fragment
recyclerView = view.findViewById(R.id.recyclerView_sub_category);
recyclerView.setHasFixedSize(true);
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
adapter code
public class SubCategoryAdapter extends RecyclerView.Adapter {
private final int VIEW_TYPE_LOADING = 0;
private final int VIEW_TYPE_ITEM = 1;
private final int VIEW_TYPE_QUOTES = 2;
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_ITEM) {
other data load
} else if (viewType == VIEW_TYPE_QUOTES) {
View v = LayoutInflater.from(activity).inflate(R.layout.quotes_adapter, parent, false);
return new Quotes(v);
} else if (viewType == VIEW_TYPE_LOADING) {
progressbar load
}
return null;
}
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
if (holder.getItemViewType() == VIEW_TYPE_ITEM) {
final ViewHolder viewHolder = (ViewHolder) holder;
// ------------- code -----
} else if (holder.getItemViewType() == VIEW_TYPE_QUOTES) {
final Quotes quotes = (Quotes) holder;
quotes.relativeLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, columnWidth / 2));
Typeface typeface = Typeface.createFromAsset(activity.getAssets(), "text_font/" + subCategoryLists.get(position).getQuote_font());
quotes.textView.setTypeface(typeface);
quotes.textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
quotes.textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int noOfLinesVisible = quotes.textView.getHeight() / quotes.textView.getLineHeight();
quotes.textView.setMaxLines(noOfLinesVisible);
quotes.textView.setEllipsize(TextUtils.TruncateAt.END);
quotes.textView.setText(subCategoryLists.get(position).getStatus_title());
}
});
}
}
@Override
public int getItemCount() {
return subCategoryLists.size() + 1;
}
@Override
public int getItemViewType(int position) {
if (position != subCategoryLists.size()) {
if (subCategoryLists.get(position).getStatus_type().equals("quote")) {
return VIEW_TYPE_QUOTES;
} else {
return VIEW_TYPE_ITEM;
}
} else {
return VIEW_TYPE_LOADING;
}
}
public class Quotes extends RecyclerView.ViewHolder {
private RelativeLayout relativeLayout;
private MaterialTextView textView;
public Quotes(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView_quotes_adapter);
relativeLayout = itemView.findViewById(R.id.rel_quotes_adapter);
}
}
}
I am not completely sure about the problem that you are having there. However, I think I found some problems in your code.
The first problem that I see in setting up your
RecyclerView
is setting the layout size fixed by the following.Looks like you are changing the item layout size dynamically. Hence you need to remove the line above while setting up your
RecyclerView
.The second problem that I see is, there is no
textView_category
in theViewHolder
forQuote
. Hence the following should throw an error.