mvp(retrofit+rxjava实现)

1.首先创建一个MyApi(用于存放接口,调用接口)

public interface MyApi {

    @GET(HttpConfig.DISH)
    Observable<DishBean> showDish(@Query("menu")String menu);

}

2.model层

public class DishModel {

    private static final String TAG = "DishModel******";

    public Observable<DishBean> getDish(String menu){

        return RetrofitManager.getDafault().create(MyApi.class).showDish(menu);
    }

}

3.presenter

public class DishPresenter extends BasePresenter<DishView> {

    private static final String TAG = "DishPresenter******";
    private DishModel dishModel;

    public DishPresenter(DishView view) {
        super(view);
    }

    @Override
    protected void initmodel() {
        dishModel = new DishModel();
    }


    public void Dish(String menu) {
        dishModel.getDish(menu)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<DishBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }
                    @Override
                    public void onNext(DishBean dishBean) {
                        if (view != null) {
                            view.onSuccess(dishBean);
                            Log.e(TAG, "onNext:// "+dishBean.getResult().getData().size() );
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        if (view != null) {
                            view.onFaild(e.toString());
                        }
                    }
                    @Override
                    public void onComplete() {
                    }
                });

    }
}

4.view层

public interface DishView extends IView {
     void onSuccess(DishBean dishBean);
     void onFaild(String error);
}

5.主页面(流式布局+清空浏览记录)

public class MainActivity extends AppCompatActivity {

    private EditText editText;
    private Button btn_searvh;
    private FlowLayout flowLayout;
    private Button btn_remove;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
    }

    private void initViews() {
        editText = findViewById(R.id.main_ed);
        btn_searvh = findViewById(R.id.btn_search);
        flowLayout = findViewById(R.id.main_flow);
        btn_remove = findViewById(R.id.btn_remove);
        btn_searvh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = editText.getText().toString();
                TextView textView = new TextView(MainActivity.this);
                textView.setText(name);
                flowLayout.addView(textView);
                Intent it = new Intent(MainActivity.this, DishActivity.class);
                    it.putExtra("name",name);
                    startActivityForResult(it,100);
            }
        });
        btn_remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flowLayout.removeAllViews();
            }
        });
    }
}

6.流失布局

public class FlowLayout extends ViewGroup {

    public FlowLayout(Context context) {
        super(context);
    }

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

    public FlowLayout(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 widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        //自定义属性
        int width = 0;
        int height = 0;
        int childWidth = 0;
        int childHeight = 0;
        int linWidth = 0;
        int linHeight = 0;
        int totalHeight = 0;
        View viewChild;

        //遍历
        for (int i = 0; i < getChildCount(); i++) {
            viewChild = getChildAt(i);
            //获取子布局宽高
            childWidth = viewChild.getMeasuredWidth();
            childHeight = viewChild.getMeasuredHeight();
            if (childWidth > widthSize) {
                throw new IllegalArgumentException("长度过大");
            }
            if (linWidth > widthSize) {
                //换行
                width = widthSize;
                totalHeight += linHeight;
                linWidth = childWidth;
                linHeight = childHeight;
            } else {
                //否则不换行
                linWidth += childWidth;
                linHeight = Math.max(linHeight, childHeight);
                width = Math.max(linWidth, width);
            }
            if (i == getChildCount() - 1) {
                totalHeight = totalHeight + linHeight;
                height = totalHeight;
            }

        }
        width = widthMode == MeasureSpec.EXACTLY ? widthSize : width;
        height = heightMode == MeasureSpec.EXACTLY ? heightSize : height;

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int width = 0;
        int height = 0;
        int childWidth = 0;
        int childHeight = 0;
        int linWidth = 0;
        int linHeight = 0;
        int totalHeight = 0;
        View viewChild;

        for (int i = 0; i < getChildCount(); i++) {
            viewChild = getChildAt(i);
            childWidth = viewChild.getMeasuredWidth();
            childHeight = viewChild.getMeasuredHeight();
            if (linWidth + childWidth > getMeasuredWidth()) {
                //换行
                linWidth = 0;
                totalHeight += linHeight;
                layoutView(viewChild, linWidth, totalHeight, linWidth + childWidth, totalHeight + childHeight);
                linHeight = childHeight;
                linWidth = childWidth;
            } else {
                //不换行
                layoutView(viewChild, linWidth, totalHeight, linWidth + childWidth, totalHeight + childHeight);
                linWidth += childWidth;
                linHeight = Math.max(linHeight, childHeight);
            }
        }
    }

    private void layoutView(View viewChild, int linWidth, int totalHeight, int i, int i1) {

        linWidth += getPaddingLeft();
        linWidth += getPaddingTop();
        viewChild.layout(linWidth, totalHeight, i, i1);
    }

}

7.主页面布局

<?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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


    <EditText
        android:id="@+id/main_ed"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:hint="请输入搜索内容"
        />

    <Button
        android:id="@+id/btn_search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="搜索"
        />
    </LinearLayout>

    <com.bw.day0714_demo_tomato.FlowLayout
        android:id="@+id/main_flow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

    </com.bw.day0714_demo_tomato.FlowLayout>

<Button
    android:id="@+id/btn_remove"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="清空浏览记录"
    />
