Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

java - Using ViewTreeObserver i am adding setMaxLines and setEllipsize in MaterialTextView inside RecyclerView.Adapter

  • I am set MaterialTextView inside RelativeLayout and set RelativeLayout size programmatically different size for every device.

  • Then i have using ViewTreeObserver to set setMaxLines and setEllipsize in MaterialTextView but i am facing some problem show the text in MaterialTextView 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 list

  • phone 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);

        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

recyclerView.setHasFixedSize(true);

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 the ViewHolder for Quote. Hence the following should throw an error.

quotes.textView_category.setText(subCategoryLists.get(position).getCategory_name());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...