XUtils请求数据
public class MyFragment extends Fragment implements XListView.IXListViewListener { private XListView xListView; private List<InfoBean.ResultBean.DataBean> list; private MyBase base; private boolean flag; private int index; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.myfragment,container,false); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); View view = getView(); xListView = (XListView) view.findViewById(R.id.XLV); xListView.setPullLoadEnable(true); xListView.setXListViewListener(this); data(); } public void data(){ RequestParams params = new RequestParams("http://v.juhe.cn/toutiao/index"); params.addQueryStringParameter("key","5b6258c74f4346147b12fe38490a12b2"); params.addQueryStringParameter("type","top"); x.http().post(params, new Callback.CommonCallback<String>() { @Override public void onSuccess(String result) { Gson gson = new Gson(); InfoBean infoBean = gson.fromJson(result, InfoBean.class); list = infoBean.getResult().getData(); if (base==null){ base = new MyBase(list,getActivity(),xListView); xListView.setAdapter(base); }else { base.add(list,flag); base.notifyDataSetChanged(); } } @Override public void onError(Throwable ex, boolean isOnCallback) { } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } }); } @Override public void onRefresh() { ++index; data(); flag=true; xListView.stopRefresh(true); } @Override public void onLoadMore() { ++index; data(); flag = false; xListView.stopLoadMore(); } }适配器
public class MyBase extends BaseAdapter implements AdapterView.OnItemClickListener { ImageOptions options = new ImageOptions.Builder() .setLoadingDrawableId(R.mipmap.ic_launcher) .setUseMemCache(true) .setSize(200,200) .build(); private List<InfoBean.ResultBean.DataBean> list; private Context context; private XListView xListView; public MyBase(List<InfoBean.ResultBean.DataBean> list, Context context, XListView xListView) { this.list = list; this.context = context; this.xListView = xListView; } public void add(List<InfoBean.ResultBean.DataBean> list1, Boolean flag){ for (InfoBean.ResultBean.DataBean bean: list1) { if (flag){ list.add(0,bean); } list.add(bean); } } @Override public int getCount() { return list!=null?list.size():0; } @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) { ViewHolder holder; if (convertView==null){ holder = new ViewHolder(); convertView = convertView.inflate(context,R.layout.mybase,null); holder.text = (TextView) convertView.findViewById(R.id.text_view); holder.image = (ImageView) convertView.findViewById(R.id.image_view); convertView.setTag(holder); }else { holder = (ViewHolder) convertView.getTag(); } holder.text.setText(list.get(position).getTitle()); x.image().bind(holder.image,list.get(position).getThumbnail_pic_s(),options); xListView.setOnItemClickListener(this); return convertView; } Xlistview点击item传图片实现步骤 @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { ArrayList<String> list2 = new ArrayList<String>(); list2.add(list.get(position-1).getThumbnail_pic_s()); list2.add(list.get(position-1).getThumbnail_pic_s02()); list2.add(list.get(position-1).getThumbnail_pic_s03()); Intent intent = new Intent(context,PicActivity.class); intent.putStringArrayListExtra("key",list2); context.startActivity(intent); } class ViewHolder{ private TextView text; private ImageView image; } } 另一个activity接收传过来的值
public class PicActivity extends AppCompatActivity { private HackyViewPager pv; private ArrayList<String> list; private ArrayList<View> arrayList = new ArrayList<View>(); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.picactivity); pv = (HackyViewPager) findViewById(R.id.HVP); Intent intent = getIntent(); list = intent.getStringArrayListExtra("key"); ImageOptions options = new ImageOptions.Builder() .setLoadingDrawableId(R.mipmap.ic_launcher) .setUseMemCache(true) .setSize(200,200) .build(); PhotoView photoView1 = new PhotoView(PicActivity.this); x.image().bind(photoView1, list.get(0),options); PhotoView photoView2 = new PhotoView(PicActivity.this); x.image().bind(photoView2, list.get(1),options); PhotoView photoView3 = new PhotoView(PicActivity.this); x.image().bind(photoView3, list.get(2),options); arrayList.add(photoView1); arrayList.add(photoView2); arrayList.add(photoView3); MyAdapter adapter = new MyAdapter(); pv.setAdapter(adapter); } 这个activity的适配器 class MyAdapter extends PagerAdapter{ @Override public int getCount() { return arrayList!=null?list.size():0; } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(arrayList.get(position)); return arrayList.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(arrayList.get(position)); } } } 这个activity的布局
xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <cn.bluemobi.dylan.photoview.HackyViewPager android:id="@+id/HVP" android:layout_width="300dp" android:layout_height="200dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="135dp"> cn.bluemobi.dylan.photoview.HackyViewPager> RelativeLayout>