流式布局,排序,切换

1.//mainactivity中

package com.example.w.dsmoni1;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.library.AutoFlowLayout;
import com.example.library.FlowAdapter;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView image_search;
    private EditText edit_name;
    private TextView text_qx;
    private ArrayList<String> list;
    private ImageView image_delete;
    private AutoFlowLayout auto_flow;
    private TextView text_aearch_name;
    private View view;

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

    private void init() {
        list = new ArrayList<>();
        image_search = findViewById(R.id.image_search);
        edit_name = findViewById(R.id.edit_name);
        text_qx = findViewById(R.id.text_qx);
        image_delete = findViewById(R.id.image_delete);
        auto_flow = findViewById(R.id.auto_flow);
        text_qx.setOnClickListener(this);
        image_delete.setOnClickListener(this);
        image_search.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.text_qx:
                edit_name.getText().clear();
                break;
            case R.id.image_delete:
                edit_name.getText().clear();
                list.clear();
                auto_flow.removeAllViews();
                break;
            case R.id.image_search:
                String string = edit_name.getText().toString();
                list.add(string);
                auto();
                break;
        }
    }

    private void auto() {
        auto_flow.setAdapter(new FlowAdapter(list) {
            @Override
            public View getView(int i) {
                 view = View.inflate(MainActivity.this, R.layout.main_item, null);
                text_aearch_name = view.findViewById(R.id.text_search_name);
                final String string = list.get(i);
                text_aearch_name.setText(string);
                list.clear();
                text_aearch_name.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(MainActivity.this, ShoppingActivity.class);
                        intent.putExtra("name",string);
                        startActivity(intent);

                    }
                });
                return view;
            }
        });
    }
}

 

2.//shoppactivity中

package com.example.w.dsmoni1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.example.w.dsmoni1.presener.ShopPresenerImpl;
import com.example.w.dsmoni1.view.ShopView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;

import java.util.List;

import adapter.MyAdapter;
import bean.ShopBean;

public class ShoppingActivity extends Activity implements View.OnClickListener,ShopView {

    private ImageView image_shop_back;
    private ImageView image_shop_search;
    private ImageView image_shop_remove;
    private ImageView image_shop_replace;
    private PullToRefreshScrollView pull_refufh_view;
    private RecyclerView recycle_view;
    private Button btn_price;
    private Button btn_xiaoliang;
    private Button btn_zonghe;
    private EditText edit_shop_search;
    private boolean b=false;
    private ShopPresenerImpl presener;
    private int page=1;
    private int sort=0;
    private List<ShopBean.DataBean> data;
    private String tvName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping);
        Intent intent = getIntent();
        //判断
        if (intent != null) {
            tvName = intent.getStringExtra ("name");
        }
//       edit_shop_search.setText(tvName);
        //控件
        init();
        initData();
    }

    private void initData() {
        pull_refufh_view.setMode(PullToRefreshBase.Mode.BOTH);
        recycle_view.setLayoutManager(new LinearLayoutManager(ShoppingActivity.this,LinearLayoutManager.VERTICAL,false));
        presener = new ShopPresenerImpl(this);
        pull_refufh_view.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> pullToRefreshBase) {
                page=1;
                presener.getdata(page,sort);
                pull_refufh_view.onRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> pullToRefreshBase) {
                page++;
                presener.getdata(page,sort);
                pull_refufh_view.onRefreshComplete();
            }
        });
    }

    private void init() {
        image_shop_back = findViewById(R.id.image_shop_back);
        image_shop_search = findViewById(R.id.image_shop_search);
        image_shop_remove = findViewById(R.id.image_shop_remove);
        edit_shop_search = findViewById(R.id.edit_shop_search);
        image_shop_replace = findViewById(R.id.image_shop_replace);
        pull_refufh_view = findViewById(R.id.pull_refhsh_view);
        recycle_view = findViewById(R.id.recycle_view);
        btn_price = findViewById(R.id.btn_price);
        btn_xiaoliang = findViewById(R.id.btn_xiaoliang);
        btn_zonghe = findViewById(R.id.btn_zonghe);
        image_shop_back.setOnClickListener(this);
        image_shop_remove.setOnClickListener(this);
        image_shop_replace.setOnClickListener(this);
        image_shop_search.setOnClickListener(this);
        btn_price.setOnClickListener(this);
        btn_xiaoliang.setOnClickListener(this);
        btn_zonghe.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image_shop_back:
                finish();
                break;
            case R.id.image_shop_remove:
               edit_shop_search.getText().clear();
                break;
            case R.id.image_shop_replace:
                if (b == false) {
                    recycle_view.setLayoutManager(new GridLayoutManager(ShoppingActivity.this,2));
                    b=true;
                    image_shop_replace.setImageResource(R.mipmap.kind_grid);
                } else {
                    recycle_view.setLayoutManager(new LinearLayoutManager(ShoppingActivity.this,LinearLayoutManager.VERTICAL,false));
                    b=false;
                    image_shop_replace.setImageResource(R.mipmap.kind_liner);
                }
                break;
            case R.id.image_shop_search:
                break;
            case R.id.btn_price:
                sort=2;
                presener.getdata(page,sort);
                break;
            case R.id.btn_xiaoliang:
                sort=1;
                presener.getdata(page,sort);
                break;
            case R.id.btn_zonghe:
                sort=0;
                presener.getdata(page,sort);
                break;
        }
    }



    @Override
    public void SuccessCallback(ShopBean vbean) {
        data = vbean.getData();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                MyAdapter myAdapter = new MyAdapter(ShoppingActivity.this, data);
                recycle_view.setAdapter(myAdapter);
                pull_refufh_view.onRefreshComplete();
            }
        });
    }

    @Override
    public void FailerCallback(int code) {

    }
}

 

