XListView+多条目

代码段

String urlStirng = "http://api.expoon.com/AppNews/getNewsList/type/1/p/";
XListView xListView;
ArrayList<Person.DataBean> list = new ArrayList<Person.DataBean>();
int page=1;
MxAdapetr mxAdapetr;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View mView = inflater.inflate(R.layout.frag_item01, container, false);
    xListView = mView.findViewById(R.id.xlistView);
    //加载数据
    indata(0);
	mxAdapetr = new MxAdapetr(getActivity(), list);
    xListView.setAdapter(mxAdapetr);
    
    //支持加载更多数据
    xListView.setPullLoadEnable(true);
    
    //xlistView监听
    
    xListView.setXListViewListener(new XListView.IXListViewListener() {
        @Override
        public void onRefresh() {//向下拉刷新
            //清除旧数据
            list.clear();

            //加载新数据
            indata(0);
        }
        //上拉加载更多数据
        @Override
        public void onLoadMore() {
            page++;
            indata(page);
        }
    });

    return mView;
}
   private void indata(int page) {

    String urlData = urlStirng + page;
	//异步加载图片
    new MAsyncTsk().execute(urlData);
}	
class MAsyncTsk extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... strings) {
        return NetData.getNetjson(strings[0]);
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        Gson gson = new Gson();
        Person person = gson.fromJson(s, Person.class);
        List<Person.DataBean> data = person.getData();
        
        //将数据存入大集合
        list.addAll(data);
      
        //刷新条目
        mxAdapetr.notifyDataSetChanged();
		

        ClearHeard();

    }
}
private void ClearHeard() {

    //下拉刷新时显示的字体
    xListView.setRefreshTime("正在刷新……");
    xListView.stopRefresh();
    xListView.stopLoadMore();
}

适配器

public class MxAdapetr extends BaseAdapter {

Context context;
ArrayList<Person.DataBean> list =
        new ArrayList<Person.DataBean>();

public MxAdapetr(Context context, ArrayList<Person.DataBean> list) {

    this.context = context;
    this.list = list;
}

@Override
public int getItemViewType(int position) {
//返回条目类型,我只里面只有两种类型所以简写了,可以根据需求判断返回的条目类型
    return position % 2;
}

@Override
public int getViewTypeCount() {
//条目类型的个数
    return 2;
}

@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) {
	//获取返回的类型
   // int i = getItemViewType(position);
   
   //判断类型加载不同条目
    switch (getItemViewType(position)){
        case 0:
            ViewHolder1 holder=null;
            //Log.e("zmz","============");
            if (convertView == null) {
                holder = new ViewHolder1();
                convertView = View.inflate(context,R.layout.item,null);
                holder.textView = convertView.findViewById(R.id.textview);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder1) convertView.getTag();
            }
            holder.textView.setText(list.get(position).getNews_title());
            break;
        case 1:

            ViewHolder2 holder2 = null;
            if (convertView == null) {
               // Log.e("zmz","============1");
                holder2 = new ViewHolder2();
                convertView = View.inflate(context,R.layout.xlv_item,null);
                holder2.textView = convertView.findViewById(R.id.textview);
                holder2.imageView = convertView.findViewById(R.id.xlv_image);
                
                convertView.setTag(holder2);
            }else {
                holder2 = (ViewHolder2) convertView.getTag();
            }

            holder2.textView.setText(list.get(position).getNews_title());
			//使用Imageloader加载图片
           	ImageLoader.getInstance().displayImage(list.get(position).getPic_url(),holder2.imageView);
            
            break;
    }
    return convertView;
}

class ViewHolder1 {
    TextView textView;
}
class ViewHolder2{
    TextView textView;
    ImageView imageView;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值