Cfrg.java
public class Cfrag extends Fragment{
private TitleBarView mTitleBarView;
List<String> ff=new ArrayList<>();
private CustomWaterFallViewGroup mFallViewGroup;
private Button mButton;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.cfrag, null);
mTitleBarView = view.findViewById(R.id.cf_title);
mFallViewGroup = view.findViewById(R.id.cf_water_fall);
mButton = view.findViewById(R.id.cf_btn);
mTitleBarView.setOnButtonClickListener(new TitleBarView.OnButtonClickListener() {
@Override
public void onClickListener(String str) {
ff.add(str);
mFallViewGroup.setData(ff);
}
});
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mFallViewGroup.clearAll();
}
});
return view;
}
}
cfrag.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fanruiqi.bwie.com.shopcartlx.view.TitleBarView
android:id="@+id/cf_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<fanruiqi.bwie.com.shopcartlx.view.CustomWaterFallViewGroup
android:id="@+id/cf_water_fall"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/cf_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清空历史"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
TitleBarView.java
public class TitleBarView extends LinearLayout{
private Context mContext;
private EditText mEditText;
private ImageView mImageView;
public TitleBarView(Context context) {
super(context);
this.mContext=context;
initData();
}
public TitleBarView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.mContext=context;
initData();
}
private void initData() {
View view = View.inflate(mContext, R.layout.title, null);
mEditText = view.findViewById(R.id.title_edit);
mImageView = view.findViewById(R.id.title_img);
mImageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
String str = mEditText.getText().toString();
if (mOnButtonClickListener!=null){
mOnButtonClickListener.onClickListener(str);
}
}
});
addView(view);
}
public interface OnButtonClickListener{
void onClickListener(String str);
}
OnButtonClickListener mOnButtonClickListener;
public void setOnButtonClickListener(OnButtonClickListener onButtonClickListener){
mOnButtonClickListener=onButtonClickListener;
}
}
title.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<ImageView
android:id="@+id/title_img"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@mipmap/ic_launcher_round"/>
<EditText
android:id="@+id/title_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
CustomWaterFallViewGroup.java
public class CustomWaterFallViewGroup extends LinearLayout{
private Context mContext;
List<String> StringList=new ArrayList<>();
int MAX=10;
public CustomWaterFallViewGroup(Context context) {
super(context);
this.mContext=context;
initData();
}
public CustomWaterFallViewGroup(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.mContext=context;
initData();
}
public void setData(List<String> data) {
this.StringList = data;
showData();
}
private void initData() {
setOrientation(VERTICAL);
}
public void showData(){
removeAllViews();
LinearLayout linearLayout = (LinearLayout) View.inflate(mContext, R.layout.item_water_fall_h, null);
addView(linearLayout);//首先先存放一个布局
int len=0;
for (int i=0;i<StringList.size();i++){
final String strs = StringList.get(i);
len+=strs.length();
if (len>MAX){ //如果第一条存放已满,接着添加往下添加布局
linearLayout = (LinearLayout) View.inflate(mContext,R.layout.item_water_fall_h,null);
addView(linearLayout);
len=strs.length();
}
View view = View.inflate(mContext, R.layout.item_water_fall, null);
TextView textView = view.findViewById(R.id.tv_item_water_fall);
textView.setText(strs);
linearLayout.addView(view);
LinearLayout.LayoutParams layoutParams = (LayoutParams) view.getLayoutParams(); //设置文本布局间隔
layoutParams.weight=1;
view.setLayoutParams(layoutParams);
final int index=i;
view.setOnClickListener(new OnClickListener() { //点击吐司
@Override
public void onClick(View view) {
Toast.makeText(mContext,StringList.get(index),Toast.LENGTH_SHORT).show();
}
});
view.setOnLongClickListener(new OnLongClickListener() { //长按历史记录删除
@Override
public boolean onLongClick(View view) {
StringList.remove(index); //长按删除单个记录
showData();
return true;
}
});
}
}
public void clearAll() { //点击按钮清除全部历史记录
StringList.clear();
showData();
}
}