/** *2017/4/24 8:42 *Created by jiangchen *作用:主activity */ public class MainActivity extends Activity { private ListView mylistview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mylistview = (ListView) findViewById(R.id.mylistview); /** *2017/4/24 8:55 *Created by jiangchen *作用:ArrayAdapter能用来《展示》简单的数据源,展示一个数组里面的内容 */ //数据源 String[] arr={"这是arrayadapter","这是arrayadapter","这是arrayadapter"}; //系统的带有简单的textview的布局android.R.layout.simple_list_item_1 mylistview.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arr)); //自己写的布局,用的是自己写的textview布局R.layout.layout,R.id.tv是布局里面textview的id mylistview.setAdapter(new ArrayAdapter<String>(this,R.layout.layout,R.id.tv,arr)); /** *2017/4/24 10:01 *Created by jiangchen *作用:SimpleAdapter能用来《展示》复杂的数据源,展示List<Map<String,?>>集合里面的数据源 * Object,表示放所有数据类型都行 */ List<Map<String,Object>> mapList=new ArrayList<>(); Map<String,Object> map=new HashMap<>(); map.put("imgId",R.mipmap.ic_launcher); map.put("text","这是SimpleAdapter"); mapList.add(map); //new String[]{"imgId","text"}表示map里面<键名>的数组 // new int[]{R.id.img,R.id.tv}表示与其对应的<控件ID> mylistview.setAdapter(new SimpleAdapter(this,mapList,R.layout.layout, new String[]{"imgId","text"},new int[]{R.id.img,R.id.tv})); /** *2017/4/27 9:38 *Created by jiangchen *作用:BaseAdapter能用来展示和操作复杂的数据,可以在adapter里面设各类事件 */ //添加数据 MyBean myBean=new MyBean(); myBean.setId(R.mipmap.ic_launcher); myBean.setText("我最帅,没得争议"); List<MyBean> myBeanList=new ArrayList<>(); myBeanList.add(myBean); //初始化适配器 MyBaseAdapter myBaseAdapter=new MyBaseAdapter(this,myBeanList); //设置适配器 mylistview.setAdapter(myBaseAdapter); } }
/** * Created by jiangchen on 2017/4/27- 9:02. * 说明: */ public class MyBaseAdapter extends BaseAdapter { Context context; //全局上下文 List<MyBean> list; //实体类集合 /** * 带参构造,要传入ScannerActivity 实例来调用带参返回跳转方法 */ public MyBaseAdapter(Context context, List<MyBean> list) { this.context = context; this.list = list; } @Override public int getCount() { //获取item数量 if (list == null) { return 0; } else { return list.size(); } } @Override public Object getItem(int position) { //获取item实例 return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; //convertView合理利用缓存,第一次加载到缓存里,后面直接从缓存取 //《这样做的好处就是减少控件实例化次数,即findViewById次数》 if (convertView == null) { viewHolder = new ViewHolder(); convertView = LayoutInflater.from(context).inflate(R.layout.layout, null); viewHolder.imageView = (ImageView) convertView.findViewById(R.id.img); viewHolder.textView = (TextView) convertView.findViewById(R.id.tv); //存入 convertView.setTag(viewHolder); } else { //取出 viewHolder = (ViewHolder) convertView.getTag(); } //最好做一个非空判断,以免炸穿 if (list.get(position)!=null){ viewHolder.textView.setText(list.get(position).getText()); viewHolder.imageView.setImageResource(list.get(position).getId()); //如果有点击事件,可以写在这里 } return convertView; } //ViewHolder,存放控件实例,用静态的只会加载一次且最先加载 private static class ViewHolder { ImageView imageView; TextView textView; } }
//这个是ViewPager配合Fragment所用的adapter 《注意要继承FragmentActivity》
public class GuidePageActivity extends FragmentActivity { private ViewPager guideVp; private List<Fragment> list; private CirclePageIndicator indicator; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_guide_page); guideVp = (ViewPager) findViewById(R.id.guide_vp); list=new ArrayList<Fragment>(); initFragment(); //设置最大缓存页数,避免数据加载重复 guideVp.setOffscreenPageLimit(4); VPAdapter vpAdapter=new VPAdapter(getSupportFragmentManager(),list); guideVp.setAdapter(vpAdapter); indicator = (CirclePageIndicator) findViewById(R.id.guide_indicator); indicator.setViewPager(guideVp); } private void initFragment(){ list.add(new GuideFragment()); list.add(new GuideFragmentTwo()); list.add(new GuideFragmentThree()); list.add(new GuideFragmentFour()); } }
/** * Created by jiangchen * need FragmentPagerAdapter ---> FragmentManager */ public class VPAdapter extends FragmentPagerAdapter { List<Fragment> list; public VPAdapter(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(); } }
//还有个adapter用的不多,直接贴俩过来,懒得注释和修改了
/** * Created by JiangChen on 2016/12/20. * Emai:334967198@qq.com 这个是直接用来加载图片的viewPagerAdapter,把网址传进去,viewpager显示图片 */ public class TuPianViewPagerAdapter extends PagerAdapter { Context ctx = null; List<String> pagerData = null; public TuPianViewPagerAdapter(Context ct , List<String> data) { ctx = ct; pagerData = data; } /** * Return the number of views available. */ @Override public int getCount() { return pagerData.size(); } /** * Create the page for the given position. The adapter is responsible * for adding the view to the container given here, although it only * must ensure this is done by the time it returns from * {@link #finishUpdate(ViewGroup)}. * * @param container The containing View in which the page will be shown. * @param position The page position to be instantiated. * @return Returns an Object representing the new page. This does not * need to be a View, but can be some other container of the page. */ @Override public Object instantiateItem(ViewGroup container, int position) { Log.i("PagerAdapter" , "method instantiateItem be calling" ); // TODO: 2016/12/20 这里要写一个item来放图片 View v = LayoutInflater.from(ctx).inflate(R.layout.common_tupian_item , null); ImageView img = (ImageView) v.findViewById(R.id.common_tupian_img);/*这里写item里面的那个imageview的id*/ Glide.with(ctx).load(pagerData.get(position)).into(img); img.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String arr[]=new String[pagerData.size()]; for (int i=0;i<pagerData.size();i++){ arr[i]=pagerData.get(i); } ctx.startActivity(new Intent(ctx,TupianShowActivity.class).putExtra("arr",arr)); } }); container.addView(v); return v; //return super.instantiateItem(container, position); } /** * Remove a page for the given position. The adapter is responsible * for removing the view from its container, although it only must ensure * this is done by the time it returns from {@link #finishUpdate(ViewGroup)}. * * @param container The containing View from which the page will be removed. * @param position The page position to be removed. * @param object The same object that was returned by * {@link #instantiateItem(View, int)}. */ @Override public void destroyItem(ViewGroup container, int position, Object object) { Log.i("PagerAdapter" , "method destroyItem be calling"); container.removeView((View)object); } /** * Determines whether a page View is associated with a specific key object * as returned by {@link #instantiateItem(ViewGroup, int)}. This method is * required for a PagerAdapter to function properly. * * @param view Page View to check for association with <code>object</code> * @param object Object to check for association with <code>view</code> * @return true if <code>view</code> is associated with the key object <code>object</code> */ @Override public boolean isViewFromObject(View view, Object object) { return view == object; } }
//还有一个BaseExpandableListAdapter是用来加载多级listview的那种adapter,也用的非常少,自行百度吧~