//相当于一个流式布局
package view;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.bawie.weekthree_moni.R;
import java.util.List;
import custor.MyViewLiuShi;
import util.Dao;
public class MainActivity extends AppCompatActivity{
private MyViewLiuShi mFlowLayout;
private String mNames[] = {
"welcome","android","TextView",
"apple","jamy","kobe bryant",
"jordan","layout","viewgroup",
"margin","padding","text"
};
private TextView ed_text;
private ListView lv;
private LinearLayout search_clear;
private LinearLayout search_history;
private List<String> list;
private ArrayAdapter adapter;
private Dao dao;
private Button search;
private String sname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
search = (Button) findViewById(R.id.search);
search_history = (LinearLayout) findViewById(R.id.search_history);
search_clear = (LinearLayout) findViewById(R.id.search_clear);
ed_text = (TextView) findViewById(R.id.ed_text);
lv = (ListView) findViewById(R.id.lv);
initChildViews();//流式布局
dao = new Dao(MainActivity.this);
//查询历史搜索
dao = new Dao(this);
list = dao.select();
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
//集合的长度决定历史搜索是否显示
ishide();
}
private void initChildViews() {
// TODO Auto-generated method stub
mFlowLayout = (MyViewLiuShi) findViewById(R.id.flowlayout);
ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.leftMargin = 5;
lp.rightMargin = 5;
lp.topMargin = 5;
lp.bottomMargin = 5;
for(int i = 0; i < mNames.length; i ++){
TextView view = new TextView(this);
view.setText(mNames[i]);
view.setTextColor(Color.BLACK);
view.setTextSize(15);
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.liushi_shape));
mFlowLayout.addView(view,lp);
}
}
//清除历史
public void clear(View view) {
dao.del();
list = dao.select();
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
//集合的长度决定历史搜索是否显示
ishide();
}
//搜索
public void search(View view) {
sname = ed_text.getText().toString();
Intent intent = new Intent(MainActivity.this,ShowActivity.class);
intent.putExtra("sname",sname);
startActivity(intent);
if(sname!=null){
dao.add(sname);//添加
}
//查询
list = dao.select();
//设置适配器
adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
//条目点击事件
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(i == list.size()-1){
dao.del();
list = dao.select();
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
//集合的长度决定历史搜索是否显示
ishide();
}
}
});
//集合的长度决定历史搜索是否显示
ishide();
}
private void ishide() {
if(list.size() > 0){
search_history.setVisibility(View.VISIBLE);
search_clear.setVisibility(View.VISIBLE);
}else{
search_history.setVisibility(View.GONE);
search_clear.setVisibility(View.GONE);
}
}
}
//布局里
<?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="view.MainActivity" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/fan" android:textSize="35dp" android:text="<"/> <EditText android:id="@+id/ed_text" android:layout_width="240dp" android:layout_gravity="center_horizontal" android:layout_height="wrap_content" /> <Button android:textSize="25dp" android:text="搜索" android:id="@+id/search" android:onClick="search" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <TextView android:background="#f1ebeb" android:layout_width="match_parent" android:layout_height="2dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="10dp"> <TextView android:text="热搜" android:textSize="25dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <custor.MyViewLiuShi android:id="@+id/flowlayout" android:layout_width="match_parent" android:layout_height="120dp"></custor.MyViewLiuShi> </LinearLayout> <TextView android:background="#f1ebeb" android:layout_width="match_parent" android:layout_height="6dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/search_history" android:padding="10dp"> <TextView android:text="历史搜索" android:textSize="25dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="370dp"></ListView> </LinearLayout> <LinearLayout android:layout_width="162dp" android:layout_height="42dp" android:layout_gravity="center" android:id="@+id/search_clear" android:gravity="center" android:layout_marginBottom="0dp" android:background="#000"> <Button android:text="清空搜索历史" android:onClick="clear" android:layout_width="160dp" android:layout_height="40dp" android:background="#fff"/> </LinearLayout> </LinearLayout>
//流失布局的类::
package custor;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
public class MyViewLiuShi extends ViewGroup {
//存储所有子View
private List<List<View>> mAllChildViews = new ArrayList<>();
//每一行的高度
private List<Integer> mLineHeight = new ArrayList<>();
public MyViewLiuShi(Context context) {
this(context, null);
// TODO Auto-generated constructor stub
}
public MyViewLiuShi(Context context, AttributeSet attrs) {
this(context, attrs, 0);
// TODO Auto-generated constructor stub
}
public MyViewLiuShi(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
//父控件传进来的宽度和高度以及对应的测量模式
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
//如果当前ViewGroup的宽高为wrap_content的情况
int width = 0;//自己测量的 宽度
int height = 0;//自己测量的高度
//记录每一行的宽度和高度
int lineWidth = 0;
int lineHeight = 0;
//获取子view的个数
int childCount = getChildCount();
for(int i = 0;i < childCount; i ++){
View child = getChildAt(i);
//测量子View的宽和高
measureChild(child, widthMeasureSpec, heightMeasureSpec);
//得到LayoutParams
MarginLayoutParams lp = (MarginLayoutParams) getLayoutParams();
//子View占据的宽度
int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
//子View占据的高度
int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
//换行时候
if(lineWidth + childWidth > sizeWidth){
//对比得到最大的宽度
width = Math.max(width, lineWidth);
//重置lineWidth
lineWidth = childWidth;
//记录行高
height += lineHeight;
lineHeight = childHeight;
}else{//不换行情况
//叠加行宽
lineWidth += childWidth;
//得到最大行高
lineHeight = Math.max(lineHeight, childHeight);
}
//处理最后一个子View的情况
if(i == childCount -1){
width = Math.max(width, lineWidth);
height += lineHeight;
}
}
//wrap_content
setMeasuredDimension(modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width,
modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
mAllChildViews.clear();
mLineHeight.clear();
//获取当前ViewGroup的宽度
int width = getWidth();
int lineWidth = 0;
int lineHeight = 0;
//记录当前行的view
List<View> lineViews = new ArrayList<View>();
int childCount = getChildCount();
for(int i = 0;i < childCount; i ++){
View child = getChildAt(i);
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
int childWidth = child.getMeasuredWidth();
int childHeight = child.getMeasuredHeight();
//如果需要换行
if(childWidth + lineWidth + lp.leftMargin + lp.rightMargin > width){
//记录LineHeight
mLineHeight.add(lineHeight);
//记录当前行的Views
mAllChildViews.add(lineViews);
//重置行的宽高
lineWidth = 0;
lineHeight = childHeight + lp.topMargin + lp.bottomMargin;
//重置view的集合
lineViews = new ArrayList();
}
lineWidth += childWidth + lp.leftMargin + lp.rightMargin;
lineHeight = Math.max(lineHeight, childHeight + lp.topMargin + lp.bottomMargin);
lineViews.add(child);
}
//处理最后一行
mLineHeight.add(lineHeight);
mAllChildViews.add(lineViews);
//设置子View的位置
int left = 0;
int top = 0;
//获取行数
int lineCount = mAllChildViews.size();
for(int i = 0; i < lineCount; i ++){
//当前行的views和高度
lineViews = mAllChildViews.get(i);
lineHeight = mLineHeight.get(i);
for(int j = 0; j < lineViews.size(); j ++){
View child = lineViews.get(j);
//判断是否显示
if(child.getVisibility() == View.GONE){
continue;
}
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
int cLeft = left + lp.leftMargin;
int cTop = top + lp.topMargin;
int cRight = cLeft + child.getMeasuredWidth();
int cBottom = cTop + child.getMeasuredHeight();
//进行子View进行布局
child.layout(cLeft, cTop, cRight, cBottom);
left += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
}
left = 0;
top += lineHeight;
}
}
/**
* 与当前ViewGroup对应的LayoutParams
*/
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
// TODO Auto-generated method stub
return new MarginLayoutParams(getContext(), attrs);
}
}
//搜索时跳转到另外一个界面::
package view;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.bawie.weekthree_moni.R;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import adapter.SearchRecyclerAdapter;
import adapter.SearchRecyclerGVAdapter;
import bean.SearchBean;
import presenter.SearchPresenter;
import util.ApiUtil;
import view.interfac.IViewSearch;
import view.interfac.OnItemClickListener;
public class ShowActivity extends AppCompatActivity implements IViewSearch {
private XRecyclerView xrv;
private CheckBox ck;
boolean flag = false;//定义全局变量用来保存按钮的状态
private TextView fan;
private SearchPresenter searchPresenter;
private List<SearchBean.DataBean> data = new ArrayList<>();
private String encodecontent;
private int num = 1;
private SearchRecyclerAdapter adapter;
private SearchRecyclerGVAdapter gvAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
fan = (TextView) findViewById(R.id.fan);
xrv = (XRecyclerView) findViewById(R.id.xrv);
ck = (CheckBox) findViewById(R.id.ck);
try {
//接收搜索页面传来的数据
Intent intent = getIntent();
String sname = intent.getStringExtra("sname");
encodecontent = URLEncoder.encode(sname, "utf-8");
searchPresenter = new SearchPresenter(this);
searchPresenter.getData(encodecontent,"1", ApiUtil.SEARCH_URL);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
private void setAdapter(List<SearchBean.DataBean> data) {
//设置适配器
adapter = new SearchRecyclerAdapter(ShowActivity.this,data);
xrv.setAdapter(adapter);
//设置管理器
xrv.setLayoutManager(new LinearLayoutManager(ShowActivity.this, LinearLayout.VERTICAL,false));
}
@Override
public void onSuccess(final SearchBean searchBean) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(ShowActivity.this,searchBean.getMsg(), Toast.LENGTH_SHORT).show();
data.addAll(searchBean.getData());
//设置默认数据
setAdapter(data);//设置管理器、适配器
ck.setChecked(flag);//设置按钮默认值
//点击事件
ck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(flag){
setAdapter(data);//设置管理器、适配器
ck.setChecked(false);
flag = ck.isChecked();
}else{
//设置适配器
gvAdapter = new SearchRecyclerGVAdapter(ShowActivity.this, data);
xrv.setAdapter(gvAdapter);
//设置管理器--瀑布
xrv.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
ck.setChecked(true);
flag = ck.isChecked();
}
gvAdapter.setMyItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Intent intent = new Intent(ShowActivity.this,XiangQingActivity.class);
intent.putExtra("pid",data.get(position).getPid());
startActivity(intent);
}
});
}
});
//返回
fan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
//设置XRecyclerView的上下拉监听方法
xrv.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
data.clear();
searchPresenter.getData(encodecontent,"1",ApiUtil.SEARCH_URL);
xrv.refreshComplete();//刷新完成
}
@Override
public void onLoadMore() {
num++;
searchPresenter.getData(encodecontent,num+"",ApiUtil.SEARCH_URL);
xrv.loadMoreComplete();//加载完成
}
});
//点击事件
adapter.setMyItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
//Toast.makeText(ShowActivity.this, data.get(position).getTitle(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(ShowActivity.this,XiangQingActivity.class);
intent.putExtra("pid",data.get(position).getPid());
startActivity(intent);
}
});
}
});
}
}
//布局示例::
<?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="view.ShowActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="60dp" android:padding="10dp"> <TextView android:textSize="35dp" android:id="@+id/fan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="<"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="搜索商品" android:textSize="35dp" android:layout_centerInParent="true"/> <CheckBox android:button="@null" android:layout_width="30dp" android:layout_height="30dp" android:id="@+id/ck" android:background="@drawable/myselect" android:layout_alignParentRight="true" /> </RelativeLayout> <TextView android:background="#f1ebeb" android:layout_width="match_parent" android:layout_height="3dp"/> <com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/xrv" android:layout_width="wrap_content" android:layout_height="wrap_content"></com.jcodecraeer.xrecyclerview.XRecyclerView> </LinearLayout>
//搜索的model::
package model;
import android.util.Log;
import com.google.gson.Gson;
import java.io.IOException;
import bean.SearchBean;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import presenter.IPresenterSearch;
import util.OkHttp3Util;
public class SearchModel {
private IPresenterSearch iPresenterSearch;
public SearchModel(IPresenterSearch iPresenterSearch) {
this.iPresenterSearch = iPresenterSearch;
}
public void getData(String sname,String page, String searchUrl) {
/* Map<String, String> map = new HashMap<>();
map.put("keywords",sname);
map.put("page","1");
map.put("source","android");*/
searchUrl = searchUrl+"?keywords="+sname+"&page="+page+"&source=android";
Log.d("url",searchUrl);
OkHttp3Util.doGet(searchUrl, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if(response.isSuccessful()){
String json = response.body().string();
if(json!=null){
Gson gson = new Gson();
SearchBean searchBean = gson.fromJson(json, SearchBean.class);
iPresenterSearch.onSuccess(searchBean);
}
}
}
});
}
}
//搜索的P层::
//p层的接口
package presenter;
import bean.SearchBean;
public interface IPresenterSearch {
void onSuccess(SearchBean searchBean);
}
//p层的实现类
package presenter;
import bean.SearchBean;
import model.SearchModel;
import view.interfac.IViewSearch;
public class SearchPresenter implements IPresenterSearch{
private IViewSearch iViewSearch;
private final SearchModel searchModel;
public SearchPresenter(IViewSearch iViewSearch) {
this.iViewSearch = iViewSearch;
searchModel = new SearchModel(this);
}
public void getData(String sname,String page, String searchUrl) {
searchModel.getData(sname, page,searchUrl);
}
@Override
public void onSuccess(SearchBean searchBean) {
iViewSearch.onSuccess(searchBean);
}
}
//搜索的V层
package view.interfac;
import bean.SearchBean;
public interface IViewSearch {
void onSuccess(SearchBean searchBean);
}
//数据库dao类::
package util;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
public class Dao {
MyDBHelper helper;
Context context;
private SQLiteDatabase sqLiteDatabase;
public Dao(Context context) {
this.helper = new MyDBHelper(context);
}
public void add(String content){
sqLiteDatabase = helper.getWritableDatabase();
sqLiteDatabase.execSQL("insert into search_tb (content) values (?)",new Object[]{content});
sqLiteDatabase.close();
}
public List<String> select(){
List<String> list = new ArrayList<>();
sqLiteDatabase = helper.getReadableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("select *from search_tb", null);
while(cursor.moveToNext()){
String string = cursor.getString(1);
list.add(string);
}
return list;
}
public void del(){
sqLiteDatabase = helper.getWritableDatabase();
sqLiteDatabase.execSQL("delete from search_tb");
sqLiteDatabase.close();
}
}
//数据库操作类::
package util;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDBHelper extends SQLiteOpenHelper {
public MyDBHelper(Context context) {
super(context, "search_database", null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table search_tb(id Integer primary key,content varchar(50))");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
//竖向排列的适配器::
package adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bawie.weekthree_moni.R;
import com.bumptech.glide.Glide;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.util.List;
import bean.SearchBean;
import holder.SearchRecyclerHolder;
import view.interfac.OnItemClickListener;
public class SearchRecyclerAdapter extends XRecyclerView.Adapter<SearchRecyclerHolder> implements View.OnClickListener{
Context context;
List<SearchBean.DataBean> data;
private OnItemClickListener monItemClickListener;
//暴露给外面的调用者,定义一个设置Listener的方法():
public void setMyItemClickListener(OnItemClickListener monItemClickListener){
this.monItemClickListener = monItemClickListener;
}
public SearchRecyclerAdapter(Context context, List<SearchBean.DataBean> data) {
this.context = context;
this.data = data;
}
@Override
public SearchRecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
SearchRecyclerHolder searchRecyclerHolder = new SearchRecyclerHolder(view);
view.setOnClickListener(this);
return searchRecyclerHolder;
}
@Override
public void onBindViewHolder(SearchRecyclerHolder holder, int position) {
SearchBean.DataBean dataBean = data.get(position);
String images = dataBean.getImages();
String[] split = images.split("\\|");
//设置数据
Glide.with(context).load(split[0]).into(holder.img);
holder.price.setText(dataBean.getPrice()+"");
holder.title.setText(dataBean.getTitle());
holder.itemView.setTag(position);
}
@Override
public int getItemCount() {
return data.size();
}
@Override
public void onClick(View view) {
if(monItemClickListener != null){
monItemClickListener.onItemClick(view, (Integer) view.getTag());
}
}
}
//横向排列的适配器::
package adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bawie.weekthree_moni.R;
import com.bumptech.glide.Glide;
import java.util.List;
import bean.SearchBean;
import holder.SearchRecyclerHolder;
import view.interfac.OnItemClickListener;
public class SearchRecyclerGVAdapter extends RecyclerView.Adapter<SearchRecyclerHolder> implements View.OnClickListener{
Context context;
List<SearchBean.DataBean> data;
private OnItemClickListener monItemClickListener;
public void setMyItemClickListener(OnItemClickListener onItemClickListener){
this.monItemClickListener = monItemClickListener;
}
public SearchRecyclerGVAdapter(Context context, List<SearchBean.DataBean> data) {
this.context = context;
this.data = data;
}
@Override
public SearchRecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.grid_item, parent, false);
SearchRecyclerHolder searchRecyclerHolder = new SearchRecyclerHolder(view);
return searchRecyclerHolder;
}
@Override
public void onBindViewHolder(SearchRecyclerHolder holder, int position) {
SearchBean.DataBean dataBean = data.get(position);
String images = dataBean.getImages();
String[] split = images.split("\\|");
//设置数据
Glide.with(context).load(split[0]).into(holder.img);
holder.price.setText(dataBean.getPrice()+"");
holder.title.setText(dataBean.getTitle());
holder.itemView.setTag(position);
}
@Override
public int getItemCount() {
return data.size();
}
@Override
public void onClick(View view) {
if(monItemClickListener != null){
monItemClickListener.onItemClick(view, (Integer) view.getTag());
}
}
}
//适配器点击的接口::
package view.interfac;
import android.view.View;
public interface OnItemClickListener {
void onItemClick(View view , int position);
}
//详情的model::
package model;
import com.google.gson.Gson;
import java.io.IOException;
import bean.XiangQingBean;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import presenter.IPresenterXiangQing;
import util.OkHttp3Util;
public class XiangQingModel {
private IPresenterXiangQing iPresenterXiangQing;
public XiangQingModel(IPresenterXiangQing iPresenterXiangQing) {
this.iPresenterXiangQing = iPresenterXiangQing;
}
public void getData(int pid, String searchUrl) {
//searchUrl = searchUrl+"?pid="+pid+"&source=android";
OkHttp3Util.doGet(searchUrl+"?pid="+pid, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if(response.isSuccessful()){
String json = response.body().string();
if(json!=null){
Gson gson = new Gson();
XiangQingBean xiangQingBean = gson.fromJson(json, XiangQingBean.class);
iPresenterXiangQing.onSuccess(xiangQingBean);
}
}
}
});
}
}
//详情的p::
//接口
package presenter;
import bean.XiangQingBean;
public interface IPresenterXiangQing {
void onSuccess(XiangQingBean xiangQingBean);
}
//实现
package presenter;
import bean.XiangQingBean;
import model.XiangQingModel;
import view.interfac.IViewXiangQing;
public class XiangQingPresenter implements IPresenterXiangQing{
private IViewXiangQing iViewXiangQing;
private final XiangQingModel xiangQingModel;
public XiangQingPresenter(IViewXiangQing iViewXiangQing) {
this.iViewXiangQing = iViewXiangQing;
xiangQingModel = new XiangQingModel(this);
}
public void getData(int pid, String searchUrl) {
xiangQingModel.getData(pid,searchUrl);
}
@Override
public void onSuccess(XiangQingBean xiangQingBean) {
iViewXiangQing.onSuccess(xiangQingBean);
}
}
//详情的V::
package view.interfac;
import bean.XiangQingBean;
public interface IViewXiangQing {
void onSuccess(XiangQingBean xiangQingBean);
}
//详情的activity::
package view;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bawie.weekthree_moni.R;
import com.bumptech.glide.Glide;
import com.stx.xhb.xbanner.XBanner;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import bean.XiangQingBean;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import presenter.XiangQingPresenter;
import util.ApiUtil;
import util.OkHttp3Util;
import view.interfac.IViewXiangQing;
public class XiangQingActivity extends AppCompatActivity implements IViewXiangQing{
private XBanner banner_1;
private XiangQingPresenter xiangQingPresenter;
List<String> imglist = new ArrayList<>();
private TextView xq_title;
private TextView xq_yuanprice;
private TextView xq_price;
private int pid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xiang_qing);
TextView fan = (TextView) findViewById(R.id.fan);
banner_1 = (XBanner) findViewById(R.id.banner_1);
xq_title = (TextView) findViewById(R.id.xq_title);
xq_price = (TextView) findViewById(R.id.xq_price);
xq_yuanprice = (TextView) findViewById(R.id.xq_yuanprice);
xiangQingPresenter = new XiangQingPresenter(this);
//返回
fan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
//得到自条目的索引值
pid = getIntent().getIntExtra("pid", 0);
xiangQingPresenter.getData(pid, ApiUtil.XIANGQING_URL);
}
@Override
public void onSuccess(final XiangQingBean xiangQingBean) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if(xiangQingBean!=null){
XiangQingBean.DataBean data = xiangQingBean.getData();
String images = data.getImages();
String[] split = images.split("\\|");
for(String imgurl:split){
imglist.add(imgurl);
}
//设置图片
banner_1.setData(imglist);
banner_1.setPointsIsVisible(false);
banner_1.setmAutoPlayAble(false);
banner_1.setmAdapter(new XBanner.XBannerAdapter() {
@Override
public void loadBanner(XBanner banner, View view, int position) {
Glide.with(XiangQingActivity.this).load(imglist.get(position)).into((ImageView) view);
}
});
//设置数据
xq_title.setText(data.getTitle());
xq_price.setText("优惠价:"+data.getPrice());
xq_yuanprice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
xq_yuanprice.setText("原价:"+data.getBargainPrice());
}
}
});
}
//添加购物车
public void addCar(View view) {
OkHttp3Util.doGet(ApiUtil.ADD_CART + "?pid=" + pid + "&uid=2845", new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if(response.isSuccessful()){
final String json = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
if(json!=null){
try {
JSONObject obj = new JSONObject(json);
String code = obj.getString("code");
if("0".equals(code)){
Toast.makeText(XiangQingActivity.this,obj.getString("msg"),Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(XiangQingActivity.this,obj.getString("msg"),Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
}
}
});
}
}
//布局示例:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="view.XiangQingActivity"> <LinearLayout android:id="@+id/linear1" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp"> <TextView android:textSize="35dp" android:id="@+id/fan" android:text="<" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="30dp" android:text="商品详情"/> </LinearLayout> <TextView android:layout_below="@id/linear1" android:id="@+id/linear2" android:background="#f1ebeb" android:layout_width="match_parent" android:layout_height="3dp" /> <com.stx.xhb.xbanner.XBanner xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_below="@id/linear2" android:id="@+id/banner_1" android:layout_width="match_parent" android:layout_height="220dp"></com.stx.xhb.xbanner.XBanner> <LinearLayout android:layout_below="@id/banner_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="10dp"> <TextView android:id="@+id/xq_title" android:layout_marginBottom="8dp" android:textSize="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/xq_yuanprice" android:textSize="20dp" android:layout_marginBottom="8dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/xq_price" android:textColor="#f00" android:textSize="20dp" android:layout_marginBottom="8dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#000"/> <Button android:background="#fff" android:onClick="addCar" android:text="加入购物车" android:textSize="25dp" android:layout_marginBottom="0dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </RelativeLayout>