搜索的布局
显示的布局
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:id="@+id/souxiangDrawerLayout"
tools:context="com.example.week3.view.Activity.SouSuoShow">
android:layout_height="match_parent"
android:id="@+id/souxiangScrollView"
>
android:layout_height="match_parent"
android:orientation="vertical">
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="80dp">
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_marginRight="10dp"
android:layout_height="match_parent">
android:layout_width="30dp"
android:layout_height="25dp"
android:src="@drawable/icon_back"
/>
android:background="@drawable/search_bg"
android:layout_weight="1"
android:layout_width="0dp"
android:gravity="center_vertical"
android:id="@+id/souxiangsousuo"
android:layout_height="40dp">
android:layout_height="wrap_content"
android:src="@drawable/search_icon2"
/>
android:padding="2dp"
android:layout_width="0dp"
android:textSize="18dp"
android:layout_height="wrap_content"
android:text="都市丽人领劵满399减200"
android:background="@null"
android:id="@+id/souxiangkeywords"
/>
android:layout_height="wrap_content"
android:src="@drawable/root"
/>
android:gravity="center"
android:orientation="vertical"
android:layout_marginRight="15dp"
android:layout_width="wrap_content"
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/biaoge"
/>
android:layout_height="1dp"
android:background="#B9C0C5"/>
android:layout_height="60dp">
android:layout_weight="1"
android:gravity="center"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:gravity="center"
android:text="综合"
android:id="@+id/souxiangzong"
android:textSize="20dp"
android:textColor="#F00"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="▼"
android:textColor="#F00"/>
android:layout_weight="1"
android:gravity="center"
android:layout_height="match_parent"
android:text="销量"
android:textSize="20dp"
android:id="@+id/souxiangxiao"
android:textColor="#000"/>
android:layout_weight="1"
android:layout_height="match_parent"
android:gravity="center"
>
android:gravity="center"
android:layout_height="match_parent"
android:text="价格"
android:id="@+id/souxiangjiage"
android:textSize="20dp"
android:textColor="#000"/>
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_marginLeft="5dp"
>
android:layout_height="wrap_content"
android:textColor="#F00"
android:text="▲"
android:id="@+id/souxiangjiageUp"
/>
android:layout_height="wrap_content"
android:textColor="#000"
android:text="▼"
android:id="@+id/souxiangjiageDown"
android:layout_marginTop="-15dp"
/>
android:layout_weight="1"
android:gravity="center"
android:layout_height="match_parent">
android:gravity="center"
android:layout_height="match_parent"
android:text="筛选"
android:id="@+id/souxiangshuai"
android:textSize="20dp"
android:textColor="#F00"/>
android:layout_height="15dp"
android:background="@drawable/shaixuan"/>
android:layout_height="1dp"
android:background="#B9C0C5"/>
android:layout_height="60dp"
>
android:layout_weight="1"
android:gravity="center"
android:layout_height="match_parent">
android:layout_height="30dp"
android:background="@drawable/souxiangkuang"
android:gravity="center">
android:layout_height="match_parent"
android:text="12.12"
android:gravity="center"
android:textColor="#F00"/>
android:layout_weight="1"
android:gravity="center"
android:layout_height="match_parent">
android:layout_height="30dp"
android:background="@drawable/souxiangkuang"
android:gravity="center">
android:layout_height="match_parent"
android:text="送到家"
android:gravity="center"
android:textColor="#000"/>
android:layout_weight="1"
android:gravity="center"
android:layout_height="match_parent">
android:layout_height="30dp"
android:background="@drawable/souxiangkuang"
android:gravity="center">
android:layout_height="match_parent"
android:text="品牌"
android:gravity="center"
android:textColor="#000"/>
android:layout_height="8dp"
android:background="@drawable/down"
android:layout_marginLeft="5dp"/>
android:layout_weight="1"
android:gravity="center"
android:layout_height="match_parent">
android:layout_height="30dp"
android:background="@drawable/souxiangkuang"
android:gravity="center">
android:layout_height="match_parent"
android:text="大小"
android:gravity="center"
android:textColor="#000"/>
android:layout_height="8dp"
android:background="@drawable/down"
android:layout_marginLeft="5dp"/>
android:layout_height="1dp"
android:background="#B9C0C5"
android:layout_marginBottom="10dp"/>
android:layout_height="match_parent"
android:id="@+id/souxiangListView"
>
android:layout_height="match_parent"
android:id="@+id/souxiangGridView"
android:numColumns="2"
android:visibility="gone"
>
android:layout_height="match_parent"
android:layout_gravity="end"
android:id="@+id/souxiangcebian"
>
android:layout_height="match_parent"
/>
android:layout_height="30dp"
android:id="@+id/cebianrg"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
android:layout_weight="1"
android:layout_height="match_parent"
android:text="神兽青龙"
android:checked="true"
android:textSize="20dp"
android:button="@null"
android:gravity="center"
android:background="@drawable/cebiandianji"
android:textColor="#FFF"/>
android:layout_weight="1"
android:layout_height="match_parent"
android:text="凶兽梼杌"
android:checked="true"
android:textSize="20dp"
android:button="@null"
android:gravity="center"
android:background="@drawable/cebiandianji"
android:textColor="#FFF"/>
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:text="凶兽穷奇"
android:textSize="20dp"
android:button="@null"
android:gravity="center"
android:background="@drawable/cebiandianji"
android:textColor="#FFF"/>
android:layout_weight="1"
android:layout_height="match_parent"
android:text="神兽朱雀"
android:checked="true"
android:textSize="20dp"
android:button="@null"
android:gravity="center"
android:background="@drawable/cebiandianji"
android:textColor="#FFF"/>
activity中的代码
搜索的代码
public class MainActivity extends AppCompatActivity {
private String mNames[] = {
"白板擦","德龙咖啡机","镜头手机",
"记忆棉垫","小米手环","笔记本",
"三只松鼠","晾衣架","viewgroup",
"margin","padding","text",
"name","type","search","logcat"
};
private ImageView sousuofan;
private TextView sousuobut;
private EditText sousuozhi;
private SouPresenter souPresenter;
private LinearLayout flowReSou;
private LinearLayout lishiSouSuo;
private RecyclerView resouRecyclerView;
private ListView sousuolv;
private MySousuoDao mySousuoDao;
ArrayList
private FlowLayout myflowLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myflowLayout = findViewById(R.id.myflowLayout);
sousuofan = (ImageView) findViewById(R.id.sousuofan);
sousuobut = (TextView) findViewById(R.id.sousuobut);
sousuozhi = (EditText) findViewById(R.id.sousuozhi);
flowReSou = (LinearLayout) findViewById(R.id.flowReSou);
lishiSouSuo = (LinearLayout) findViewById(R.id.lishiSouSuo);
resouRecyclerView = (RecyclerView) findViewById(R.id.resouRecyclerView);
sousuolv = (ListView) findViewById(R.id.sousuolv);
SQLiteDatabase db = openOrCreateDatabase("wc.db", MODE_PRIVATE, null);
mySousuoDao = new MySousuoDao(db);
souPresenter = new SouPresenter();
}
@Override
protected void onResume() {
super.onResume();
//获取数据库,,,判断是否有值,,有值就隐藏流式
ArrayList
if(list2.size()>0){
setLayout();
initChildViews();
}else{
initChildViews();//流式布局,,,展示数据,,,流式布局的点击事件
}
//返回按钮
sousuofan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
//搜索按钮
sousuobut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String s = sousuozhi.getText().toString();
if(s.equals("")){
Toast.makeText(MainActivity.this,"搜索内容不能为空!",Toast.LENGTH_SHORT).show();
}else{
boolean b = mySousuoDao.chaLiShi(s);
if(!b){
mySousuoDao.addShuJu(s);
}
//线布局,,,后接口
setLayout();
String sousuo = APIuil.sousuo(s);
getSouSuoContent(s,sousuo);
}
}
});
}
//流式布局,,,展示数据,,,流式布局的点击事件
private void initChildViews() {
ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.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.WHITE);
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.flowtextview));
myflowLayout.addView(view,lp);
final int finalI = i;
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//判断钙元素是否存在数据库中
boolean b = mySousuoDao.chaLiShi(mNames[finalI]);
if(!b){
mySousuoDao.addShuJu(mNames[finalI]);
}
//先改布局,,后请求接口
setLayout();
String sousuo = APIuil.sousuo(mNames[finalI]);
getSouSuoContent(mNames[finalI],sousuo);
}
});
}
}
//更改布局
private void setLayout() {
//将历史布局显示,,,并改变热搜布局
flowReSou.setVisibility(View.GONE);
lishiSouSuo.setVisibility(View.VISIBLE);
//流式横向布局
resouRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this, OrientationHelper.HORIZONTAL,false));
MyReSouAdapter myReSouAdapter = new MyReSouAdapter(MainActivity.this, mNames);
resouRecyclerView.setAdapter(myReSouAdapter);
//历史展示
ArrayList
list2.clear();
list2.addAll(list);
sousuolv.setAdapter(new MySouSuoLvAdapter(MainActivity.this,list2));
//横向流式点击
myReSouAdapter.setDianjie(new DianjiJieKou() {
@Override
public void onItemClike(int i) {
boolean b = mySousuoDao.chaLiShi(mNames[i]);
if(!b){
mySousuoDao.addShuJu(mNames[i]);
}
ArrayList
list2.clear();
list2.addAll(list1);
sousuolv.setAdapter(new MySouSuoLvAdapter(MainActivity.this,list2));
String sousuo = APIuil.sousuo(mNames[i]);
getSouSuoContent(mNames[i],sousuo);
}
});
sousuolv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> adapterView, View view, int i, long l) {
String s = list2.get(i);
String sousuo = APIuil.sousuo(s);
getSouSuoContent(s,sousuo);
}
});
}
//拼接参数,,,访问接口,,获取数据
private void getSouSuoContent(final String s, String sousuo) {
souPresenter.getContent(sousuo, new MyJieKou() {
@Override
public void onChengGong(final String json) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Gson gson = new Gson();
MySousuoBean mySousuoBean = gson.fromJson(json, MySousuoBean.class);
if(mySousuoBean.getCode().equals("0")){
if(mySousuoBean.getData().toString().equals("[]")){
Toast.makeText(MainActivity.this,"没有该类商品!",Toast.LENGTH_SHORT).show();
}else{
//跳转,,将解析出来的对象穿走
Intent intent = new Intent(MainActivity.this, SouSuoShow.class);
intent.putExtra("json",json);
intent.putExtra("keywords",s);
startActivity(intent);
finish();
}
}else{
Toast.makeText(MainActivity.this,mySousuoBean.getMsg(),Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onShiBai(final String ss) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,ss,Toast.LENGTH_SHORT).show();
}
});
}
});
}
public void sousuoqingkong(View view) {
mySousuoDao.qingLishi();
//将历史布局隐藏,,,热搜显示
flowReSou.setVisibility(View.VISIBLE);
lishiSouSuo.setVisibility(View.GONE);
}
@Override
protected void onDestroy() {
super.onDestroy();
finish();
}
}
显示列表的代码
public class SouSuoShow extends AppCompatActivity {
ArrayList
private ListView souxiangListView;
private MySouxiangLieAdapter mySouxiangLieAdapter;
private MyScrollView souxiangScrollView;
private DrawerLayout souxiangDrawerLayout;
private RadioGroup cebianrg;
private RelativeLayout souxiangcebian;
private TextView souxiangzong;
int i=0;
int j=0;
private TextView souxiangjiageUp;
private TextView souxiangjiageDown;
private TextView souxiangzongDown;
private ImageView souxiangGeShi;
int z=0;
private MySouxiangBiaoAdapter mySouxiangBiaoAdapter;
private MyGridView souxiangGridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sou_suo_show);
souxiangScrollView = (MyScrollView) findViewById(R.id.souxiangScrollView);
souxiangListView = (ListView) findViewById(R.id.souxiangListView);
TextView souxiangkeywords = (TextView) findViewById(R.id.souxiangkeywords);//搜索值
ImageView souxiangfan = (ImageView) findViewById(R.id.souxiangfan);//返回键
LinearLayout souxiangsousuo = (LinearLayout) findViewById(R.id.souxiangsousuo);
souxiangzong = (TextView) findViewById(R.id.souxiangzong);
final TextView souxiangxiao = (TextView) findViewById(R.id.souxiangxiao);
final TextView souxiangjiage = (TextView) findViewById(R.id.souxiangjiage);
final TextView souxiangshuai = (TextView) findViewById(R.id.souxiangshuai);
souxiangDrawerLayout = (DrawerLayout) findViewById(R.id.souxiangDrawerLayout);
cebianrg = (RadioGroup) findViewById(R.id.cebianrg);
souxiangcebian = (RelativeLayout) findViewById(R.id.souxiangcebian);
souxiangjiageUp = (TextView) findViewById(R.id.souxiangjiageUp);
souxiangjiageDown = (TextView) findViewById(R.id.souxiangjiageDown);
souxiangzongDown = (TextView) findViewById(R.id.souxiangzongDown);
souxiangGeShi = (ImageView) findViewById(R.id.souxiangGeShi);
souxiangGridView = (MyGridView) findViewById(R.id.souxiangGridView);
String json = getIntent().getStringExtra("json");//获取值
String keywords = getIntent().getStringExtra("keywords");
souxiangkeywords.setText(keywords);//给搜索框添加值
//数据的解析,,,,显示
Gson gson = new Gson();
final MyGouWuBean myGouWuBean = gson.fromJson(json, MyGouWuBean.class);
List
list.addAll(data);
//默认以listview格式展示商品数据
setAdapter();
View view = View.inflate(SouSuoShow.this, R.layout.souxiangshanglajiazai, null);
souxiangListView.addFooterView(view);
//让listview与gridview失去焦点,从而达到scrollview默认展示顶部的效果
souxiangListView.setFocusable(false);
souxiangGridView.setFocusable(false);
//上拉加载数据
souxiangScrollView.setScrollViewListener(new ScrollViewListener() {
@Override
public void onScrollChanged(MyScrollView context, int x, int y, int oldx, int oldy) {
// Toast.makeText(MySouSuoShow.this,"正在加载。。。",Toast.LENGTH_SHORT).show();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
List
list.addAll(data);
if(z%2==1){
setBiaoGeAdapter();
}else{
setAdapter();
}
}
});
//点击综合排序,,,弹出popu框,,,并选择传值,,,加载popu视图是不能写在点击事件里面
final View view1 = View.inflate(SouSuoShow.this, R.layout.souxiangzonghe, null);
final PopupWindow popupWindow = new PopupWindow(view1, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
souxiangzong.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
souxiangzong.setTextColor(Color.RED);
souxiangzongDown.setTextColor(Color.RED);
souxiangxiao.setTextColor(Color.BLACK);
souxiangjiage.setTextColor(Color.BLACK);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.showAsDropDown(souxiangzong, 0, 0);
}
});
//点击其他地方也可以关闭popu框
view1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popupWindow.dismiss();
}
});
setPopuWindowClick(view1,popupWindow);//点击popu框传值
//点击销量排序,,奇数倒序,,偶数正序
souxiangxiao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
i++;
souxiangzong.setTextColor(Color.BLACK);
souxiangzongDown.setTextColor(Color.BLACK);
souxiangxiao.setTextColor(Color.RED);
souxiangjiage.setTextColor(Color.BLACK);
if(i%2==1){
Collections.sort(list, new Comparator
@Override
public int compare(MyGouWuBean.DataBean dataBean, MyGouWuBean.DataBean t1) {
//sort判断,,根据x轴定律,,小数在左,大数在右
if(dataBean.getSalenum()>t1.getSalenum()){
return -1;
}else if(dataBean.getSalenum()
}else{
return 0;
}
}
});
}else{
Collections.sort(list, new Comparator
@Override
public int compare(MyGouWuBean.DataBean dataBean, MyGouWuBean.DataBean t1) {
if(dataBean.getSalenum()>t1.getSalenum()){
return 1;
}else if(dataBean.getSalenum()
}else{
return 0;
}
}
});
}
if(z%2==1){
setBiaoGeAdapter();
}else{
setAdapter();
}
}
});
//点击价格排序,,,默认正序,,奇数倒序,,偶数正序
souxiangjiage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
j++;
souxiangzong.setTextColor(Color.BLACK);
souxiangzongDown.setTextColor(Color.BLACK);
souxiangxiao.setTextColor(Color.BLACK);
souxiangjiage.setTextColor(Color.RED);
if(j%2==1){
souxiangjiageDown.setTextColor(Color.RED);
souxiangjiageUp.setTextColor(Color.BLACK);
Collections.sort(list, new Comparator
@Override
public int compare(MyGouWuBean.DataBean dataBean, MyGouWuBean.DataBean t1) {
if(dataBean.getPrice()>t1.getPrice()){
return -1;
}else if(dataBean.getPrice()
}else{
return 0;
}
}
});
}else{
souxiangjiageDown.setTextColor(Color.BLACK);
souxiangjiageUp.setTextColor(Color.RED);
Collections.sort(list, new Comparator
@Override
public int compare(MyGouWuBean.DataBean dataBean, MyGouWuBean.DataBean t1) {
if(dataBean.getPrice()>t1.getPrice()){
return 1;
}else if(dataBean.getPrice()
}else{
return 0;
}
}
});
}
if(z%2==1){
setBiaoGeAdapter();
}else{
setAdapter();
}
}
});
//筛选点击,,,打开侧拉
souxiangshuai.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
souxiangDrawerLayout.openDrawer(souxiangcebian);
}
});
//关闭侧拉
cebianrg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
RadioButton cebianshuai = (RadioButton) findViewById(R.id.cebianshuai);
if(i==R.id.cebianshuai){
souxiangDrawerLayout.closeDrawer(souxiangcebian);
cebianshuai.setChecked(false);
}
}
});
//搜索框的点击事件
souxiangsousuo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(SouSuoShow.this, MainActivity.class);
startActivity(intent);
}
});
//"<"按钮的点击事件
/*souxiangfan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MySouSuoShow.this, ShouYe.class);
startActivity(intent);
}
});*/
//切换布局格式,,奇数表格,,偶数列表
souxiangGeShi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
z++;
if(z%2==1){
souxiangGeShi.setImageResource(R.drawable.liebiao);
souxiangGridView.setVisibility(View.VISIBLE);
souxiangListView.setVisibility(View.GONE);
setBiaoGeAdapter();
}else{
souxiangGeShi.setImageResource(R.drawable.biaoge);
souxiangGridView.setVisibility(View.GONE);
souxiangListView.setVisibility(View.VISIBLE);
setAdapter();
}
}
});
souxiangListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> adapterView, View view, int i, long l) {
MyGouWuBean.DataBean dataBean = list.get(i);
Intent intent = new Intent(SouSuoShow.this, XiangActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("dataBean",dataBean);
intent.putExtras(bundle);
startActivity(intent);
}
});
souxiangGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> adapterView, View view, int i, long l) {
MyGouWuBean.DataBean dataBean = list.get(i);
Intent intent = new Intent(SouSuoShow.this, XiangActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("dataBean",dataBean);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
//popuwindow框内部的操作
private void setPopuWindowClick(final View view1, final PopupWindow popupWindow) {
RelativeLayout souxiangpopu1 = view1.findViewById(R.id.souxiangpopu1);
RelativeLayout souxiangpopu2 = view1.findViewById(R.id.souxiangpopu2);
RelativeLayout souxiangpopu3 = view1.findViewById(R.id.souxiangpopu3);
final TextView zongpai = view1.findViewById(R.id.zongpai);
final TextView zonggou = view1.findViewById(R.id.zonggou);
final TextView xinpinpai = view1.findViewById(R.id.xinpinpai);
final TextView xinpingou = view1.findViewById(R.id.xinpingou);
final TextView pingpai = view1.findViewById(R.id.pingpai);
final TextView pinggou = view1.findViewById(R.id.pinggou);
souxiangpopu1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
souxiangzong.setText("综合");
zongpai.setTextColor(Color.RED);
zonggou.setVisibility(View.VISIBLE);
xinpinpai.setTextColor(Color.BLACK);
xinpingou.setVisibility(View.INVISIBLE);
pingpai.setTextColor(Color.BLACK);
pinggou.setVisibility(View.INVISIBLE);
popupWindow.dismiss();
Toast.makeText(SouSuoShow.this,"按照综合排序!",Toast.LENGTH_SHORT).show();
}
});
souxiangpopu2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
souxiangzong.setText("新品");
zongpai.setTextColor(Color.BLACK);
zonggou.setVisibility(View.INVISIBLE);
xinpinpai.setTextColor(Color.RED);
xinpingou.setVisibility(View.VISIBLE);
pingpai.setTextColor(Color.BLACK);
pinggou.setVisibility(View.INVISIBLE);
popupWindow.dismiss();
Toast.makeText(SouSuoShow.this,"按照新品排序!",Toast.LENGTH_SHORT).show();
}
});
souxiangpopu3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
souxiangzong.setText("评论");
zongpai.setTextColor(Color.BLACK);
zonggou.setVisibility(View.INVISIBLE);
xinpinpai.setTextColor(Color.BLACK);
xinpingou.setVisibility(View.INVISIBLE);
pingpai.setTextColor(Color.RED);
pinggou.setVisibility(View.VISIBLE);
popupWindow.dismiss();
Toast.makeText(SouSuoShow.this,"按照评论排序!",Toast.LENGTH_SHORT).show();
}
});
}
//刷新适配器
public void setAdapter(){
if(mySouxiangLieAdapter==null){
mySouxiangLieAdapter = new MySouxiangLieAdapter(SouSuoShow.this, list);
souxiangListView.setAdapter(mySouxiangLieAdapter);
}else{
mySouxiangLieAdapter.notifyDataSetChanged();
}
}
public void setBiaoGeAdapter(){
if(mySouxiangBiaoAdapter==null){
mySouxiangBiaoAdapter = new MySouxiangBiaoAdapter(SouSuoShow.this, list);
souxiangGridView.setAdapter(mySouxiangBiaoAdapter);
}else{
mySouxiangBiaoAdapter.notifyDataSetChanged();
}
}
@Override
protected void onStop() {
super.onStop();
finish();
}
}
适配器的操作
public class MyReSouAdapter extends RecyclerView.Adapter
Context context;
String[] mNames;
public DianjiJieKou dianjie;
public void setDianjie(DianjiJieKou dianjie) {
this.dianjie = dianjie;
}
public MyReSouAdapter(Context context, String[] mNames) {
this.context = context;
this.mNames = mNames;
}
@Override
public ReSouHolde onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.resouliushihengbuju, parent, false);
ReSouHolde reSouHolde = new ReSouHolde(view);
return reSouHolde;
}
@Override
public void onBindViewHolder(ReSouHolde holder, final int position) {
holder.resouname.setText(mNames[position]);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dianjie.onItemClike(position);
}
});
}
@Override
public int getItemCount() {
return mNames.length;
}
}
class ReSouHolde extends RecyclerView.ViewHolder{
public TextView resouname;
public ReSouHolde(View itemView) {
super(itemView);
resouname = itemView.findViewById(R.id.resouname);
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
public class MySouSuoLvAdapter extends BaseAdapter {
Context context;
ArrayList
public MySouSuoLvAdapter(Context context, ArrayList
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view==null){
view=View.inflate(context, R.layout.sousuolishibuju,null);
}
TextView sousuolishiname = view.findViewById(R.id.sousuolishiname);
sousuolishiname.setText(list.get(i));
return view;
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public class MySouxiangBiaoAdapter extends BaseAdapter{
Context context;
ArrayList
public MySouxiangBiaoAdapter(Context context, ArrayList
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(view==null){
view=View.inflate(context, R.layout.souxiangbiaogebuju,null);
}
ImageView souxiangBiaoimg = view.findViewById(R.id.souxiangBiaoimg);
TextView souxiangBiaoTitle = view.findViewById(R.id.souxiangBiaoTitle);
TextView souxiangBiaoSelNum = view.findViewById(R.id.souxiangBiaoSelNum);
TextView souxiangBiaoprice = view.findViewById(R.id.souxiangBiaoprice);
String[] split = list.get(i).getImages().split("\\|");
ImageLoader.getInstance().displayImage(split[0],souxiangBiaoimg, ImageLoaderUtils.getcrioption());
souxiangBiaoTitle.setText(list.get(i).getTitle().substring(0,20)+"....");
souxiangBiaoSelNum.setText("销量:"+list.get(i).getSalenum());
souxiangBiaoprice.setText("¥"+list.get(i).getPrice());
return view;
}
}
++++++++++++++++++++++++++++++++++++++
public class MySouxiangLieAdapter extends BaseAdapter {
Context context;
List
public MySouxiangLieAdapter(Context context, List
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(view==null){
view = View.inflate(context, R.layout.souxiangliebuju, null);
}
ImageView souxianglieimg = view.findViewById(R.id.souxianglieimg);
TextView souxianglietitle = view.findViewById(R.id.souxianglietitle);
TextView souxianglieSelNum = view.findViewById(R.id.souxianglieSelNum);
TextView souxianglieprice = view.findViewById(R.id.souxianglieprice);
String[] split = list.get(i).getImages().split("\\|");
ImageLoader.getInstance().displayImage(split[0],souxianglieimg, ImageLoaderUtils.getcrioption());
souxianglietitle.setText(list.get(i).getTitle().substring(0,20)+"....");
souxianglieSelNum.setText("销量:"+list.get(i).getSalenum());
souxianglieprice.setText("¥"+list.get(i).getPrice());
return view;
}
}
View层下的IView接口
public interface DianjiJieKou {
void onItemClike(int i);
}
public interface MyJieKou {
void onChengGong(String json);
void onShiBai(String ss);
}
public interface ScrollViewListener {
void onScrollChanged(MyScrollView context, int x, int y, int oldx, int oldy);
}
++++++++++++++++++++++++++++++++++++++++++
API接口
public class APIuil {
public static String shouye="https://www.zhaoapi.cn/ad/getAd?source=android";
//http://120.27.23.105/product/searchProducts?keywords=德龙咖啡机&source=android
//德龙咖啡机
//搜索
public static String sousuo(String s){
String ss="http://120.27.23.105/product/searchProducts?keywords="+s+"&source=android";
return ss;
}
//商品数据
public static String gouwu(int page){
String gouwu="https://www.zhaoapi.cn/product/getProducts?pscid=2&page="+page+"&source=android";
return gouwu;
}
//注册
public static String zhuceJieKou(String mobile,String pass){
String zhuce="https://www.zhaoapi.cn/user/reg?mobile="+mobile+"&password="+pass+"&source=android";
return zhuce;
}
//登录
public static String dengluJieKou(String mobile,String pass){
String denglu="https://www.zhaoapi.cn/user/login?mobile="+mobile+"&password="+pass+"&source=android";
return denglu;
}
//查看个人信息
public static String getRen(String uid){
String geren="https://www.zhaoapi.cn/user/getUserInfo?uid="+uid+"&source=android";
return geren;
}
//商品详情 https://www.zhaoapi.cn/product/getProductDetail?pid=34
public static String shangping(String pid){
String ss="https://www.zhaoapi.cn/product/getProductDetail?pid="+pid+"&source=android";
return ss;
}
//添加购物车
public static String addCar(String uid, int pid){
String addCarJiekou="https://www.zhaoapi.cn/product/addCart?uid="+uid+"&pid="+pid+"&source=android";
return addCarJiekou;
}
//查询车 https://www.zhaoapi.cn/product/getCarts
public static String getCar(String uid){
String addCarJiekou="http://120.27.23.105/product/getCarts?Uid="+uid+"&source=android";
return addCarJiekou;
}
//删除数据 http://120.27.23.105/product/deleteCart?uid=72&pid=1
public static String deleteCar(String uid,String pid){
String ss="http://120.27.23.105/product/deleteCart?uid="+uid+"&pid="+pid+"&source=android";
return ss;
}
/*//更新购物车
public static String updataCart(String uid,MyGouBean.DataBean.ListBean l){
String ss="https://www.zhaoapi.cn/product/updateCarts?uid="+uid+"&sellerid="+l.getSellerid()+"&pid="+l.getPid()+"&selected="+l.getSelected()+"&num="+l.getNum()+"&source=android";
return ss;
}*/
//分类左侧列表
public static String fenlei="http://120.27.23.105/product/getCatagory";
//分类布局...右侧..商品子分类接口
public static String zifenlei(String cid){
String zifenlei="http://120.27.23.105/product/getProductCatagory?cid="+cid+"&source=android";
return zifenlei;
}
//上传文件
public static String chuanFile(String file){
String chuan="https://www.zhaoapi.cn/file/upload?uid=3470&file="+file+"&source=android";
return chuan;
}
}
present层
public class SouPresenter {
private final SouModel souModel;
public SouPresenter() {
souModel = new SouModel();
}
public void getContent(String url, final MyJieKou jie){
souModel.getContentMessge(url, new MyJieKou() {
@Override
public void onChengGong(String json) {
jie.onChengGong(json);
}
@Override
public void onShiBai(String ss) {
jie.onShiBai(ss);
}
});
}
}
moudle层
public class SouModel {
public void getContentMessge(String url, final MyJieKou jie){
OkHttpClient okHttpClient = new OkHttpClient();
Request build = new Request.Builder().url(url).build();
Call call = okHttpClient.newCall(build);
okHttpClient.newCall(build).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
jie.onShiBai("请求失败!");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String ss=response.body().string();
Log.i("jiba","======购物"+ss);
jie.onChengGong(ss);
}
});
}
}
++++++++++++++++++++++++++++++++++++
Dao包
public class MySousuoDao {
private final SQLiteDatabase db;
public MySousuoDao(SQLiteDatabase db) {
this.db=db;
db.execSQL("create table if not exists sousuo(id Integer primary key,name varchar(50))");
}
public void addShuJu(String name){
db.execSQL("insert into sousuo(name) values('"+name+"')");
}
public boolean chaLiShi(String name){
Cursor cursor = db.rawQuery("select *from sousuo where name='" + name + "'", null);
boolean b = cursor.moveToNext();
return b;
}
public ArrayList
ArrayList
Cursor cursor = db.rawQuery("select *from sousuo", null);
while (cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
list.add(name);
}
return list;
}
public void qingLishi(){
db.execSQL("delete from sousuo");
}
}
一些帮助类
public class FlowLayout extends ViewGroup {
//存储所有子View
private List> mAllChildViews = new ArrayList<>();
//每一行的高度
private List
public FlowLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//父控件传进来的宽度和高度以及对应的测量模式
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 b, int z, int i1, int i2, int i3) {
mAllChildViews.clear();
mLineHeight.clear();
//获取当前ViewGroup的宽度
int width = getWidth();
int lineWidth = 0;
int lineHeight = 0;
//记录当前行的view
List
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);
}
}
++++++++++++++++++++++++++++++++++++++++++
list和grild是一样的
public class MyGridView extends GridView {
public MyGridView(Context context) {
super(context);
}
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
++++++++++++++++++++++++++++++++++++++++++++++++
public class MyScrollView extends ScrollView {
private ScrollViewListener scrollViewListener;
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setScrollViewListener(ScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
//scrollview的起始点+总高度==crollView的computeVerticalScrollRange
if(getScrollY() + getHeight() == computeVerticalScrollRange())
{
Log.d("jiaba","------滚动到最下方------");
if(scrollViewListener!=null){
scrollViewListener.onScrollChanged(this, l, t, oldl, oldt);
}
}
}
}
还有一些布局
resouliushihengbuju
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:text="sdfgf"
android:textColor="#FFF"
android:background="@drawable/flowtextview"
android:id="@+id/resouname"
android:layout_margin="10dp"
/>
============================================
sousuolishibuju
android:layout_height="match_parent">
android:layout_height="wrap_content"
android:text="sadfgh"
android:textSize="20dp"
android:id="@+id/sousuolishiname"
/>
=================================================
souxiangbiaogebuju
android:orientation="vertical"
android:layout_height="wrap_content"
>
android:layout_height="200dp"
android:id="@+id/souxiangBiaoimg"
/>
android:layout_height="wrap_content"
android:id="@+id/souxiangBiaoTitle"
android:textSize="20dp"
android:textColor="#000"
android:text="asdfasd"
android:layout_marginTop="5dp"
/>
android:layout_height="wrap_content"
android:id="@+id/souxiangBiaoSelNum"
android:textSize="20dp"
android:textColor="#000"
android:text="asdfasd"
android:layout_marginTop="5dp"
/>
android:layout_height="wrap_content"
android:textColor="#F89"
android:text="1sdf"
android:textSize="20dp"
android:id="@+id/souxiangBiaoprice"
android:layout_marginTop="5dp"
/>
=============================================
souxiangliebuju
android:layout_height="wrap_content">
android:layout_height="120dp"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:id="@+id/souxianglieimg"/>
android:layout_height="120dp"
android:orientation="vertical"
android:layout_marginLeft="15dp"
android:layout_gravity="center"
>
android:layout_height="wrap_content"
android:text="空间的查看次机会"
android:layout_marginTop="8dp"
android:textSize="20dp"
android:textColor="#000"
android:id="@+id/souxianglietitle"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="90"
android:layout_marginTop="28dp"
android:textSize="20dp"
android:textColor="#000"
/>
android:layout_height="wrap_content"
android:text="15250.00"
android:textColor="#F00"
android:textSize="20dp"
android:layout_marginTop="5dp"
android:id="@+id/souxianglieprice"/>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
souxiangshanglajiazai
android:layout_height="30dp"
android:orientation="horizontal"
android:gravity="center_horizontal" >
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正在载入...."
android:textSize="20dp"
/>
=========================================================
souxiangzonghe
android:orientation="vertical"
android:background="#33000000"
android:layout_height="130dp">
android:layout_height="40dp"
android:id="@+id/souxiangpopu1"
android:background="#FFF">
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="综合排序"
android:gravity="center"
android:textSize="20dp"
android:layout_marginLeft="20dp"
android:textColor="#F00"/>
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="✔"
android:textSize="20dp"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:textColor="#F00"/>
android:layout_height="40dp"
android:id="@+id/souxiangpopu2"
android:background="#FFF">
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="新品优先"
android:gravity="center"
android:textSize="20dp"
android:layout_marginLeft="20dp"
android:textColor="#000"/>
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="✔"
android:textSize="20dp"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:textColor="#F00"
android:visibility="invisible"/>
android:layout_height="40dp"
android:id="@+id/souxiangpopu3"
android:background="#FFF">
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="评论数从高到低"
android:gravity="center"
android:textSize="20dp"
android:layout_marginLeft="20dp"
android:textColor="#000"/>
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="✔"
android:textSize="20dp"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:textColor="#f00"
android:visibility="invisible"/>
drawable中的属性
cebiandianji
flowtextview
android:right="5dp"
android:top="5dp"
android:bottom="5dp"
/>
search_bg
>
sousuokuang
android:bottom="5px"/>
souxiangkuang