imageLoder依赖:
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
okHttp的封装类
okHttp依赖:
compile 'com.squareup.okhttp3:okhttp:3.9.0'
springView依赖:
compile 'com.liaoinstan.springview:library:1.2.6'
recyclerview依赖:
compile 'com.android.support:recyclerview-v7:25.3.1'
配置一些权限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
MainActivity类
public class MainActivity extends AppCompatActivity {
String num="1";
private static final String TAG = "";
@Bind(R.id.shuru)
EditText shuru;
@Bind(R.id.sousuo)
Button sousuo;
@Bind(R.id.recycler)
RecyclerView recycler;
List<DataDataBean> list=new ArrayList<DataDataBean>();
private Button btn;
private SpringView springview;
private String string;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn_activity_main);
springview = (SpringView) findViewById(R.id.springview);
ButterKnife.bind(this);
//加载默认数据
getData("笔记本",num);
//刷新
springview.setHeader(new DefaultHeader(this));
springview.setFooter(new DefaultFooter(this));
springview.setListener(new SpringView.OnFreshListener() {
@Override
public void onRefresh() {
int i = Integer.parseInt(num);
i++;
String s = String.valueOf(i);
Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();
num=s;
getData("笔记本",num);
springview.onFinishFreshAndLoad();
}
@Override
public void onLoadmore() {
getData("笔记本","2");
springview.onFinishFreshAndLoad();
}
});
}
@OnClick(R.id.sousuo)
public void onClick() {
//拿到输入的数据
string = shuru.getText().toString();
//点击搜索出来相应的
getData(string,"1");
}
//获取数据
public void getData(String name,String num){
OkhttpUtils.getInstance().asy(null, "http://120.27.23.105/product/searchProducts?keywords="+name+"&page="+num+"&source=android", new AbstractUiCallBack<DataDataBean>() {
@Override
public void success(DataDataBean dataBean) {
//单列数据列表式展示
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
//网格式布局
final GridLayoutManager gridlayoutmanger = new GridLayoutManager(MainActivity.this,2);
//标识符
final boolean[] flag = {true};
recycler.setLayoutManager(linearLayoutManager);
MyAdapter myadapter= new MyAdapter(MainActivity.this,dataBean.getData());
recycler.setAdapter(myadapter);
myadapter.notifyDataSetChanged();
//切换布局点击事件
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(flag[0]){
recycler.setLayoutManager(gridlayoutmanger);
flag[0] =false;
}else{
recycler.setLayoutManager(linearLayoutManager);
flag[0]=true;
}
}
});
}
@Override
public void failure(Exception e) {
}
});
}
}
activity_main布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.bawei.lilufei1509d20171113.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="输入商品"
android:layout_centerHorizontal="true"
android:textSize="24sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="切换布局"
android:id="@+id/btn_activity_main"/>
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#BEBEBE"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:hint="输入关键字"
android:id="@+id/shuru"
android:padding="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索"
android:id="@+id/sousuo"
android:padding="20dp"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#BEBEBE"/>
<com.liaoinstan.springview.widget.SpringView
android:layout_width="match_parent"
android:id="@+id/springview"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler">
</android.support.v7.widget.RecyclerView>
</com.liaoinstan.springview.widget.SpringView>
<!-- <GridView
android:id="@+id/gridview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:columnWidth="60dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:padding="10dp"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
<ListView
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
-->
</LinearLayout>
MyAdapter适配器
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ImageViewHolder> {
Context context;
List<DataDataBean.DataBean> list;
private int itemWidth;
public MyAdapter(Context context, List<DataDataBean.DataBean> list) {
this.context = context;
this.list = list;
// WindowManager wm = (WindowManager) context
// .getSystemService(Context.WINDOW_SERVICE);
//
// int width = wm.getDefaultDisplay().getWidth();
// itemWidth = width / 3;
}
@Override
public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.layout_item, null);
return new ImageViewHolder(view);
}
@Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
holder.title.setText(list.get(position).getTitle());
String[] split = list.get(position).getImages().split("|");
Log.i("---",split[position]);
ImageLoader.getInstance().displayImage(split[position], holder.image);
}
@Override
public int getItemCount() {
return list.size();
}
static class ImageViewHolder extends RecyclerView.ViewHolder{
@Bind(R.id.image)
ImageView image;
@Bind(R.id.title)
TextView title;
ImageViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
}
layout_item布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/image"
android:src="@mipmap/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sdghjdkjsf"
android:layout_gravity="center_vertical"
android:id="@+id/title"/>
</LinearLayout>
</LinearLayout>
MyApp类
在清单文件里调用MyApppublic class MyApp extends Application{ @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration imageLoaderConfiguration = new ImageLoaderConfiguration.Builder(this).build(); ImageLoader.getInstance().init(imageLoaderConfiguration); } }