搜索+流式布局+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
    评论
RecyclerView本身并不支持流式布局,但是可以通过自定义LayoutManager来实现流式布局。下面是一个简单的实现步骤: 1. 创建一个继承自RecyclerView.LayoutManager的自定义LayoutManager。 2. 在自定义LayoutManager中重写onLayoutChildren方法,实现流式布局的摆放。 3. 在onMeasure方法中计算RecyclerView的宽高。 4. 在RecyclerView.Adapter中根据需要调整item的宽高,以适配流式布局。 以下是一个简单的示例代码: ```java public class FlowLayoutManager extends RecyclerView.LayoutManager { private int mTotalHeight; @Override public RecyclerView.LayoutParams generateDefaultLayoutParams() { return new RecyclerView.LayoutParams( RecyclerView.LayoutParams.WRAP_CONTENT, RecyclerView.LayoutParams.WRAP_CONTENT); } @Override public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) { if (state.getItemCount() == 0) { return; } detachAndScrapAttachedViews(recycler); int offsetX = getPaddingLeft(); int offsetY = getPaddingTop(); int lineMaxHeight = 0; int width = getWidth(); for (int i = 0; i < getItemCount(); i++) { View child = recycler.getViewForPosition(i); addView(child); measureChildWithMargins(child, 0, 0); int childWidth = getDecoratedMeasuredWidth(child); int childHeight = getDecoratedMeasuredHeight(child); if (offsetX + childWidth > width - getPaddingRight()) { offsetX = getPaddingLeft(); offsetY += lineMaxHeight; lineMaxHeight = 0; } layoutDecorated(child, offsetX, offsetY, offsetX + childWidth, offsetY + childHeight); offsetX += childWidth; lineMaxHeight = Math.max(lineMaxHeight, childHeight); } mTotalHeight = offsetY + lineMaxHeight + getPaddingBottom(); } @Override public boolean canScrollVertically() { return true; } @Override public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) { if (getChildCount() == 0 || dy == 0) { return 0; } int scrolled = 0; if (dy > 0) { View lastChild = getChildAt(getChildCount() - 1); int lastVisiblePos = getPosition(lastChild); if (lastVisiblePos == getItemCount() - 1 && lastChild.getBottom() - dy < getHeight() - getPaddingBottom()) { dy = lastChild.getBottom() - getHeight() + getPaddingBottom(); } } else { View firstChild = getChildAt(0); int firstVisiblePos = getPosition(firstChild); if (firstVisiblePos == 0 && firstChild.getTop() - dy > getPaddingTop()) { dy = firstChild.getTop() - getPaddingTop(); } } offsetChildrenVertical(-dy); scrolled += dy; fill(recycler); return scrolled; } private void fill(RecyclerView.Recycler recycler) { int topOffset = getPaddingTop(); int leftOffset = getPaddingLeft(); int lineMaxHeight = 0; int width = getWidth(); for (int i = 0; i < getChildCount(); i++) { View child = getChildAt(i); int childWidth = getDecoratedMeasuredWidth(child); int childHeight = getDecoratedMeasuredHeight(child); if (leftOffset + childWidth > width - getPaddingRight()) { leftOffset = getPaddingLeft(); topOffset += lineMaxHeight; lineMaxHeight = 0; } layoutDecorated(child, leftOffset, topOffset, leftOffset + childWidth, topOffset + childHeight); leftOffset += childWidth; lineMaxHeight = Math.max(lineMaxHeight, childHeight); } detachAndScrapAttachedViews(recycler); } @Override public int computeVerticalScrollOffset(RecyclerView.State state) { return computeVerticalScrollExtent(state); } @Override public int computeVerticalScrollExtent(RecyclerView.State state) { return getHeight() - getPaddingTop() - getPaddingBottom(); } @Override public int computeVerticalScrollRange(RecyclerView.State state) { return mTotalHeight; } } ``` 使用时只需要将RecyclerView的LayoutManager设置为自定义的FlowLayoutManager即可实现流式布局

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值