/** * 展示商品的适配器 */
import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.example.myapp.R; import com.example.myapp.ShopBean.BiaoBean; import com.nostra13.universalimageloader.core.ImageLoader; public class BiaoAdapter extends RecyclerView.Adapter<BiaoAdapter.MyHolder> { OnItemClickListener listener; public void setOnItemClickListener(OnItemClickListener listener){ this.listener=listener; } private Context context; private BiaoBean biaobean; public BiaoAdapter(Context context, BiaoBean biaobean) { this.context = context; this.biaobean = biaobean; } @Override public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.shop_item, null); MyHolder holder = new MyHolder(view); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { listener.onItemClick((int) view.getTag()); } }); return holder; } @Override public void onBindViewHolder(MyHolder holder, int position) { holder.names.setText(biaobean.getDatas().getGoods_list().get(position).getGoods_name()); holder.names.setText(biaobean.getDatas().getGoods_list().get(position).getGoods_price()); ImageLoader.getInstance().displayImage(biaobean.getDatas().getGoods_list().get(position).getGoods_image_url(),holder.images); holder.itemView.setTag(position); } @Override public int getItemCount() { return biaobean.getDatas().getGoods_list().size(); } class MyHolder extends RecyclerView.ViewHolder { final TextView names; final TextView prices; final ImageView images; public MyHolder(View itemView) { super(itemView); names = (TextView) itemView.findViewById(R.id.names); prices = (TextView) itemView.findViewById(R.id.prices); images=(ImageView) itemView.findViewById(R.id.images); } } public interface OnItemClickListener{ public void onItemClick(int position); } }
/** * 展示商品并点击跳转到详情页面 */
import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.Parcelable; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.widget.EditText; import android.widget.Toast; import com.example.myapp.R; import com.example.myapp.ShopBean.Bean; import com.example.myapp.ShopBean.BiaoBean; import com.example.myapp.SpacesItemDecoration; import com.example.myapp.TheAdapter.BiaoAdapter; import com.example.myapp.TheAdapter.MyAdapter; import com.google.gson.Gson; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.Serializable; import java.util.List; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class Shangpin extends AppCompatActivity { private RecyclerView shopRy; private BiaoBean biaobean; private BiaoAdapter biaoAdapter; private List<BiaoBean.DatasBean.GoodsListBean> goods_list; private Handler hand = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); String s = (String) msg.obj; Gson gson=new Gson(); biaobean=gson.fromJson(s,BiaoBean.class); goods_list=biaobean.getDatas().getGoods_list(); biaoAdapter=new BiaoAdapter(Shangpin.this,biaobean); shopRy.setAdapter(biaoAdapter); biaoAdapter.setOnItemClickListener(