搜索页面:
布局:
<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:orientation="vertical" android:layout_height="match_parent" tools:context="com.example.week3_lianxi.view.activity.MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="60dp"> <ImageView android:id="@+id/fanhui" android:layout_width="20dp" android:layout_height="20dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="15dp" android:src="@drawable/icon_back"/> <EditText android:id="@+id/main_name" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:hint="多功能插座"/> <TextView android:id="@+id/seltxt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="15dp" android:textSize="18dp" android:text="搜索"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_alignParentBottom="true" android:background="#cdcdcd" /> </RelativeLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20dp" android:textColor="#000" android:text="热搜"/> <com.example.week3_lianxi.view.costom.MFlowLayout android:id="@+id/flow_layout" android:layout_height="80dp" android:layout_width="wrap_content"> </com.example.week3_lianxi.view.costom.MFlowLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20dp" android:textColor="#000" android:text="历史搜索"/> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="260dp"> </ListView> <Button android:id="@+id/delAll" android:onClick="delAll" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="清空历史搜索"/> </LinearLayout>
MainActivity:
public class MainActivity extends AppCompatActivity { private MFlowLayout mFlowLayout; private String mNames[]={"羊毛衫","新品","苹果8","瑜伽球","内衣","秋冬男装","女装","童装"}; private TextView textsel; private EditText editname; private ListView listView; private Dao dao; private Main_lvAdapter lvAdapter; private List<UserBean> userBeen; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mFlowLayout = (MFlowLayout) findViewById(R.id.flow_layout); textsel = (TextView) findViewById(R.id.seltxt); editname = (EditText) findViewById(R.id.main_name); listView = (ListView) findViewById(R.id.lv); dao = new Dao(this); initChildView(); userBeen = dao.selAll(); lvAdapter= new Main_lvAdapter(userBeen,MainActivity.this); listView.setAdapter(lvAdapter); textsel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String name = editname.getText().toString(); Intent intent=new Intent(MainActivity.this,SelectActivity.class); intent.putExtra("name",name); startActivity(intent); dao.add(name); userBeen = dao.selAll(); lvAdapter= new Main_lvAdapter(userBeen,MainActivity.this); listView.setAdapter(lvAdapter); } }); }
流式布局:具体可以参考其他博客 private void initChildView() { ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); lp.leftMargin = 10; lp.rightMargin = 10; lp.topMargin = 5; lp.bottomMargin = 10; for(int i = 0; i < mNames.length; i ++){ TextView view = new TextView(this); view.setText(mNames[i]); view.setTextColor(Color.WHITE); view.setBackgroundDrawable(getResources().getDrawable(R.drawable.liushi_style)); mFlowLayout.addView(view,lp); } }
清空历史记录 public void delAll(View view) { dao.delAll(); userBeen = dao.selAll(); lvAdapter= new Main_lvAdapter(userBeen,MainActivity.this); listView.setAdapter(lvAdapter); } }
数据库:
Dao:
public class Dao { private final MyOpenHelper mo; private SQLiteDatabase db; public Dao(Context context) { mo = new MyOpenHelper(context); } public void add(String name){ db = mo.getWritableDatabase(); db.execSQL("insert into stu values(null,?)",new String[]{name}); } public List<UserBean> selAll(){ db = mo.getReadableDatabase(); Cursor query = db.rawQuery("select * from stu", null); List<UserBean> list=new ArrayList<>(); while (query.moveToNext()){ int id=query.getInt(0); String name=query.getString(1); UserBean bean=new UserBean(id,name); list.add(bean); } return list; } public void delAll(){ db.execSQL("delete from stu"); } public void delID(int id){ db.execSQL("delete from stu where id=?",new String[]{id+""}); } }
holder:
public class MyOpenHelper extends SQLiteOpenHelper{ public MyOpenHelper(Context context) { super(context, "user.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table stu(id integer primary key,name varchar)"); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } }
点击搜索实现跳转
布局:
<?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="com.example.week3_lianxi.view.activity.SelectActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="60dp"> <ImageView android:id="@+id/fanhui" android:layout_width="20dp" android:layout_height="20dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="15dp" android:src="@drawable/icon_back"/> <EditText android:id="@+id/sel_name" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:hint="多功能插座"/> <ImageView android:id="@+id/selimg" android:layout_width="25dp" android:layout_height="25dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="15dp" android:src="@drawable/kind_liner" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_alignParentBottom="true" android:background="#cdcdcd" /> </RelativeLayout>这里用的是XRectclerView刷新 <com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/xrecycle_view" android:layout_width="match_parent" android:layout_height="match_parent"> </com.jcodecraeer.xrecyclerview.XRecyclerView> </LinearLayout>activity代码public class SelectActivity extends AppCompatActivity implements Sel_view_face{ List<SeleBean.DataBean> listAll=new ArrayList<>(); private XRecyclerView xRecyclerView; private SelPresenter selPresenter; private EditText editname; private int page_num=1; private ImageView image; private boolean falg=false; //private List<SeleBean.DataBean> data; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_select); //获取ID xRecyclerView = (XRecyclerView) findViewById(R.id.xrecycle_view); editname = (EditText) findViewById(R.id.sel_name); image = (ImageView) findViewById(R.id.selimg); //获取intent传过来的值 String name = getIntent().getStringExtra("name"); editname.setText(name); selPresenter = new SelPresenter(this); //向P层传值 selPresenter.setData(page_num,name, ApiUrl.sel_url); } private void setGvClick(Sel_GvAdapter adapter) { adapter.setOnItemClickListener(new MyOnItemClickListener() { @Override public void OnItemClickListener(View view, int position) { Toast.makeText(SelectActivity.this,"点击了"+position,Toast.LENGTH_SHORT).show(); Intent intent=new Intent(SelectActivity.this,GoodsActivity.class); intent.putExtra("pid",listAll.get(position).getPid()); startActivity(intent); } }); } @Override public void setvSuccess(final SeleBean seleBean) { Log.d("bean",seleBean.toString()+"--------------------"); //data = seleBean.getData(); //Log.d("data", data.toString()); runOnUiThread(new Runnable() { @Override public void run() { xRecyclerView.setLayoutManager(new LinearLayoutManager(SelectActivity.this,LinearLayoutManager.VERTICAL,false)); listAll.addAll(seleBean.getData()); final Sel_LvAdapter adapter=new Sel_LvAdapter(listAll,SelectActivity.this); xRecyclerView.setAdapter(adapter); setLvClick(adapter); image.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { falg=!falg; if(falg){ image.setImageResource(R.drawable.kind_grid); xRecyclerView.setLayoutManager(new GridLayoutManager(SelectActivity.this,2)); listAll.addAll(seleBean.getData()); Sel_GvAdapter adapter=new Sel_GvAdapter(listAll,SelectActivity.this); xRecyclerView.setAdapter(adapter); setGvClick(adapter); falg=true; } if (falg==false){ image.setImageResource(R.drawable.kind_liner); xRecyclerView.setLayoutManager(new LinearLayoutManager(SelectActivity.this,LinearLayoutManager.VERTICAL,false)); listAll.addAll(seleBean.getData()); Sel_LvAdapter adapter=new Sel_LvAdapter(listAll,SelectActivity.this); xRecyclerView.setAdapter(adapter); setLvClick(adapter); falg=false; } } }); //刷新进度条样式 xRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader); xRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.Pacman); //xRecyclerView.setArrowImageView(); xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { new Handler().postDelayed(new Runnable() { @Override public void run() { //listAll.clear();List<SeleBean.DataBean> data = seleBean.getData(); listAll.addAll(0,seleBean.getData()); if(falg==true){ //xRecyclerView.setLayoutManager(new GridLayoutManager(SelectActivity.this,2)); Sel_GvAdapter adapter=new Sel_GvAdapter(listAll,SelectActivity.this); xRecyclerView.setAdapter(adapter); // falg=true; setGvClick(adapter); } if (falg==false){ //xRecyclerView.setLayoutManager(new LinearLayoutManager(SelectActivity.this,LinearLayoutManager.VERTICAL,false)); Sel_LvAdapter adapter=new Sel_LvAdapter(listAll,SelectActivity.this); xRecyclerView.setAdapter(adapter); //falg=false; setLvClick(adapter); } xRecyclerView.refreshComplete(); } },3000); } @Override public void onLoadMore() { page_num++; Log.d("page",page_num+"-------------------"); new Handler().postDelayed(new Runnable() { @Override public void run() { listAll.addAll(seleBean.getData()); //falg=!falg; if(falg==true){ //xRecyclerView.setLayoutManager(new GridLayoutManager(SelectActivity.this,2)); Sel_GvAdapter adapter=new Sel_GvAdapter(listAll,SelectActivity.this); xRecyclerView.setAdapter(adapter); // falg=true; setGvClick(adapter); } if (falg==false){ // xRecyclerView.setLayoutManager(new LinearLayoutManager(SelectActivity.this,LinearLayoutManager.VERTICAL,false)); Sel_LvAdapter adapter=new Sel_LvAdapter(listAll,SelectActivity.this); xRecyclerView.setAdapter(adapter); // falg=false; setLvClick(adapter); } xRecyclerView.loadMoreComplete(); } },2000); } }); } }); } private void setLvClick(Sel_LvAdapter adapter) { adapter.setOnItemClickListener(new MyOnItemClickListener() { @Override public void OnItemClickListener(View view, int position) { Toast.makeText(SelectActivity.this,"点击了"+position,Toast.LENGTH_SHORT).show(); Intent intent=new Intent(SelectActivity.this,GoodsActivity.class); intent.putExtra("pid",listAll.get(position).getPid()); startActivity(intent); } }); } @Override public void setvError(String s) { } }因为有条目的点击,所以这里就写一个recyclerview的适配器:public class Sel_LvAdapter extends RecyclerView.Adapter<Sel_LvHolder>{ List<SeleBean.DataBean> data; Context con; List<String> imgs=new ArrayList<>(); private MyOnItemClickListener itemClickListener; public Sel_LvAdapter(List<SeleBean.DataBean> data, Context con) { this.data = data; this.con = con; } @Override public Sel_LvHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(con).inflate(R.layout.goods_lv_child, parent, false); final Sel_LvHolder holder=new Sel_LvHolder(view); return holder; } @Override public void onBindViewHolder(final Sel_LvHolder holder, final int position) { for (int i = 0; i< data.size(); i++){ String[] images = data.get(i).getImages().split("\\|"); Log.d("iiiii",images.toString()); for (int j=0;j<images.length;j++){ imgs.add(images[j]); } } ImageLoader.getInstance().displayImage(imgs.get(position),holder.img, ImageUtil.getoption()); holder.price.setText("¥"+data.get(position).getPrice()); holder.title.setText(data.get(position).getTitle());点击,使用的是接口 if (itemClickListener!=null){ holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { itemClickListener.OnItemClickListener(holder.itemView,holder.getLayoutPosition()); } }); } } @Override public int getItemCount() { return data.size(); } /** * 列表点击事件 * * @param itemClickListener */ public void setOnItemClickListener(MyOnItemClickListener itemClickListener) { this.itemClickListener = itemClickListener; } }点击事件的接口:public interface MyOnItemClickListener { void OnItemClickListener(View view, int position); }点击条目跳转到商品详情:布局:<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:orientation="vertical" android:layout_height="match_parent" tools:context=".view.activity.GoodsActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" > <ImageView android:id="@+id/goods_fh" android:layout_width="20dp" android:layout_height="20dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="15dp" android:src="@drawable/icon_back"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:textSize="20dp" android:text="商品详情" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_alignParentBottom="true" android:background="#cdcdcd"/> </RelativeLayout> <com.stx.xhb.xbanner.XBanner android:id="@+id/banner" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="300dp" app:AutoPlayTime="3000" app:pointsContainerBackground="#44aaaaaa" app:pointsPosition="RIGHT" app:tipTextSize="12sp" app:isShowNumberIndicator="true" app:isShowIndicatorOnlyOne="true" app:pageChangeDuration="800"> </com.stx.xhb.xbanner.XBanner> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/yj" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:text="原价"/> <TextView android:id="@+id/yh" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#ff00" android:text="优惠"/> <Button android:id="@+id/addgw" android:onClick="btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="加入购物车"/> </LinearLayout>activity代码:public class GoodsActivity extends AppCompatActivity implements Good_view_face,Add_view_face{ List<String> imgs=new ArrayList<>(); private XBanner xBanner; private TextView title; private TextView yjprice; private TextView yhprice; private GoodsPresenter goodsPresenter; private AddPresenter addPresenter; private int uid=2815; private Button button; private ImageView fh; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_goods); xBanner = (XBanner) findViewById(R.id.banner); title = (TextView) findViewById(R.id.title); yjprice = (TextView) findViewById(R.id.yj); yhprice = (TextView) findViewById(R.id.yh); button = (Button) findViewById(R.id.addgw); fh = (ImageView) findViewById(R.id.goods_fh); int pid = getIntent().getIntExtra("pid", 0); goodsPresenter = new GoodsPresenter(this); goodsPresenter.setData(pid, ApiUrl.xq_url); addPresenter = new AddPresenter(this); addPresenter.setData(uid,pid,ApiUrl.add_gwcurl); } @Override public void setvSuccess(final GoodsBean goodsBean) { runOnUiThread(new Runnable() { @Override public void run() { fh.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { finish(); } }); GoodsBean.DataBean data = goodsBean.getData(); Log.d("---",data.getImages()); String[] images = data.getImages().split("\\|"); for (int i=0;i<images.length;i++){ imgs.add(images[i]); } Log.d("---",imgs.toString()); xBanner.setData(imgs,null); xBanner.setmAdapter(new XBanner.XBannerAdapter() { @Override public void loadBanner(XBanner banner, Object model, View view, int position) { Glide.with(GoodsActivity.this).load(imgs.get(position)).into((ImageView) view); } }); title.setText(data.getTitle()); yjprice.setText("原价:"+data.getPrice()); yhprice.setText("优惠价:"+data.getBargainPrice()); yjprice.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG ); //中间横线 } }); } @Override public void setvSuccess(final SucessBean sucessBean) { runOnUiThread(new Runnable() { @Override public void run() { button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String code = sucessBean.getCode(); if ("0".equals(code)){ Toast.makeText(GoodsActivity.this,sucessBean.getMsg(),Toast.LENGTH_SHORT).show(); }else { Toast.makeText(GoodsActivity.this,sucessBean.getMsg(),Toast.LENGTH_SHORT).show(); } } }); } }); } @Override public void setvError(String s) { } }