package com.example.weektwopractice.view;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
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.Toast;
import com.example.weektwopractice.R;
import com.example.weektwopractice.adapter.MyAdapter;
import com.example.weektwopractice.bean.BaseBean;
import com.example.weektwopractice.bean.DetailBean;
import com.example.weektwopractice.net.HttpUtil;
import com.example.weektwopractice.net.OnNetListener;
import java.io.IOException;
import java.util.List;
public class DetailActivity extends AppCompatActivity implements View.OnClickListener {
/**
* 请输入关键词
*/
private EditText mEtvKey;
/**
* 切换
*/
private Button mBtChange;
private RecyclerView mRv;
/**
* 搜索
*/
private Button mBtSerach;
private List<DetailBean.DataBean> list;
private boolean f;
private MyAdapter adapter;
private SwipeRefreshLayout srl;
private int page=2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
initView();
srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
Toast.makeText(DetailActivity.this,"刷新开始",Toast.LENGTH_LONG).show();
page++;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(DetailActivity.this,"刷新结束",Toast.LENGTH_LONG).show();
HttpUtil.getInstance(DetailActivity.this).doGet("http://120.27.23.105/product/searchProducts?keywords=笔记本&&page="+page, DetailBean.class, new OnNetListener() {
@Override
public void onSuccess(BaseBean baseBean) throws IOException {
DetailBean detailBean= (DetailBean) baseBean;
List<DetailBean.DataBean> data = detailBean.data;
list.addAll(0,data);
adapter.notifyDataSetChanged();
}
@Override
public void onError(IOException e) {
}
});
srl.setRefreshing(false);
}
},3000);
}
});
}
private void initView() {
mEtvKey = (EditText) findViewById(R.id.etv_key);
mBtChange = (Button) findViewById(R.id.bt_change);
mBtChange.setOnClickListener(this);
mRv = (RecyclerView) findViewById(R.id.rv);
mBtSerach = (Button) findViewById(R.id.bt_serach);
mBtSerach.setOnClickListener(this);
srl = (SwipeRefreshLayout) findViewById(R.id.srl);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_change:
//进行布局切换
if (!f){
show(false);
f=true;
}else{
show(true);
f=false;
}
break;
case R.id.bt_serach:
String key = mEtvKey.getText().toString().trim();
/*HashMap<String, String> map = new HashMap<>();
map.put("keyword","笔记本");
map.put("page","1");*/
HttpUtil.getInstance(this).doGet("http://120.27.23.105/product/searchProducts?keywords="+key+"&&page=1", DetailBean.class, new OnNetListener() {
@Override
public void onSuccess(BaseBean baseBean) throws IOException {
DetailBean detailBean = (DetailBean) baseBean;
//Toast.makeText(DetailActivity.this, detailBean.toString(), Toast.LENGTH_LONG).show();
list = detailBean.data;
show(true);
/**
* 设置点击事件
*/
adapter.setOnItemListener(new MyAdapter.OnItemListener() {
@Override
public void onItemClick(int position, DetailBean.DataBean detailBean) {
Toast.makeText(DetailActivity.this,"点击"+position,Toast.LENGTH_LONG).show();
}
@Override
public void onLongClick(int position, DetailBean.DataBean dataBean) {
Toast.makeText(DetailActivity.this,"长按"+position, Toast.LENGTH_LONG).show();
//长按删除
adapter.delete(position);
}
});
}
@Override
public void onError(IOException e) {
}
});
break;
}
}
/**
* 展示方法
* @param flag
*/
private void show(boolean flag) {
mRv.setLayoutManager(flag ? new LinearLayoutManager(this) : new GridLayoutManager(this, 2));
// mRv.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
adapter = new MyAdapter(DetailActivity.this, list);
mRv.setAdapter(adapter);
}
}
//适配器
package com.example.weektwopractice.adapter;
import android.content.Context;
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.LinearLayout;
import android.widget.TextView;
import com.example.weektwopractice.R;
import com.example.weektwopractice.bean.DetailBean;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
/**
* 2017/10/14.
*/
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<DetailBean.DataBean> list;
private static final int TYPE1 = 0;
private static final int TYPE2 = 1;
/**
* 构造方法
* @param context
* @param list
*/
public MyAdapter(Context context, List<DetailBean.DataBean> list) {
this.context = context;
this.list = list;
}
/**
* 定义接口,回调函数,点击事件
*/
private OnItemListener onItemListener;
public interface OnItemListener{
public void onItemClick(int position, DetailBean.DataBean dataBean);
public void onLongClick(int position,DetailBean.DataBean dataBean);
}
public void setOnItemListener(OnItemListener onItemListener){
this.onItemListener=onItemListener;
}
/**
* 创建布局
* @param parent
* @param viewType
* @return
*/
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
if (viewType == TYPE1) {
//布局加载器
View view1 = LayoutInflater.from(context).inflate(R.layout.item1, parent, false);
viewHolder = new Type1ViewHolder(view1);
} else {
View view2 = LayoutInflater.from(context).inflate(R.layout.item2, parent, false);
viewHolder = new Type2ViewHolder(view2);
}
return viewHolder;
}
/**
* 数据绑定展示
* @param holder
* @param position
*/
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof Type1ViewHolder) {
final DetailBean.DataBean dataBean = list.get(position);
Type1ViewHolder type1ViewHodler = (Type1ViewHolder) holder;
type1ViewHodler.item1_tv_title.setText(dataBean.title);
type1ViewHodler.item1_tv_price.setText(dataBean.price+"");
ImageLoader.getInstance().displayImage(dataBean.images, type1ViewHodler.item1_iv_image);
/**
* 点击事件
*/
type1ViewHodler.ll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onItemListener!=null){
onItemListener.onItemClick(position,dataBean);
}
}
});
/**
* 长按事件
*/
type1ViewHodler.ll.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (onItemListener!=null){
onItemListener.onLongClick(position,dataBean);
}
return true;
}
});
} else {
final DetailBean.DataBean dataBean = list.get(position);
Type2ViewHolder type2ViewHolder = (Type2ViewHolder) holder;
type2ViewHolder.item2_tv_title.setText(dataBean.title);
type2ViewHolder.item2_tv_price.setText(dataBean.price+"");
ImageLoader.getInstance().displayImage(dataBean.images, type2ViewHolder.item2_iv_image);
/**
* 点击事件
*/
type2ViewHolder.ll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onItemListener!=null){
onItemListener.onItemClick(position,dataBean);
}
}
});
/**
* 长按事件
*/
type2ViewHolder.ll.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (onItemListener!=null){
onItemListener.onLongClick(position,dataBean);
}
return true;
}
});
}
}
@Override
public int getItemCount() {
return list.size();
}
/**
* 多条目加载
* @param position
* @return
*/
@Override
public int getItemViewType(int position) {
return position % 2 == 0 ? TYPE1 : TYPE2;
}
/**
* 初始化组件
*/
private class Type1ViewHolder extends RecyclerView.ViewHolder {
private ImageView item1_iv_image;
private TextView item1_tv_title;
private TextView item1_tv_price;
private LinearLayout ll;
public Type1ViewHolder(View itemView) {
super(itemView);
item1_iv_image = itemView.findViewById(R.id.item1_iv_image);
item1_tv_title = itemView.findViewById(R.id.item1_tv_title);
item1_tv_price = itemView.findViewById(R.id.item1_tv_price);
ll = itemView.findViewById(R.id.ll);
}
}
/**
* 初始化组件
*/
private class Type2ViewHolder extends RecyclerView.ViewHolder {
private ImageView item2_iv_image;
private TextView item2_tv_price;
private TextView item2_tv_title;
private LinearLayout ll;
public Type2ViewHolder(View itemView) {
super(itemView);
item2_iv_image = itemView.findViewById(R.id.item2_iv_image);
item2_tv_price = itemView.findViewById(R.id.item2_tv_price);
item2_tv_title = itemView.findViewById(R.id.item2_tv_title);
ll = itemView.findViewById(R.id.ll);
}
}
/**
* 条目删除方法
* @param position
*/
public void delete(int position){
list.remove(position);
notifyDataSetChanged();
}
}