@Override public void onAnimationEnd(Animation animation) { // clear animation here to avoid repeated applyTransformation() calls clearAnimation(); // clear the animation flag mAnimating = false; // notify the listener if (mListener != null) { mListener.onExpandStateChanged(mTv, !mCollapsed); } } @Override
public ExpandCollapseAnimation(View view, int startHeight, int endHeight) { mTargetView = view; mStartHeight = startHeight; mEndHeight = endHeight; setDuration(mAnimationDuration); }
public ExpandableTextView(Context context, AttributeSet attrs) { super(context, attrs); init(attrs); }
mExpandIndicatorController.changeState(mCollapsed); animation = new ExpandCollapseAnimation(this, getHeight(), mCollapsedHeight); } else { animation = new ExpandCollapseAnimation(this, getHeight(), getHeight() + mTextHeightWithMaxLines - mTv.getHeight()); }); clearAnimation(); startAnimation(animation);
public void setText(@Nullable CharSequence text, @NonNull SparseBooleanArray collapsedStatus, int position) { mCollapsedStatus = collapsedStatus; mPosition = position; boolean isCollapsed = collapsedStatus.get(position, true); clearAnimation(); mCollapsed = isCollapsed; mExpandIndicatorController.changeState(mCollapsed); setText(text); }
public void setText(@Nullable CharSequence text) { mRelayout = true; mTv.setText(text); setVisibility(TextUtils.isEmpty(text) ? View.GONE : View.VISIBLE); clearAnimation(); getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT; requestLayout(); }
private void init(AttributeSet attrs) { TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.ExpandableTextView); mMaxCollapsedLines = typedArray.getInt(R.styleable.ExpandableTextView_maxCollapsedLines, MAX_COLLAPSED_LINES); mAnimationDuration = typedArray.getInt(R.styleable.ExpandableTextView_animDuration, DEFAULT_ANIM_DURATION); mAnimAlphaStart = typedArray.getFloat(R.styleable.ExpandableTextView_animAlphaStart, DEFAULT_ANIM_ALPHA_START); mExpandableTextId = typedArray.getResourceId(R.styleable.ExpandableTextView_expandableTextId, R.id.expandable_text); mExpandCollapseToggleId = typedArray.getResourceId(R.styleable.ExpandableTextView_expandCollapseToggleId, R.id.expand_collapse); mExpandToggleOnTextClick = typedArray.getBoolean(R.styleable.ExpandableTextView_expandToggleOnTextClick, true); mExpandIndicatorController = setupExpandToggleController(getContext(), typedArray); typedArray.recycle(); // enforces vertical orientation setOrientation(LinearLayout.VERTICAL); // default visibility is gone setVisibility(GONE); }
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_demo1, container, false); ((TextView) rootView.findViewById(R.id.sample1).findViewById(R.id.title)).setText("Sample 1"); ((TextView) rootView.findViewById(R.id.sample2).findViewById(R.id.title)).setText("Sample 2"); ExpandableTextView expTv1 = (ExpandableTextView) rootView.findViewById(R.id.sample1) .findViewById(R.id.expand_text_view); ExpandableTextView expTv2 = (ExpandableTextView) rootView.findViewById(R.id.sample2) .findViewById(R.id.expand_text_view); expTv1.setOnExpandStateChangeListener(new ExpandableTextView.OnExpandStateChangeListener() { @Override public void onExpandStateChanged(TextView textView, boolean isExpanded) { Toast.makeText(getActivity(), isExpanded ? "Expanded" : "Collapsed", Toast.LENGTH_SHORT).show(); } }); expTv1.setText(getString(R.string.dummy_text1)); expTv2.setText(getString(R.string.dummy_text2)); return rootView; } }
private void findViews() { mTv = (TextView) findViewById(mExpandableTextId); if (mExpandToggleOnTextClick) { mTv.setOnClickListener(this); } else { mTv.setOnClickListener(null); } mToggleView = findViewById(mExpandCollapseToggleId); mExpandIndicatorController.setView(mToggleView); mExpandIndicatorController.changeState(mCollapsed); mToggleView.setOnClickListener(this); }
private static ExpandIndicatorController setupExpandToggleController(@NonNull Context context, TypedArray typedArray) { final int expandToggleType = typedArray.getInt(R.styleable.ExpandableTextView_expandToggleType, DEFAULT_TOGGLE_TYPE); final ExpandIndicatorController expandIndicatorController; switch (expandToggleType) { case EXPAND_INDICATOR_IMAGE_BUTTON: Drawable expandDrawable = typedArray.getDrawable(R.styleable.ExpandableTextView_expandIndicator); Drawable collapseDrawable = typedArray.getDrawable(R.styleable.ExpandableTextView_collapseIndicator); if (expandDrawable == null) { expandDrawable = getDrawable(context, R.drawable.ic_expand_more_black_12dp); } if (collapseDrawable == null) { collapseDrawable = getDrawable(context, R.drawable.ic_expand_less_black_12dp); } expandIndicatorController = new ImageButtonExpandController(expandDrawable, collapseDrawable); break; case EXPAND_INDICATOR_TEXT_VIEW: String expandText = typedArray.getString(R.styleable.ExpandableTextView_expandIndicator); String collapseText = typedArray.getString(R.styleable.ExpandableTextView_collapseIndicator); expandIndicatorController = new TextViewExpandController(expandText, collapseText); break; default: throw new IllegalStateException("Must be of enum: ExpandableTextView_expandToggleType, one of EXPAND_INDICATOR_IMAGE_BUTTON or EXPAND_INDICATOR_TEXT_VIEW."); } return expandIndicatorController; }
@Override public View getView(int position, View convertView, ViewGroup parent) { final ViewHolder viewHolder; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false); viewHolder = new ViewHolder(); viewHolder.expandableTextView = (ExpandableTextView) convertView.findViewById(R.id.expand_text_view); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.expandableTextView.setText(sampleStrings[position], mCollapsedStatus, position); return convertView; }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (!mRelayout || getVisibility() == View.GONE) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); return; mTextHeightWithMaxLines = getRealTextViewHeight(mTv); mCollapsedHeight = getMeasuredHeight();
@Override public void run() { mMarginBetweenTxtAndBottom = getHeight() - mTv.getHeight(); } });
@Override protected void onFinishInflate() { super.onFinishInflate(); findViews(); }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private static Drawable getDrawable(@NonNull Context context, @DrawableRes int resId) { Resources resources = context.getResources(); if (isPostLolipop()) { return resources.getDrawable(resId, context.getTheme()); } else { return resources.getDrawable(resId); } }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) private static void applyAlphaAnimation(View view, float alpha) { if (isPostHoneycomb()) { view.setAlpha(alpha); } else { AlphaAnimation alphaAnimation = new AlphaAnimation(alpha, alpha); // make it instant alphaAnimation.setDuration(0); alphaAnimation.setFillAfter(true); view.startAnimation(alphaAnimation); } }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) public ExpandableTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(attrs); }