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

后端 未结 3 1990
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-12 06:28
  • I am set MaterialTextView inside RelativeLayout and set RelativeLayout size programmatically different size for every device.

相关标签:
3条回答
  • 2020-12-12 06:58

    One problem I can see is that you are calling

    quotes.textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    

    in the first line of your listener callback no matter what. In a RecyclerView the first few times this is called the layout might still be unmeasured but you're cancelling the listener callback after the first callback. So this line fires, and if the view is still unmeasured then the next few lines are going to fail and your code will get no more opportunities to fill the view. Try checking the measuredWidth or measuredHeight of your view for something greater than 0 before cancelling future listener callbacks.

    0 讨论(0)
  • 2020-12-12 07:01

    I have using this. works perfectly

    textView.setText(subCategoryLists.get(position).getStatus_title());
    textView.post(new Runnable() {
        @Override
        public void run() {
            ViewGroup.LayoutParams params = textView.getLayoutParams();
            if (params == null) {
                params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            }
            final int widthSpec = View.MeasureSpec.makeMeasureSpec(textView.getWidth(), View.MeasureSpec.UNSPECIFIED);
            final int heightSpec = View.MeasureSpec.makeMeasureSpec(textView.getHeight(), View.MeasureSpec.UNSPECIFIED);
            textView.measure(widthSpec, heightSpec);
            textView.setMaxLines(heightSpec / textView.getLineHeight());
            textView.setEllipsize(TextUtils.TruncateAt.END);
        }
    });
    
    0 讨论(0)
  • 2020-12-12 07:12

    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());
    
    0 讨论(0)
提交回复
热议问题