自定义控件展示

依赖

implementation ‘com.android.support:recyclerview-v7:28.+’
implementation ‘com.squareup.okio:okio:1.5.0’
implementation ‘com.squareup.okhttp3:okhttp:3.2.0’
implementation ‘com.squareup.okhttp3:logging-interceptor:3.4.1’
implementation ‘com.github.bumptech.glide:glide:4.9.0’
//SmartRefreshLayout 刷新的
implementation ‘com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-19’
implementation ‘com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-19’

自定义控件布局
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout … xmlns:android=“http://schemas.android.com/apk/res/android
android:layout_width=“match_parent”
android:layout_height=“match_parent”
xmlns:app=“http://schemas.android.com/apk/res-auto
android:orientation=“vertical”>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="15sp"
            android:text="返回"
            />
        <EditText
            android:id="@+id/ed_tex"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:hint="请输入要搜索的内容"
            />
        <Button
            android:id="@+id/search"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:background="#f0f"
            android:text="搜索"
            />
    </LinearLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rc"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

</LinearLayout …>

自定义控件

public class Customview extends LinearLayout {

private RecyclerView rc;
private EditText ed_tex;
private Button search;
public interface OnSearchGoods{
    void search(String tex);
}
public OnSearchGoods onSearchGoods;

public void setOnSearchGoods(OnSearchGoods onSearchGoods) {
    this.onSearchGoods = onSearchGoods;
}
public Customview(Context context) {
    super(context);
}

public Customview(final Context context, AttributeSet attrs) {
    super(context, attrs);
    LayoutInflater.from(context).inflate(R.layout.custom_layout, this);

    rc = findViewById(R.id.rc);
    ed_tex = findViewById(R.id.ed_tex);
    search = findViewById(R.id.search);

    search.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            String s = ed_tex.getText().toString();
            Log.i("fff",s);
            if (s.isEmpty()){
                Toast.makeText(context, "输入的内容不能为空", Toast.LENGTH_SHORT).show();
                return;
            }else {
                onSearchGoods.search(s);
            }
        }
    });


}

public Customview(Context context,AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

}

主页 布局
<?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">
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/fresh"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”>
<bawei.com.day221zuoye.custom.Customview
android:id="@+id/css"
android:layout_width=“match_parent”
android:layout_height=“match_parent”></bawei.com.day221zuoye.custom.Customview>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout…>

页面

public class MainActivity extends AppCompatActivity implements ShowView{

private ShowPresenter showPresenter;
private Customview css;
private int page=1;
private MyAdapter myAdapter;
private SmartRefreshLayout fresh;
private String bb="";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    showPresenter = new ShowPresenter(this);
    css = findViewById(R.id.css);
    css.setOnSearchGoods(new Customview.OnSearchGoods() {
        @Override
        public void search(String tex) {
            bb=tex;
            showPresenter.setData(tex,page);
        }
    });
    fresh = findViewById(R.id.fresh);
    fresh.setEnableRefresh(true);
    fresh.setEnableLoadMore(true);




}

@Override
public void goods(JSONArray jsonArray) {
    fresh.setOnRefreshListener(new OnRefreshListener() {
        @Override
        public void onRefresh(@NonNull RefreshLayout refreshLayout) {
            showPresenter.setData(bb,page);
            myAdapter.notifyDataSetChanged();

            fresh.finishRefresh();
        }
    });
    fresh.setOnLoadMoreListener(new OnLoadMoreListener() {
        @Override
        public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
            page++;
            showPresenter.setData(bb,page);
            myAdapter.notifyDataSetChanged();
            fresh.finishLoadMore();

        }
    });






    Log.i("yuy",jsonArray.toString());
    RecyclerView rc = findViewById(R.id.rc);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    rc.setLayoutManager(linearLayoutManager);
    myAdapter = new MyAdapter(jsonArray, MainActivity.this);
    rc.setAdapter(myAdapter);
}

}

View

package bawei.com.day221zuoye.view;

import org.json.JSONArray;

public interface ShowView {
void goods(JSONArray jsonArray);
}

Presenter

public class ShowPresenter {

private final ShowModel showModel;
private final ShowView showview;

public ShowPresenter(ShowView showView) {
    showModel = new ShowModel();
    showview = showView;
}


public void setData(String tex, int page) {
    showModel.getData(tex,page);
    showModel.setOnGetGoods(new ShowModel.OnGetGoods() {
        @Override
        public void result(JSONArray jsonArray) {
            showview.goods(jsonArray);
        }
    });
}

}

Model

public class ShowModel {
String url=“http://172.17.8.100/small/commodity/v1/findCommodityByKeyword”;
public interface OnGetGoods{
void result(JSONArray jsonArray);
}
private OnGetGoods onGetGoods;

public void setOnGetGoods(OnGetGoods onGetGoods) {
    this.onGetGoods = onGetGoods;
}

private Handler myHandler=new Handler(){
@Override
public void handleMessage(Message msg) {
    super.handleMessage(msg);
    switch (msg.what){
        case 0:
            String json = (String) msg.obj;

            try {
                JSONObject jsonObject = new JSONObject(json);
                JSONArray result = jsonObject.getJSONArray("result");
                Log.i("iii",result.toString());
                onGetGoods.result(result);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            break;
    }
}

};
public void getData(String tex, int page) {
OkHttpUtils.getInstance().doGet(url,tex,page,new Callback() {
@Override
public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            String s = response.body().string();
            Message message = new Message();
            message.what=0;
            message.obj=s;
            myHandler.sendMessage(message);
        }
    });

}

}

Utils

public class OkHttpUtils {

private static OkHttpUtils okHttpUtils;

public OkHttpUtils() {
}

public static OkHttpUtils getInstance(){
    if (okHttpUtils==null){
        synchronized (OkHttpUtils.class){
            if (okHttpUtils==null){
                okHttpUtils = new OkHttpUtils();
            }
        }
    }
    return okHttpUtils;
}
public synchronized static OkHttpClient okHttpClient(){
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
        @Override
        public void log(String message) {
            Log.i("xxx",message);
        }
    });
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(loggingInterceptor).build();
    return okHttpClient;
}
public static void doGet(String url,String ss,int page,Callback callback){
    OkHttpClient okHttpClient = okHttpClient();
    Request request=new Request.Builder()
            .url(url+"?keyword="+ss+"&page="+page+"&count=10")
            .build();
    okHttpClient.newCall(request).enqueue(callback);
}

}

MyAdapter

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
JSONArray json;
Context context;

public MyAdapter(JSONArray json, Context context) {
    this.json = json;
    this.context = context;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_layout, null, false);
    MyViewHolder myViewHolder = new MyViewHolder(view);
    return myViewHolder;
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
    try {
        JSONObject jsonObject = json.getJSONObject(i);
        String name = jsonObject.getString("commodityName");
        String pic = jsonObject.getString("masterPic");
        ((MyViewHolder)viewHolder).tex.setText(name);
        Glide.with(context).load(pic).into(((MyViewHolder)viewHolder).iv);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

@Override
public int getItemCount() {
    return json.length();
}
public class MyViewHolder extends RecyclerView.ViewHolder {

    private final TextView tex;
    private final ImageView iv;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        tex = itemView.findViewById(R.id.tex);
        iv = itemView.findViewById(R.id.img);
    }
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值