自定义view的流失布局的使用

package com.example.dayzhao9_7;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;

public class MyView extends ViewGroup {
    private int mleftMargin = 20;
    private int mtopMargin = 20;

    public MyView(Context context) {
        this(context,null);
    }

    public MyView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyView(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);
        int leftMargin = mleftMargin;
        int topMargin = mtopMargin;

        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);

        switch (modeHeight){
            case MeasureSpec.AT_MOST:
                int measuredHeight = 0;
                for (int j=0;j<getChildCount();j++){
                    int measuredWidth = getChildAt(j).getMeasuredWidth();
                    measuredHeight = getChildAt(j).getMeasuredHeight();
                    if (leftMargin+measuredWidth+mleftMargin>getWidth()){
                        leftMargin = mleftMargin;
                        topMargin+=measuredHeight+mtopMargin;
                    }
                    leftMargin+=measuredWidth+mleftMargin;
                }
                topMargin+=measuredHeight+mtopMargin;
                break;
        }
        setMeasuredDimension(sizeWidth,topMargin);
    }

    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        int leftMargin = mleftMargin;
        int topMargin = mtopMargin;

        for (int j=0;j<getChildCount();j++){
            int measuredWidth = getChildAt(j).getMeasuredWidth();
            int measuredHeight = getChildAt(j).getMeasuredHeight();

            if (leftMargin+measuredWidth+mleftMargin>getWidth()){
                leftMargin=mleftMargin;
                topMargin+=measuredHeight+mtopMargin;
                getChildAt(j).layout(leftMargin,topMargin,leftMargin+measuredWidth,topMargin+measuredHeight);
            }else {
                getChildAt(j).layout(leftMargin,topMargin,leftMargin+measuredWidth,topMargin+measuredHeight);
            }
            leftMargin+=measuredWidth+mleftMargin;
        }
    }

}

 

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#c0c0c0"></solid>
    <stroke android:width="1dip" android:color="#c0c0c0"></stroke>
    <padding android:bottom="5dp" android:left="12dp" android:top="5dp" android:right="12dp"></padding>
    <corners android:radius="80dp"></corners>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

     <LinearLayout
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
         >
         <FrameLayout
             android:layout_width="0dp"
             android:layout_weight="1"
             android:layout_height="wrap_content">
             <EditText
                 android:id="@+id/edt"
                 android:textColorHighlight="#ff44ff"
                 android:layout_width="match_parent"
                 android:hint="请输入搜索的内容"
                 android:layout_height="wrap_content" />
         </FrameLayout>
         <Button
             android:layout_gravity="right"
             android:id="@+id/but"
             android:background="#808080"
             android:textSize="20dp"
             android:text="搜索"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content" />
            <Button
                android:id="@+id/login"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="登陆"
                android:textSize="20sp"
                />

     </LinearLayout>
    <com.example.dayzhao9_7.MyView
        android:id="@+id/myview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </com.example.dayzhao9_7.MyView>
        <com.jcodecraeer.xrecyclerview.XRecyclerView
            android:id="@+id/xre"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        </com.jcodecraeer.xrecyclerview.XRecyclerView>

</LinearLayout>

 

package com.example.dayzhao9_7;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.dayzhao9_7.adapter.MyAdapter;
import com.example.dayzhao9_7.bean.Shop;
import com.example.dayzhao9_7.persenter.IRecyPersenter;
import com.example.dayzhao9_7.view.IRecyView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements IRecyView {
    private EditText edt;
    private Button but;
    private MyView myView;
    private TextView study;
    private Context context;
    private List<String> list2 = new ArrayList<>();
    private LayoutInflater layoutInflater;

     private XRecyclerView xRecyclerView;
     private MyAdapter adapter;
     private IRecyPersenter persenter;
     private int page=1;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        initData();
        initShop();
        persenter=new IRecyPersenter(this);
        persenter.Datashop();
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page=1;
                persenter.Datashop();

            }

            @Override
            public void onLoadMore() {
              page++;
              persenter.Datashop();
            }
        });
    }

    private void initShop() {
        xRecyclerView=findViewById(R.id.xre);
        LinearLayoutManager manager=new LinearLayoutManager(this);
        xRecyclerView.setLayoutManager(manager);
        xRecyclerView.setLoadingMoreEnabled(true);

    }

    private void initData() {
        edt = findViewById(R.id.edt);
       myView=findViewById(R.id.myview);
        but =  findViewById(R.id.but);
        layoutInflater= LayoutInflater.from(this);
        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = edt.getText().toString();
                View view=LayoutInflater.from(context).inflate(R.layout.study,null);
                study=view.findViewById(R.id.study);
                list2.add(s);
                for (int i=0;i<list2.size();i++){
                    study.setText(list2.get(i));
                    study.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(MainActivity.this, ShowActivity.class);
                            startActivity(intent);
                        }
                    });
                }
                myView.addView(view);

            }
        });
    }

    @Override
    public void shopData(Shop shop) {
        if (page==1){
            adapter=new MyAdapter(this,shop);
            xRecyclerView.setAdapter(adapter);
            xRecyclerView.refreshComplete();
        }else {
            if (adapter!=null){
                 adapter.addData(shop.getData());
                 xRecyclerView.loadMoreComplete();
            }
        }
    }
}

 

package com.example.dayzhao9_7.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.dayzhao9_7.R;
import com.example.dayzhao9_7.bean.Shop;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        private Context context;
        private Shop shop;
        private List<Shop.DataBean> list;

    public MyAdapter(Context context, Shop shop) {
        this.context = context;
        this.shop = shop;
        list=shop.getData();
    }

      public void addData(List<Shop.DataBean> list){
            this.list.addAll(list);
            notifyDataSetChanged();
      }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view=LayoutInflater.from(context).inflate(R.layout.myadapter,parent,false);
        MyViewHolder holder=new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
          MyViewHolder holder1= (MyViewHolder) holder;
        String[] imgs = shop.getData().get(position).getImages().split("\\|");
        Glide.with(context).load(imgs[0]).into(holder1.imageView);
        holder1.title.setText(shop.getData().get(position).getTitle());
        holder1.price.setText(shop.getData().get(position).getPrice()+"元");
          holder1.subhead.setText(shop.getData().get(position).getSubhead());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    public static class MyViewHolder extends RecyclerView.ViewHolder{
          private TextView title;
          private TextView subhead;
          private TextView price;
          private ImageView imageView;

        public MyViewHolder(View itemView) {
            super(itemView);
            title=itemView.findViewById(R.id.title);
            subhead=itemView.findViewById(R.id.subhead);
            price=itemView.findViewById(R.id.price);
            imageView=itemView.findViewById(R.id.imagee);
        }
    }
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值