欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android组件之MainActivity实例

程序员文章站 2022-08-20 15:54:14
android之mainactivity实例 //mainactivity private horizontalscrollview hs; private viewpager view...

android之mainactivity实例

//mainactivity

private horizontalscrollview hs;
private viewpager viewpager;
private button button;
private linearlayout layout;
list<textview> list;
private arraylist<channelbean> channelbeans;
@override
protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    initview();
    initdata();
}

private void initdata() {
    list = new arraylist<>();
    channelbeans = new arraylist<>();
    channelbeans.add(new channelbean("头条",true));
    channelbeans.add(new channelbean("娱乐",true));
    channelbeans.add(new channelbean("重庆",true));
    channelbeans.add(new channelbean("杭州",true));
    channelbeans.add(new channelbean("河北",true));
    channelbeans.add(new channelbean("八卦",false));
    channelbeans.add(new channelbean("北京",false));
    channelbeans.add(new channelbean("科技",false));
    channelbeans.add(new channelbean("经贸",false));
    channelbeans.add(new channelbean("安徽",false));
    for(int i =0;i<channelbeans.size();i++){
        textview textview = new textview(this);
        textview.settext(channelbeans.get(i).getname()+"");
        textview.settextsize(15);
        textview.setid(i+1000);
        textview.setonclicklistener(new view.onclicklistener() {
            @override
            public void onclick(view v) {
                int  id = v.getid();
                viewpager.setcurrentitem(id-1000);
            }
        });
        if(i==0){
            textview.settextcolor(color.red);
        }else{
            textview.settextcolor(color.black);
        }
        linearlayout.layoutparams layoutparams = new linearlayout.layoutparams(viewpager.layoutparams.wrap_content,viewpager.layoutparams.wrap_content);
        layoutparams.setmargins(40,10,40,10);
        layout.addview(textview,layoutparams);
        list.add(textview);
    }
    viewpager.setadapter(new myadapter(getsupportfragmentmanager()));
    viewpager.setonpagechangelistener(new viewpager.onpagechangelistener() {
        @override
        public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) {
            for(int i=0;i<list.size();i++){
                if(position==i) {
                    list.get(i).settextcolor(color.red);
                } else {
                    list.get(i).settextcolor(color.black);
                }
            }
            textview textview = list.get(position);
            int width = textview.getwidth()+10;
            hs.scrollto(width*position,0);
        }

        @override
        public void onpageselected(int position) {

        }

        @override
        public void onpagescrollstatechanged(int state) {

        }
    });
    button.setonclicklistener(new view.onclicklistener() {
        @override
        public void onclick(view v) {
            channelactivity.startchannelactivity(mainactivity.this,channelbeans);
        }
    });

}
private void initview() {
    hs = findviewbyid(r.id.hs);
    layout = findviewbyid(r.id.linear);
    viewpager = findviewbyid(r.id.viewpager);
    button = findviewbyid(r.id.btn);
}

private class myadapter extends fragmentpageradapter {
    public myadapter(fragmentmanager supportfragmentmanager) {
        super(supportfragmentmanager);
    }

    @override
    public fragment getitem(int position) {
        return fragment01.getinstance(list.get(position).gettext().tostring());
    }

    @override
    public int getcount() {
        return list.size();
    }
}

//fragmeng
private view view;
private string path="https://www.yulin520.com/a2a/impressapi/news/mergelist?pagesize=15&page=";
private int page = 1;
private pulltorefreshlistview pull;
private xbanner banner;
private mybase base;
private httputils httputils = httputils.getinstance();
private list<bean.databean> list = new arraylist<>();
private list<string> imgs = new arraylist<>();
private handler handler = new handler();
public static fragment01 getinstance(string title) {
    fragment01 fragment01 = new fragment01();
    bundle bundle = new bundle();
    bundle.putstring("title",title);
    fragment01.setarguments(bundle);
   return fragment01;
}

@nullable
@override
public view oncreateview(@nonnull layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) {
    view = inflater.inflate(r.layout.fragment01,container,false);
    banner = view.findviewbyid(r.id.banner);
    bundle a = getarguments();
    string title = a.getstring("title");
    textview textview = new textview(getactivity());
    textview.settext(title);
    imgs.add("https://img5.imgtn.bdimg.com/it/u=357293603,2414818182&fm=27&gp=0.jpg");
    imgs.add("https://www.cctv.com/program/dhc/20050207/images/100815_b2.jpg");
    imgs.add("https://ss0.bdstatic.com/70cfuhsh_q1ynxgkpowk1hf6hhy/it/u=3227877552,1311244730&fm=27&gp=0.jpg");

    banner.setdata(imgs,null);
    banner.setmadapter(new xbanner.xbanneradapter() {
        @override
        public void loadbanner(xbanner banner, view view, int position) {
            glide.with(getactivity()).load(imgs.get(position)).into((imageview)view);
        }
    });
    initview();
    initdata();
    return view;
}

