bug 场景 recycleView view 复用 。
展开和收起 状态应用 复用原来的view的,并没有适配新的内容。
我们在setText() 方法中重置 isFirst ,重新进行判断 图片tree变化的,进行显示和隐藏。
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bm.feipingflyclip.R;
import com.volokh.danylo.video_player_manager.utils.Logger;
public class MoreTextView extends LinearLayout {
protected TextView contentView;//内容显示区
protected TextView expandTextView;//收起 更多显示区
protected int textColor;//显示区文字颜色
protected int textSize = 12;//显示区文字大小
protected String text;//要显示的文字
private int maxLine =4;// 默认展示最大行数4行
private static final int SHOW_CONTENT_NONE_STATE = 0;// 扩充
private static final int SHRINK_UP_STATE = 1;// 收起状态
private static final int SPREAD_STATE = 2;// 展开状态
private int mState = SHRINK_UP_STATE;// 默认收起状态
public int defaultTextColor = Color.BLACK;
private boolean isFirst=true;
public MoreTextView(Context context, AttributeSet attrs) {
super(context, attrs);
//动态创建布局
initalize();
initWithAttrs(context, attrs);//从xml中获取初始数据
init();
//视图状态改变监听
ViewTreeObserver vto = contentView.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
if (isFirst) {
Logger.e(contentView.getLineCount()+"onPreDraw---");
if (maxLine >= contentView.getLineCount()) {
contentView.setText(contentView.getText());
mState = SHOW_CONTENT_NONE_STATE;
expandTextView.setVisibility(View.GONE);
} else {
contentView.setMaxLines(maxLine);
contentView.requestLayout();
expandTextView.setText("展开");
mState = SHRINK_UP_STATE;
expandTextView.setVisibility(View.VISIBLE);
}
isFirst=false;
}
return true;
}
});
}
private void init() {
/**
* requestLayout:当view确定自身已经不再适合现有的区域时,该view本身调用这个方法要求parent view重新调用他的onMeasure onLayout来对重新设置自己位置。
*/
contentView.requestLayout();
expandTextView.setText("展开");
mState = SHRINK_UP_STATE;
}
protected void initWithAttrs(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MoreTextStyle);
int textColor = a.getColor(R.styleable.MoreTextStyle_textColor1, defaultTextColor);
textSize = a.getDimensionPixelSize(R.styleable.MoreTextStyle_textSize1, textSize);
maxLine = a.getInt(R.styleable.MoreTextStyle_maxLine, maxLine);
text = a.getString(R.styleable.MoreTextStyle_text1);
a.recycle();
if (contentView != null)
contentView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
}
/**
* 初始化控件 判断初始状态等
*/
protected void initalize() {
setOrientation(VERTICAL);
setGravity(Gravity.LEFT);
contentView = new TextView(getContext());
// contentView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
LayoutParams llpc = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
llpc.setMargins(0, 0, 0, 0);
addView(contentView, llpc);
expandTextView = new TextView(getContext());
int padding = dip2px(getContext(), 5);
LayoutParams llp = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
expandTextView.setVisibility(View.GONE);
expandTextView.setTextColor(Color.parseColor("#4a799a"));
llp.setMargins(0, dip2px(getContext(), 2), padding, 0);
addView(expandTextView, llp);
expandTextView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
switch (mState) {
case SPREAD_STATE:
contentView.setMaxLines(maxLine);
contentView.requestLayout();
expandTextView.setText("展开");
mState = SHRINK_UP_STATE;
break;
case SHRINK_UP_STATE:
contentView.setMaxLines(Integer.MAX_VALUE);
contentView.requestLayout();
expandTextView.setText("收起");
mState = SPREAD_STATE;
break;
default:
break;
}
if (onStateChange != null) {
onStateChange.stateChangeLitener(mState);
}
}
});
}
/**
* 在list列表中需要记住当前控件的状态
* @param state
*/
public void setState(int state) {
mState = state;
if (state==SHOW_CONTENT_NONE_STATE) {
if (maxLine >= contentView.getLineCount()) {
contentView.setText(contentView.getText());
mState = SHOW_CONTENT_NONE_STATE;
expandTextView.setVisibility(View.GONE);
} else {
contentView.setMaxLines(maxLine);
contentView.requestLayout();
expandTextView.setText("展开");
mState = SHRINK_UP_STATE;
expandTextView.setVisibility(View.VISIBLE);
}
}
switch (mState) {
case SHOW_CONTENT_NONE_STATE:
expandTextView.setVisibility(View.GONE);
contentView.setText(contentView.getText());
break;
case SHRINK_UP_STATE://收起状态
contentView.setMaxLines(maxLine);
expandTextView.setVisibility(View.VISIBLE);
contentView.requestLayout();
expandTextView.setText("展开");
break;
case SPREAD_STATE://展开状态
contentView.setMaxLines(Integer.MAX_VALUE);
contentView.requestLayout();
expandTextView.setText("收起");
expandTextView.setVisibility(View.VISIBLE);
break;
default:
break;
}
}
// public void setState(int state){
// mState=state;
//
// }
public int getState(){
return mState;
}
public void setExpandTextViewColor(int textColor) {
expandTextView.setTextColor(textColor);
}
public TextView getTextView() {
return contentView;
}
public void setText(CharSequence charSequence) {
contentView.setText(charSequence);
isFirst = true;
}
public static int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
private OnStateChange onStateChange;
public void setOnStateChange(OnStateChange onStateChange) {
this.onStateChange = onStateChange;
}
public String getLineCount() {
return contentView.getLineCount()+"";
}
public interface OnStateChange{
public void stateChangeLitener(int state);
}
}
atts 定义 属性
<declare-styleable name="MoreTextStyle"> <attr name="textSize1" format="dimension" /> <attr name="textColor1" format="color" /> <attr name="maxLine" format="integer" /> <attr name="text1" format="string" /> </declare-styleable>
<com.bm.xxoo.activity.search.view.MoreTextView android:id="@+id/tv_forward" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/tv_forward_from" more:maxLine="2" android:visibility="gone" more:textColor1="#444444" more:textSize1="16.5sp" />xml 样式
//视图状态改变监听
ViewTreeObserver vto = contentView.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
if (isFirst) {
Logger.e(contentView.getLineCount()+"onPreDraw---");
if (maxLine >= contentView.getLineCount()) {
contentView.setText(contentView.getText());
mState = SHOW_CONTENT_NONE_STATE;
expandTextView.setVisibility(View.GONE);
} else {
contentView.setMaxLines(maxLine);
contentView.requestLayout();
expandTextView.setText("展开");
mState = SHRINK_UP_STATE;
expandTextView.setVisibility(View.VISIBLE);
}
isFirst=false;
}
return true;
}
});