package com.example.jdmall;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements
        AdapterView.OnItemClickListener, AbsListView.OnScrollListener {
    private ListView peopleListView;
    private JDAdapter peopleAdapter;
    private String[] peoples={"美瓷陶瓷刀","茶花调味盒","玉泉套碗","美浓餐具","希诺水瓶","小刘砧","张小泉刀具"};
    private String[] telnumber={"180元","181元","182元","183元","184元","185元","186元"};
    private int[] peoImgs={R.mipmap.p1,R.mipmap.p2,R.mipmap.p3,R.mipmap.p4,R.mipmap.p5,R.mipmap.p6,R.mipmap.p7};
    private List<JDMechandises> persons=new ArrayList<JDMechandises>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        peopleListView = findViewById(R.id.spListView);
        initDataPersons();
        peopleAdapter = new JDAdapter(persons,MainActivity.this);
        peopleListView.setAdapter(peopleAdapter);
        peopleListView.setOnItemClickListener(this);
        peopleListView.setOnScrollListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        JDMechandises person=persons.get(i);
        Toast.makeText(MainActivity.this,"点击了第"+i+"个商品，名字是："+person.getName(),Toast.LENGTH_LONG).show();
    }
    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) {
        if(i==SCROLL_STATE_FLING){
            Toast.makeText(MainActivity.this,"列表靠惯性滑动",Toast.LENGTH_LONG).show();
        }else if(i==SCROLL_STATE_IDLE){
            Toast.makeText(MainActivity.this,"停止滑动",Toast.LENGTH_LONG).show();
            JDMechandises person=new JDMechandises("江蛤悠悠球",R.mipmap.p1,"189元");
            persons.add(person);
            peopleAdapter.notifyDataSetChanged();
        }
        if(i==SCROLL_STATE_TOUCH_SCROLL){
            Toast.makeText(MainActivity.this,"手指离开了屏幕，列表在滑动",Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onScroll(AbsListView absListView, int i, int i1, int i2) {

    }

    private void initDataPersons(){
        for(int i=0;i<peoples.length;i++){
            JDMechandises person=new JDMechandises(peoples[i],peoImgs[i],telnumber[i]);
            persons.add(person);
        }
    }
}