private void initview() {
    pull = view.findviewbyid(r.id.pull);
    pull.setmode(pulltorefreshbase.mode.both);
    iloadinglayout loadinglayout = pull.getloadinglayoutproxy(true,false);
    loadinglayout.setpulllabel("下拉刷新");
    loadinglayout.setreleaselabel("正在刷新");
    loadinglayout.setreleaselabel("放开刷新");
    iloadinglayout layout = pull.getloadinglayoutproxy(false,true);
    layout.setpulllabel("上拉加载");
    layout.setrefreshinglabel("正在加载");
    layout.setrefreshinglabel("放开加载");
    pull.setonrefreshlistener(new pulltorefreshbase.onrefreshlistener2<listview>() {
        @override
        public void onpulldowntorefresh(pulltorefreshbase<listview> pulltorefreshbase) {
            page = 1;
            initdata();
            handler.postdelayed(new runnable() {
                @override
                public void run() {
                    pull.onrefreshcomplete();
                }
            },2000);
        }
        @override
        public void onpulluptorefresh(pulltorefreshbase<listview> pulltorefreshbase) {
            page = page+1;
            initdata();
            handler.postdelayed(new runnable() {
                @override
                public void run() {
                    pull.onrefreshcomplete();
                }
            },2000);
        }
    });
    base = new mybase(getactivity(),list);
    pull.setadapter(base);

}
private void initdata() {
    string url = path+page;
    httputils.getdata(url);
    httputils.sethttplicener(new httputils.httplicener() {
        @override
        public void getjsondata(string json) {
            gson gson = new gson();
            bean bean = gson.fromjson(json, bean.class);
            list<bean.databean> data = bean.getdata();
            if(page==1){
                list.clear();
            }
            list.addall(data);
            base.notifydatasetchanged();

        }
    });

}
//mybase
private context context;
private list<bean.databean> list;

public mybase(context context, list<bean.databean> list) {
    this.context = context;
    this.list = list;
}

@override
public int getcount() {
    return list.size();
}

@override
public object getitem(int position) {
    return null;
}

@override
public long getitemid(int position) {
    return 0;
}

@override
public view getview(int position, view convertview, viewgroup parent) {
    viewholder holder;
    if(convertview == null){
        convertview = view.inflate(context,r.layout.mybase,null);
        holder = new viewholder();
        holder.textview = convertview.findviewbyid(r.id.text);
        holder.imageview = convertview.findviewbyid(r.id.img);
        convertview.settag(holder);
    }else{
        holder = (viewholder) convertview.gettag();
    }
    holder.textview.settext(list.get(position).gettitle());
    imageloader.getinstance().displayimage(list.get(position).getimg(),holder.imageview,myaplicin.getoptions());
    return convertview;
}
class viewholder{
    textview textview;
    imageview imageview;
}

//httputils
private httplicener httplicener;
private static httputils httputils;
public static httputils getinstance() {
    if(httputils==null){
        httputils = new httputils();
    }
    return httputils;
}
public void getdata(string url){
    myasynctask asynctask = new myasynctask();
    asynctask.execute(url);
}
 class myasynctask extends asynctask<string,void,string> {
    string json = ""; 
     @override
     protected string doinbackground(string... strings) {

         try {
             url url = new url(strings[0]);
             httpurlconnection urlconnection = (httpurlconnection) url.openconnection();
             urlconnection.setrequestmethod("get");
             urlconnection.setconnecttimeout(5000);
             if(urlconnection.getresponsecode()==200){
                 inputstream inputstream = urlconnection.getinputstream();
                 json = streamtostring(inputstream);
                 
             }
         } catch (exception e) {
             e.printstacktrace();
         }


         return json;
     }

     @override
     protected void onpostexecute(string s) {
         super.onpostexecute(s);
         httplicener.getjsondata(s);
     }
 }

private string streamtostring(inputstream inputstream) throws ioexception {
    inputstreamreader inputstreamreader = new inputstreamreader(inputstream);
    bufferedreader reader = new bufferedreader(inputstreamreader);
    string s = "";
    stringbuilder builder = new stringbuilder();
    while ((s=reader.readline())!=null){
        builder.append(s);
    }
    reader.close();
    return builder.tostring();
}

public interface httplicener {
    void getjsondata(string json);
}

public void sethttplicener(httplicener httplicener) {
    this.httplicener = httplicener;
}
//myapplice

1.imageloader.getinstance().init(config);
2.return options;