3//mainmodelImpl中

package com.example.w.dsmoni1.model;

import com.google.gson.Gson;

import java.io.IOException;

import bean.ShopBean;
import http.Api;
import http.OkHttpUtils;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ShopModelImpl {
    public void getdata(final ShopModel shopModelListtener, final int page, final int sort){
         //拼接
        String api="http://www.zhaoapi.cn/product/searchProducts?keywords=手机&page="+page+"&sort="+sort+"";
        OkHttpUtils.doget(api, new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {
                shopModelListtener.FailerCallback(1);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                Gson gson = new Gson();
                ShopBean shopBean = gson.fromJson(string, ShopBean.class);
                shopModelListtener.SuccessCallback(shopBean,page,sort);
            }
        });
    }
}

 

4.//背景

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="oval"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="18dp"/>
    <solid android:color="#EAEBEE"/>
    <size android:height="18dp" android:width="18dp"/>
</shape>

5//api

public static final String api="http://www.zhaoapi.cn/product/searchProducts?keywords=手机&page=1&sort=0";

6.//依赖

implementation 'com.squareup.okhttp3:okhttp:3.10.0'

implementation 'com.google.code.gson:gson:2.8.2'

implementation 'com.github.bumptech.glide:glide:4.6.1'

annotationProcessor 'com.github.bumptech.glide:compiler:4.6.1'

implementation 'com.github.userswlwork:pull-to-refresh:1.0.0'

implementation 'com.android.support:recyclerview-v7:27.1.1'

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Android中的布局是一种常用的布局,它可以根据内容的大小和数量自动调整控件的位置和大小,使得界面能够自适应屏幕的宽度。通常在需要展示多个标签、图片或文字等的场景下使用。 布局的特点是将内容按照先后顺序从左到右排列,当一行的宽度不足以容纳下一个控件时,会自动换行。这种布局能够节省空间,提高界面的可读性和美观性。 在Android中,可以使用FlowLayout这个第三方库来实现布局。使用FlowLayout的步骤如下:首先在项目的build.gradle文件中添加依赖,然后在布局文件中将根布局设置为FlowLayout,并在其中添加需要展示的控件,可以通过调整控件的属性来定义布局的样和排列方布局可以动态调整控件的位置和大小,可以通过设置权重来控制每个控件在水平方向上的占比,也可以设置边距来调整控件之间的间隔。另外,布局还可以为每个控件设置点击事件和长按事件,方便实现更丰富的交互效果。 总之,布局是一种灵活且强大的布局,可以有效地解决多个控件在界面上排列不下或排列不美观的问题,同时也能够提高界面的可读性和用户体验。在开发Android应用时,如果遇到需要展示多个标签、图片或文字等的场景,布局是一个很好的选择。 ### 回答2: Android布局是一种灵活的布局,用于在屏幕上动态自适应地显示一系列视图。它能够根据子视图的大小和屏幕大小自动调整子视图的位置和宽度。这种布局适用于显示不规则大小的子视图,尤其适用于显示标签、图片、标签云等。 Android布局可以通过使用LinearLayout或GridLayout来实现。在LinearLayout中,可以设置orientation属性为horizontal或vertical来实现水平或垂直布局。在GridLayout中,可以通过设置列数来控制每行显示的子视图数量。 Android布局的优点是可以根据屏幕的大小和方向自动调整子视图的布局,使得页面在不同设备上都能够良好地显示。同时,它也提供了更好的用户体验,因为用户可以在不同屏幕上以不同的方查看和交互。 然而,Android布局也存在一些限制。由于其自适应特性,子视图的大小和位置可能会受到限制。此外,较复杂的布局可能会导致性能问题,因为在布局过程中需要进行多次测量和计算。因此,在使用布局时,需要谨慎处理子视图的大小和数量,以提高性能并避免布局过于复杂。 总结来说,Android布局是一种灵活而自适应的布局,适用于显示不规则大小的子视图。它可以根据屏幕的大小和方向自动调整子视图的布局,并提供更好的用户体验。然而,需要注意处理子视图的大小和数量,以提高性能并避免布局过于复杂。 ### 回答3: Android布局(Flow Layout)是一种动态适应屏幕宽度的布局,主要用于解决在屏幕上按行排列多个子视图的问题。 在传统的线性布局中,如果视图超出屏幕宽度,就会自动换行,但是每一行只会放置一个子视图。而在布局中,子视图会根据屏幕宽度自动换行,并且每一行可以放置多个子视图,适应屏幕不同宽度的设备。 布局的使用非常方便,只需要将子视图添加到布局中即可。它提供了一些属性来控制子视图在布局中的排列方,比如子视图之间的间距、子视图的对齐方等。此外,布局还可以通过设置权重属性,实现子视图的均匀分布或者按比例分布。 布局在一些场景下非常有用,比如在标签云、瀑布展示等需要动态调整子视图排列的情况下。相比于其他布局布局可以更好地利用屏幕空间,提高用户体验。 总之,Android布局是一种动态适应屏幕宽度的布局,可以方便地排列多个子视图,并提供了一些属性来控制子视图的排列方和样。它的使用简单灵活,适用于多种场景,可以有效地提高用户界面的可用性和美观性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值