GridLayout

//布局

    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_b"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="lipengfei.yuekao.BActivity">
            android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#3F51B5">
   

            android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Empty View,Pull Down/Up to Add items"/>
            android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="2"
        android:horizontalSpacing="20dp"
        android:columnWidth="240dp"
        android:gravity="center_horizontal"
        >
   
//适配器
package lipengfei.yuekao.Adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import org.xutils.image.ImageOptions;
import org.xutils.x;
import java.util.ArrayList;
import lipengfei.yuekao.Bean.BeanFood;
import lipengfei.yuekao.R;
/**
 * Created by Administrator on 2017/11/23.
 */
public class MyGridViewAdapter extends BaseAdapter{
    private Context context;
    private ArrayList arrays;
    public MyGridViewAdapter(Context context, ArrayList arrays) {
        this.context = context;
        this.arrays = arrays;
    }
    @Override
    public int getCount() {
        return arrays.size();
    }
    @Override
    public Object getItem(int i) {
        return arrays.get(i);
    }
    @Override
    public long getItemId(int i) {
        return i;
    }
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        MyGridViewAdapter.Viewhander vh;
        if (view==null){
            view= LayoutInflater.from(context).inflate(R.layout.item2_layout,null);
            vh=new Viewhander(view);
            view.setTag(vh);
        }else{
            vh= (MyGridViewAdapter.Viewhander) view.getTag();
        }
        vh.tv_title.setText(arrays.get(i).getTitle());
        vh.tv_subtitle.setText(arrays.get(i).getFood_str());
        ImageOptions options = new ImageOptions.Builder()
                .setCircular(true)
    .build();
        x.image().bind(vh.img,arrays.get(i).getPic(),options);
        return view;
    }
    class Viewhander{
        private ImageView img;
        private TextView tv_title;
        private TextView tv_subtitle;
        public Viewhander(View view) {
            img=view.findViewById(R.id.item2_img);
            tv_title=view.findViewById(R.id.item2_tv_title);
            tv_subtitle=view.findViewById(R.id.item2_tv_subtitle);
        }
    }
}
//Activity
package lipengfei.yuekao;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import com.google.gson.Gson;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;
import java.util.ArrayList;
import lipengfei.yuekao.Adapter.MyGridViewAdapter;
import lipengfei.yuekao.Bean.BeanFood;
public class BActivity extends AppCompatActivity implements AbsListView.OnScrollListener, AdapterView.OnItemClickListener {
    //需要加载的网址
    private String path="http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=";
    private Toolbar toolbar;
    private GridView grid;
    private ArrayList arrays;
    private MyGridViewAdapter adapter;
    private int index;
    private boolean isFlag;
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);
        //实例化控件
        initView();
    }
    private void initView() {
        //实例化toolbar并设置属性
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("菜单二");
        toolbar.setSubtitle("好菜好吃好美味!");
        toolbar.inflateMenu(R.menu.aa);
        grid = (GridView) findViewById(R.id.grid);
        tv = (TextView) findViewById(R.id.tv);
        //实例化数据源
        arrays=new ArrayList<>();
        adapter=new MyGridViewAdapter(this,arrays);
        grid.setAdapter(adapter);
        //加载网路数据的方法
        LoadData(path);
        grid.setOnScrollListener(this);
        //GridView的点击事件
        grid.setOnItemClickListener(this);
    }
    private void LoadData(String path) {
        index++;
        RequestParams requestParams = new RequestParams(path+index);
        x.http().get(requestParams, new Callback.CommonCallback() {
            @Override//加载成功后调用的方法
            public void onSuccess(String s) {
                //数据加载成功后隐藏
                tv.setVisibility(View.GONE);
                Gson gson=new Gson();
                BeanFood beanFood = gson.fromJson(s, BeanFood.class);
                arrays.addAll(beanFood.getData());
                adapter.notifyDataSetChanged();
            }
            @Override
            public void onError(Throwable throwable, boolean b) {
            }
            @Override
            public void onCancelled(CancelledException e) {
            }
            @Override
            public void onFinished() {
            }
        });
    }
    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) {
        if (isFlag&&i==0){
            LoadData(path);
        }
    }
    @Override
    public void onScroll(AbsListView absListView, int i, int i1, int i2) {
        if (i+i1==i2){
            isFlag=true;
        }else{
            isFlag=false;
        }
    }
    @Override
    public void onItemClick(AdapterView adapterView, View view, int i, long l) {
        Intent intent = new Intent(this,Main2Activity.class);
        intent.putExtra("data",arrays.get(i));
        startActivity(intent);
    }
}

你可能感兴趣的:(GridLayout)