流式布局,继承ViewGroup
public class LiuView extends ViewGroup {
public LiuView(Context context) {
super(context);
}
public LiuView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LiuView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureChildren(widthMeasureSpec,heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int x=0;
int y=0;
int row=1;
int disWidth=r-1;
int count = getChildCount();
for (int i = 0; i <count ; i++) {
View childAt = getChildAt(i);
int height = childAt.getMeasuredHeight();
int width = childAt.getMeasuredWidth();
x+=width;
if (x>disWidth){
x=width;
row++;
}
y=row*height;
childAt.layout(x-width,y-height,x,y);
}
}
}
//搜索框,继承linearLayout
public class SouView extends LinearLayout {
public SouView(Context context) {
this(context,null);
}
public SouView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public SouView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
LayoutInflater.from(context).inflate(R.layout.sou_item,this,true);
}
}
进入显示页面,进行展示
public class MainActivity extends AppCompatActivity implements Contract.Iview {
private String url=“加入路径”;
private Presenter presenter;
private String weiyi=“卫衣”;
private RecyclerView recy_view;
private SouView sou_view;
private EditText edit_sou;
private TextView text_sou;
private LiuView liu_view;
private List<RecyBean.ResultBean> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
initView();
presenter = new Presenter();
presenter.attch(this);
presenter.show(url+"?keyword="+ URLEncoder.encode(weiyi)+"&page="+1+"&count="+10);
//搜索框,点击
text_sou.setOnClickListener(new View.OnClickListener() {
private TextView textView;
@Override
public void onClick(View v) {
String trim = edit_sou.getText().toString().trim();
if (trim.equals("")){
Toast.makeText(MainActivity.this,"不能为空",Toast.LENGTH_LONG).show();
}else{
presenter.show(url+"?keyword="+ URLEncoder.encode(trim)+"&page="+1+"&count="+10);
}
textView = new TextView(MainActivity.this);
textView.setText(trim);
textView.setTextSize(20);
textView.setTextColor(Color.BLUE);
textView.setPadding(20,20,20,20);
ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
liu_view.addView(textView,params);
}
});
}
private void initView() {
recy_view = findViewById(R.id.recy_view);
sou_view = findViewById(R.id.sou_view);
edit_sou = sou_view.findViewById(R.id.edit_sou);
text_sou = sou_view.findViewById(R.id.text_sou);
liu_view = findViewById(R.id.liu_view);
//布局管理器
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recy_view.setLayoutManager(linearLayoutManager);
}
//列表展示
@Override
public void getRequest(String data) {
Gson gson = new Gson();
RecyBean recyBean = gson.fromJson(data, RecyBean.class);
list = recyBean.getResult();
RecyAdapter recyAdapter = new RecyAdapter(this, list);
recy_view.setAdapter(recyAdapter);
recyAdapter.setOnclick(new RecyAdapter.onClick() {
@Override
public void onclick(int i) {
Intent intent = new Intent();
String pic = list.get(i).getMasterPic();
intent.putExtra("pic",pic);
intent.setClass(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
presenter.detch();
}
}