介面卡

MEIMEI_BK發表於2018-05-28

一,Fragment

package com.bwei.month_lianxi1.adapter;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

public class MyFragmentAdapter2 extends FragmentPagerAdapter{
    private List<Fragment> list;

    public MyFragmentAdapter2(FragmentManager fm, List<Fragment> list) {
        super(fm);
        this.list = list;
    }

    //獲取條目
    @Override
    public Fragment getItem(int position) {
        return list.get(position);
    }


    //獲取數量
    @Override
    public int getCount() {
        return list.size();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return list.get(position).getArguments().getString("title");
    }
}

二,

package com.bwei.month_lianxi1.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bwei.month_lianxi1.R;
import com.bwei.month_lianxi1.app.MyApp;
import com.bwei.month_lianxi1.bean.ShangPin;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class MyShangPin extends BaseAdapter{
    private Context context;
    private List<ShangPin.DataBean> list;


    public MyShangPin(Context context, List<ShangPin.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return 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.shangpin_base,null);
            holder=new ViewHolder();
            holder.imageView=convertView.findViewById(R.id.shangpin_mybase);
            holder.text1=convertView.findViewById(R.id.shangpin_mybase_text1);
            holder.text2=convertView.findViewById(R.id.shangpin_mybase_text2);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }

        //賦值
        holder.text1.setText(list.get(position).getTitle());
        holder.text2.setText(list.get(position).getPrice()+"");

        //獲取圖片地址
        String images = list.get(position).getImages();
        //擷取圖片地址
        String pin_url=images.split("\\|")[0];
        ImageLoader.getInstance().displayImage(pin_url,holder.imageView, MyApp.getOptions());
        return convertView;
    }

    class ViewHolder{
        ImageView imageView;
        TextView text1,text2;
    }
}

相關文章