Android组件之MainActivity实例,mainactivity

6
回复
592
查看
[复制链接]

443

主题

1194

帖子

1959

安币

手工艺人

发表于 2018-7-10 15:53:04 | 显示全部楼层 |阅读模式

        

        Android组件之MainActivity实例

[Java] 查看源文件 复制代码
//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(&quot;头条&quot;,true));
    channelBeans.add(new ChannelBean(&quot;娱乐&quot;,true));
    channelBeans.add(new ChannelBean(&quot;重庆&quot;,true));
    channelBeans.add(new ChannelBean(&quot;杭州&quot;,true));
    channelBeans.add(new ChannelBean(&quot;河北&quot;,true));
    channelBeans.add(new ChannelBean(&quot;八卦&quot;,false));
    channelBeans.add(new ChannelBean(&quot;北京&quot;,false));
    channelBeans.add(new ChannelBean(&quot;科技&quot;,false));
    channelBeans.add(new ChannelBean(&quot;经贸&quot;,false));
    channelBeans.add(new ChannelBean(&quot;安徽&quot;,false));
    for(int i =0;i<channelBeans.size();i++){
        TextView textView = new TextView(this);
        textView.setText(channelBeans.get(i).getName()+&quot;&quot;);
        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=&quot;https://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=15&amp;page=&quot;;
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(&quot;title&quot;,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(&quot;title&quot;);
    TextView textView = new TextView(getActivity());
    textView.setText(title);
    imgs.add(&quot;https://img5.imgtn.bdimg.com/it/u=357293603,2414818182&amp;fm=27&amp;gp=0.jpg&quot;);
    imgs.add(&quot;https://www.cctv.com/program/dhc/20050207/images/100815_b2.jpg&quot;);
    imgs.add(&quot;https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3227877552,1311244730&amp;fm=27&amp;gp=0.jpg&quot;);

    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(&quot;下拉刷新&quot;);
    loadingLayout.setReleaseLabel(&quot;正在刷新&quot;);
    loadingLayout.setReleaseLabel(&quot;放开刷新&quot;);
    ILoadingLayout layout = pull.getLoadingLayoutProxy(false,true);
    layout.setPullLabel(&quot;上拉加载&quot;);
    layout.setRefreshingLabel(&quot;正在加载&quot;);
    layout.setRefreshingLabel(&quot;放开加载&quot;);
    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 = &quot;&quot;; 
     @Override
     protected String doInBackground(String... strings) {

         try {
             URL url = new URL(strings[0]);
             HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
             urlConnection.setRequestMethod(&quot;GET&quot;);
             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 = &quot;&quot;;
    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;

                                               

7

主题

9624

帖子

1968

安币

Android大神

Rank: 6Rank: 6

发表于 2018-7-10 16:29:11 | 显示全部楼层
感谢分享,安卓巴士有你更精彩:lol

4

主题

9713

帖子

794

安币

代码手工艺人

Rank: 4

发表于 2018-7-10 17:50:05 | 显示全部楼层
支持,感谢,祝巴士越来越好~

758

主题

1467

帖子

1870

安币

手工艺人

发表于 2018-7-10 20:00:09 | 显示全部楼层
帮帮顶顶!!

0

主题

9518

帖子

2470

安币

Android大神

Rank: 6Rank: 6

发表于 2018-7-10 21:48:53 | 显示全部楼层
每次我都积极回帖的,想要安币~

465

主题

9958

帖子

809

安币

代码手工艺人

Rank: 4

发表于 2018-7-10 23:01:45 | 显示全部楼层
安卓巴士是个不错的网站,我来顶个贴~

0

主题

9441

帖子

2412

安币

Android大神

Rank: 6Rank: 6

发表于 2018-7-11 00:35:55 | 显示全部楼层
楼主威武,以后多发干货,多办活动~!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

领先的中文移动开发者社区
18620764416
7*24全天服务
意见反馈:1294855032@qq.com

扫一扫关注我们

Powered by Discuz! X3.2© 2001-2019 Comsenz Inc.( 粤ICP备15117877号 )