</LinearLayout>

8.点击搜索按钮进入详情页面

 
<?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=".mvp.view.activity.DishActivity">


<android.support.v7.widget.RecyclerView
    android:id="@+id/dish_recyler"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>

</LinearLayout>



 public class DishActivity extends BaseActivity<DishPresenter> implements DishView {

    String menu;
    private RecyclerView recyclerView;
    private static final String TAG = "DishActivity*****";

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initViews() {
        recyclerView = findViewById(R.id.dish_recyler);
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        presenter.Dish(name);
    }

    @Override
    protected DishPresenter provide() {
        return new DishPresenter((DishView) this);
    }

    @Override
    protected int provId() {
        return R.layout.activity_dish;
    }

    @Override
    public void onSuccess(DishBean dishBean) {
        String resultcode = dishBean.getResultcode();
        if (resultcode.equals("200")) {
            Log.e(TAG, "长度:-------------- " + dishBean.getResult().getData().get(0).getTitle());
            final List<DishBean.ResultBean.DataBean> data = dishBean.getResult().getData();
            Log.e(TAG, "onSuccess: " + data.size());
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(DishActivity.this);
            recyclerView.setLayoutManager(linearLayoutManager);
            MyAdapter adapter = new MyAdapter(data);
            recyclerView.setAdapter(adapter);
            recyclerView.addItemDecoration(new DividerItemDecoration(DishActivity.this, DividerItemDecoration.VERTICAL));

            //设置点击事件(点击条目进入轮播图)
            adapter.setOnClickListener(new MyAdapter.onClickListener() {
                @Override
                public void onClick(View v, int position) {
                    Intent intent = new Intent(DishActivity.this,ShowActivity.class);
                    String title = data.get(position).getTitle();
                    List<DishBean.ResultBean.DataBean.StepsBean> steps = data.get(position).getSteps();
                    ArrayList<String> list1 = new ArrayList<>();
                    ArrayList<String> list2 = new ArrayList<>();
                    for (int i = 0; i < steps.size(); i++) {
                        String img = steps.get(i).getImg();
                        list1.add(img);
                        String step = steps.get(i).getStep();
                        list2.add(step);
                    }
                    intent.putExtra("title", title);
                    intent.putStringArrayListExtra("list1_step", list1);
                    intent.putStringArrayListExtra("list2_step", list2);
                    startActivity(intent);
                }
            });
        } else {
            Toast.makeText(DishActivity.this, "查询失败", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    @Override
    public void onFaild(String error) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}

9.recylerview适配器

public class MyAdapter extends RecyclerView.Adapter {

    private static final String TAG = "MyAdapter****";
    private List<DishBean.ResultBean.DataBean> list;

    public MyAdapter(List<DishBean.ResultBean.DataBean> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item1, null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {


        ((MyHolder) holder).tv_title.setText(list.get(position).getTitle());
        Log.e(TAG, "onBindViewHolder: " + list.get(position).getTitle());
        ((MyHolder) holder).tv_content.setText(list.get(position).getTags());
        Log.e(TAG, "onBindViewHolder: " + list.get(position).getTags());
        String[] pic = list.get(position).getAlbums().get(0).split("\\|");
        Uri uri = Uri.parse(pic[0]);
        ((MyHolder) holder).img.setImageURI(uri);
        ((MyHolder) holder).img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListener != null) {
                    onClickListener.onClick(v, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        Log.e(TAG, "getItemCount: " + list.size());
        return list == null ? 0 : list.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {

        private final SimpleDraweeView img;
        private final TextView tv_title;
        private final TextView tv_content;

        public MyHolder(View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.item1_img);
            tv_title = itemView.findViewById(R.id.item1_title);
            tv_content = itemView.findViewById(R.id.item1_content);
        }
    }

    onClickListener onClickListener;

    public void setOnClickListener(MyAdapter.onClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public interface onClickListener {
        void onClick(View v, int position);
    }

}

11.点击条目到打轮播图页面

<?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=".mvp.view.activity.ShowActivity">

    <com.stx.xhb.xbanner.XBanner
        android:id="@+id/show_xbanner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </com.stx.xhb.xbanner.XBanner>

<TextView
    android:id="@+id/show_tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="文字"
    />
</LinearLayout>
//轮播图页面
 public class ShowActivity extends AppCompatActivity {

    private XBanner xBanner;
    private TextView textView;
    List<String> imgs = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        initViews();
    }

    private void initViews() {
        xBanner = findViewById(R.id.show_xbanner);
        textView = findViewById(R.id.show_tv);
        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        textView.setText(title);
        ArrayList<String> list1_step = intent.getStringArrayListExtra("list1_step");
        ArrayList<String> list2_step = intent.getStringArrayListExtra("list2_step");
        for (int i = 0; i < list1_step.size(); i++) {
            imgs.addAll(list1_step);
        }
        xBanner.setData(list1_step,list2_step);
        xBanner.setmAdapter(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, Object model, View view, int position) {
                Glide.with(ShowActivity.this).load(imgs.get(position)).into((ImageView) view);
            }
        });
        xBanner.setPageTransformer(Transformer.Default);
        xBanner.setPageChangeDuration(1000);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值