搜索+流式布局+RecyclerView

流式布局,继承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();
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值