主xml
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<com.handmark.pulltorefresh.library.PullToRefreshScrollView
android:id="@+id/pull_to_refresh_scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="240dp">
<android.support.v4.view.ViewPager
android:id="@+id/goods_viewpager"
android:layout_width="match_parent"
android:layout_height="240dp"></android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/ll_points"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:background="#0f0"
android:gravity="center"
android:orientation="horizontal"></LinearLayout>
</RelativeLayout>
<com.example.day_0524.MyListView
android:id="@+id/goods_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"></com.example.day_0524.MyListView>
</LinearLayout>
</com.handmark.pulltorefresh.library.PullToRefreshScrollView>
</LinearLayout>
子布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/goods_pic"
android:layout_width="80dp"
android:layout_height="80dp"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/goods_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:text="标题"
android:textSize="20sp" />
<TextView
android:id="@+id/goods_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="价格"
android:textColor="#FF0000" />
</LinearLayout>
</LinearLayout>
java 代码
public class GoodsActivity extends AppCompatActivity {
private PullToRefreshScrollView scrollView;
private ViewPager viewPager;
private List<ImageView> ads = new ArrayList<>();//轮播图集合
private List<ImageView> ps = new ArrayList<>();//小圆点集合
List<GoodsBean.DataBean> list = new ArrayList<>();//商品集合
private MyAdapter myAdapter;
private int page = 1;//页数
private GoodsAdapter goodsAdapter;
private LinearLayout ll_points;
private MyHandler myHandler = new MyHandler();
private MyListView listView;
private List<AdBean.DataBean> data;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_goods);
initView();
goodsAdapter = new GoodsAdapter(list, GoodsActivity.this);
listView.setAdapter(goodsAdapter);
getDataFromNet();
}
懒加载 放到Fragment用这个, 平时Activity不用这个
@Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if (isVisibleToUser){ getDataFromNet(); } }
private void initView() {
scrollView = findViewById(R.id.pull_to_refresh_scrollview);
viewPager = findViewById(R.id.goods_viewpager);
ll_points = findViewById(R.id.ll_points);
listView = findViewById(R.id.goods_listview);
//刷新
scrollView.setMode(PullToRefreshBase.Mode.BOTH);
scrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
//下拉
page = 1;
getGoodsListFromNet();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
//上啦
page++;
getGoodsListFromNet();
}
});
//Viewpager 点击事件
viewPager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
myHandler.removeCallbacksAndMessages(null);
break;
case MotionEvent.ACTION_UP:
myHandler.sendEmptyMessageDelayed(0, 1000);
break;
case MotionEvent.ACTION_MOVE:
myHandler.removeCallbacksAndMessages(null);
break;
}
return false;
}
});
}
//设置原点
class MyHandler extends Handler{
@Override
public void handleMessage(Message msg) {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
//设置小圆点
for (int i = 0; i < ps.size(); i++) {
if (i == viewPager.getCurrentItem() % ps.size()) {
ps.get(i).setSelected(true);
} else {
ps.get(i).setSelected(false);
}
}
myHandler.sendEmptyMessageDelayed(0, 1000);
}
}
//轮播图数据
private void getDataFromNet(){
HttpUtils httpUtils = HttpUtils.getInstance();
httpUtils.get(HttpConfig.AD_URL);
httpUtils.setHttpUtilsListener(new HttpUtils.HttpUtilListener() {
@Override
public void getSuccess(String json) {
Gson gson = new Gson();
AdBean bean = gson.fromJson(json, AdBean.class);
data = bean.getData();
for (int i = 0; i< data.size(); i++){
String icon = data.get(i).getIcon();
ImageView imageView = new ImageView(GoodsActivity.this);
//缩放
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
ImageLoader.getInstance().displayImage(icon, imageView, MyApp.getOptions());
ads.add(imageView);
//点击吐司
final int finalI = i;
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(GoodsActivity.this, "--" + data.get(finalI),Toast.LENGTH_LONG ).show();
}
});
//添加小圆点
ImageView p = new ImageView(GoodsActivity.this);
p.setImageResource(R.drawable.ad_selector);
ps.add(p);
//设置小圆点间距
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(15, 0, 0, 0);
p.setLayoutParams(lp);
//将小圆点加入的布局里面
ll_points.addView(p, i);
}
//默认第一个选中
ps.get(0).setSelected(true);
//设置适配器
//轮播图适配器
myAdapter = new MyAdapter(ads);
viewPager.setAdapter(myAdapter);
//开始自动轮播
myHandler.sendEmptyMessageDelayed(0, 1000);
//请求商品列表
getGoodsListFromNet();
}
@Override
public void getError(String error) {
}
});
}
//获取商品数据
private void getGoodsListFromNet(){
HttpUtils httpUtils = HttpUtils.getInstance();
httpUtils.get(HttpConfig.GOODS_URL+page);
httpUtils.setHttpUtilsListener(new HttpUtils.HttpUtilListener() {
@Override
public void getSuccess(String json) {
Gson gson = new Gson();
GoodsBean goodsBean = gson.fromJson(json, GoodsBean.class);
List<GoodsBean.DataBean> data = goodsBean.getData();
if(page==1){
list.clear();
}
if(data.size()==0){
Toast.makeText(GoodsActivity.this,"没有数据了",Toast.LENGTH_SHORT).show();
}
list.addAll(data);
goodsAdapter.notifyDataSetChanged();
scrollView.onRefreshComplete();
}
@Override
public void getError(String error) {
}
});
}
}
适配器
public class GoodsAdapter extends BaseAdapter {
private List<GoodsBean.DataBean> list;
private Context context;
public GoodsAdapter(List<GoodsBean.DataBean> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
convertView= View.inflate(context, R.layout.goods_item,null);
holder=new ViewHolder();
holder.price= convertView.findViewById(R.id.goods_price);
holder.title= convertView.findViewById(R.id.goods_title);
holder.imageView= convertView.findViewById(R.id.goods_pic);
convertView.setTag(holder);
}else{
holder= (ViewHolder) convertView.getTag();
}
String images = list.get(position).getImages();
//截取url地址
String pic_url = images.split("\\|")[0];
ImageLoader.getInstance().displayImage(pic_url,holder.imageView, MyApp.getOptions());
holder.title.setText(list.get(position).getTitle());
holder.price.setText("价格"+list.get(position).getPrice());
return convertView;
}
class ViewHolder{
ImageView imageView;
TextView title;
TextView price;
}
}
MyList
public class MyListView extends ListView {
public MyListView(Context context) {
super(context);
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heigtSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heigtSpec);
}
}