布局和简单的适配器

来源:互联网 发布:外星人源码论坛eenot 编辑:程序博客网 时间:2024/06/07 19:41

main

<com.jcodecraeer.xrecyclerview.XRecyclerView    android:id="@+id/xrecyclerview"    android:layout_width="match_parent"    android:layout_height="match_parent"></com.jcodecraeer.xrecyclerview.XRecyclerView>


条目的xml


<ImageView    android:id="@+id/image_01"    android:layout_width="100dp"    android:layout_height="100dp"    android:layout_gravity="center_horizontal"    android:background="@mipmap/ic_launcher" /><TextView    android:id="@+id/text_title"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_gravity="center_horizontal"    android:layout_marginTop="20dp"    android:text="555"    android:textColor="#800080"    android:textSize="20sp" /><TextView    android:id="@+id/text_price"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_gravity="center_horizontal"    android:layout_marginTop="20dp"    android:text="555"    android:textColor="#ff0000"    android:textSize="20sp" />
adater
public class ShopAdater extends RecyclerView.Adapter<ShopAdater.viewholder> {    Context context;    ShopBean shopBean;    public ShopAdater(Context context, ShopBean shopBean) {        this.context = context;        this.shopBean = shopBean;        ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context));    }    @Override    public viewholder onCreateViewHolder(ViewGroup parent, int viewType) {        View view = View.inflate(context, R.layout.item_01_layout, null);        return new viewholder(view);    }    @Override    public void onBindViewHolder(viewholder holder, final int position) {        final String[] image = shopBean.getData().get(position).getImages().split("\\|");        ImageLoader.getInstance().displayImage(image[0], holder.image_01);        holder.text_price.setText("+shopBean.getData().get(position).getPrice());        holder.text_title.setText(shopBean.getData().get(position).getSubhead());        //获取pid        final String pid= shopBean.getData().get(position).getPid()+"";        final String text_price=shopBean.getData().get(position).getSalenum()+"";        final String text_title=shopBean.getData().get(position).getSubhead();        holder.image_01.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                clicklistener.dianjiclick(view,position,pid,text_price,text_title,image[0]);            }        });    }    @Override    public int getItemCount() {        return shopBean.getData() == null ? 0 : shopBean.getData().size();    }    class viewholder extends RecyclerView.ViewHolder {        private ImageView image_01;        private TextView text_title;        private TextView text_price;        public viewholder(View itemView) {            super(itemView);            image_01 = (ImageView) itemView.findViewById(R.id.image_01);            text_title = (TextView) itemView.findViewById(R.id.text_title);            text_price = (TextView) itemView.findViewById(R.id.text_price);        }    }    Clicklistener clicklistener;    public void setclick(Clicklistener clicklistener) {        this.clicklistener = clicklistener;    }    public interface Clicklistener {        public void dianjiclick(View view,int position,String pid,String text_title,String text_price,String image);    }}

main文件
public class MainActivity extends AppCompatActivity implements MyShopview {    private XRecyclerView xrecyclerview;    private Myshoppresenter myshoppresenter;    private ShopAdater shopAdater;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //控件        xrecyclerview = (XRecyclerView) findViewById(R.id.xrecyclerview);        myshoppresenter = new Myshoppresenter(this, this);        myshoppresenter.jsondata();    }    @Override    public void showview01(ShopBean shopBean) {        //设置适配器        shopAdater = new ShopAdater(MainActivity.this, shopBean);        xrecyclerview.setAdapter(shopAdater);        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);        xrecyclerview.setLayoutManager(gridLayoutManager);        //接口回调监听事件        shopAdater.setclick(new ShopAdater.Clicklistener() {            @Override            public void dianjiclick(View view, int position, String pid, String text_title, String text_price, String image) {                //传值                Intent intent = new Intent(MainActivity.this, AddActivity.class);                intent.putExtra("text_title", text_title);                intent.putExtra("text_price", text_price);                intent.putExtra("pid", pid);                intent.putExtra("pid", pid);                intent.putExtra("image", image);                startActivity(intent);            }        });    }    @Override    protected void onDestroy() {        super.onDestroy();        myshoppresenter.death();    }}

加入的activity
public class AddActivity extends AppCompatActivity implements MyAddview, View.OnClickListener {    Addbean addbean;    private Myaddpresenter myaddpresenter;    private ImageView mView01Image;    private TextView mTitleText;    private TextView mPriceText;    private Button mAddBtn;    private Button mBuyBtn;    private String pid;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_add);        initView();        //接收数据        Intent intent = getIntent();        String name = intent.getStringExtra("text_title");        String price = intent.getStringExtra("text_price");        pid = intent.getStringExtra("pid");        String image = intent.getStringExtra("image");        //设置数据        mTitleText.setText(name);        mPriceText.setText("人民币:" + price);        //设置图片        Glide.with(AddActivity.this).load(image).into(mView01Image);    }    //控件    private void initView() {        mView01Image = (ImageView) findViewById(R.id.image_view_01);        mTitleText = (TextView) findViewById(R.id.text_title);        mPriceText = (TextView) findViewById(R.id.text_price);        mAddBtn = (Button) findViewById(R.id.btn_add);        mAddBtn.setOnClickListener(this);        mBuyBtn = (Button) findViewById(R.id.btn_buy);        mBuyBtn.setOnClickListener(this);    }    @Override    public void showview02(Addbean addbean) {        //跳转页面        Toast.makeText(this, addbean.getMsg(), Toast.LENGTH_SHORT).show();        if (addbean.getMsg().equals("加购成功") ) {            startActivity(new Intent(AddActivity.this, ShopActivity.class));       }    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.btn_add:                //点击加入购物车                myaddpresenter = new Myaddpresenter(this, this);                myaddpresenter.jsondata(pid);                break;            case R.id.btn_buy:                Toast.makeText(this, "购买成功", Toast.LENGTH_SHORT).show();                break;            default:                break;        }    }}