//Activity布局
<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"    android:layout_height="match_parent" tools:context="com.bwie.skn.conan_lianxi00.MainActivity">    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="match_parent">    <LinearLayout        android:id="@+id/ll"        android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        >        <Button            android:id="@+id/b1"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="wrap_content"            android:text="首页"            android:onClick="b1"            android:textSize="18dp"            />        <Button            android:id="@+id/b2"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="wrap_content"            android:text="行情"            android:onClick="b2"            android:textSize="18dp"            />        <Button            android:id="@+id/b3"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="wrap_content"            android:text="资讯"            android:onClick="b3"            android:textSize="18dp"            />    LinearLayout>    <FrameLayout        android:id="@+id/fl"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_above="@id/ll"        >FrameLayout>    RelativeLayout>    <LinearLayout        android:layout_width="300dp"        android:layout_height="match_parent"        android:orientation="vertical"        android:layout_gravity="left"        android:background="#5cbbf4"        >        <ImageView            android:layout_width="150dp"            android:layout_height="150dp"            android:src="@drawable/qwe"            android:layout_gravity="center_horizontal"            />        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:gravity="center"            android:textSize="30dp"            android:text=""            />        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:gravity="center"            android:textSize="30dp"            android:text=""            />        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:gravity="center"            android:textSize="30dp"            android:text=""            />        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:gravity="center"            android:textSize="30dp"            android:text=""            />        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:gravity="center"            android:textSize="30dp"            android:text=""            />    LinearLayout>

android.support.v4.widget.DrawerLayout>

//Activity代码

package com.bwie.skn.conan_lianxi00;import android.graphics.Color;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.FrameLayout;import com.bwie.skn.conan_lianxi00.feagment.Fragment1;import com.bwie.skn.conan_lianxi00.feagment.Fragment2;import com.bwie.skn.conan_lianxi00.feagment.Fragment3;public class MainActivity extends AppCompatActivity {    private FrameLayout fl;    private Button b1,b2,b3;    private Fragment1 fragment1;    private Fragment2 fragment2;    private Fragment3 fragment3;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        initfragment();    }    private void initView() {        b1 = (Button) findViewById(R.id.b1);        b2 = (Button) findViewById(R.id.b2);        b3 = (Button) findViewById(R.id.b3);        b1.setTextColor(Color.RED);    }    private void initfragment() {        fragment1 = new Fragment1();        fragment2 = new Fragment2();        fragment3 = new Fragment3();        getSupportFragmentManager().beginTransaction().add(R.id.fl,fragment1).commit();        getSupportFragmentManager().beginTransaction().add(R.id.fl,fragment2).commit();        getSupportFragmentManager().beginTransaction().add(R.id.fl,fragment3).commit();        getSupportFragmentManager().beginTransaction().hide(fragment1).commit();        getSupportFragmentManager().beginTransaction().hide(fragment2).commit();        getSupportFragmentManager().beginTransaction().hide(fragment3).commit();        getSupportFragmentManager().beginTransaction().show(fragment1).commit();    }    public void b1(View view){        b1.setTextColor(Color.RED);        b2.setTextColor(Color.BLACK);        b3.setTextColor(Color.BLACK);        getSupportFragmentManager().beginTransaction().show(fragment1).commit();        getSupportFragmentManager().beginTransaction().hide(fragment2).commit();        getSupportFragmentManager().beginTransaction().hide(fragment3).commit();    }    public void b2(View view){        b1.setTextColor(Color.BLACK);        b2.setTextColor(Color.RED);        b3.setTextColor(Color.BLACK);        getSupportFragmentManager().beginTransaction().hide(fragment1).commit();        getSupportFragmentManager().beginTransaction().show(fragment2).commit();        getSupportFragmentManager().beginTransaction().hide(fragment3).commit();    }    public  void b3(View view){        b1.setTextColor(Color.BLACK);        b2.setTextColor(Color.BLACK);        b3.setTextColor(Color.RED);        getSupportFragmentManager().beginTransaction().hide(fragment1).commit();        getSupportFragmentManager().beginTransaction().hide(fragment2).commit();        getSupportFragmentManager().beginTransaction().show(fragment3).commit();    }}

//Fragment布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    >    <HorizontalScrollView        android:id="@+id/hsv"        android:layout_width="match_parent"        android:layout_height="40dp">        <LinearLayout            android:id="@+id/hll"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal"            >        LinearLayout>    HorizontalScrollView>    <android.support.v4.view.ViewPager        android:id="@+id/hvp"        android:layout_width="match_parent"        android:layout_height="match_parent">android.support.v4.view.ViewPager>LinearLayout>
   
   
//Fragment代码
package com.bwie.skn.conan_lianxi00.feagment;import android.graphics.Color;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentPagerAdapter;import android.support.v4.view.ViewPager;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.HorizontalScrollView;import android.widget.LinearLayout;import android.widget.TextView;import com.bwie.skn.conan_lianxi00.R;import java.util.ArrayList;import java.util.List;/** * author:Created by WangZhiQiang on 2017/10/11. */public class Fragment3 extends Fragment{    private View view;    private HorizontalScrollView hsv;    private LinearLayout hll;    private ViewPager hvp;    private List<TextView> textlist = new ArrayList<>();    final String[] titles = new String[]{"数据新闻","快讯","头条","精编广告","美股","港股","基金","理财"};    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        view = inflater.inflate(R.layout.fragment3,container,false);        return view;    }    @Override    public void onActivityCreated(@Nullable Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        initView();    }    private void initView() {        hsv =(HorizontalScrollView) view.findViewById(R.id.hsv);        hll =(LinearLayout) view.findViewById(R.id.hll);        hvp =(ViewPager) view.findViewById(R.id.hvp);        for(int i=0;i<titles.length;i++){            TextView textView = new TextView(getActivity());            textView.setText(titles[i]);            textView.setTextSize(18);            textView.setWidth(150);            textView.setId(i+1000);            textView.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View view) {                    int id = view.getId();                    hvp.setCurrentItem(id-1000);                }            });            if(i==0){                textView.setTextColor(Color.RED);            }else{                textView.setTextColor(Color.BLACK);            }            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);            layoutParams.setMargins(20,10,20,10);            //添加到布局            hll.addView(textView,layoutParams);            //添加到集合            textlist.add(textView);        }        hvp.setAdapter(new MAdapter(getActivity().getSupportFragmentManager()));        hvp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {            @Override            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {                for (int i=0;i<textlist.size();i++){                    if(position==i){                        textlist.get(i).setTextColor(Color.RED);                    }else{                        textlist.get(i).setTextColor(Color.BLACK);                    }                }                TextView textView = textlist.get(position);                int i = textView.getWidth()+10;                hsv.scrollTo(i*position,0);            }            @Override            public void onPageSelected(int position) {            }            @Override            public void onPageScrollStateChanged(int state) {            }        });    }    class MAdapter extends FragmentPagerAdapter{        public MAdapter(FragmentManager fm) {            super(fm);        }        @Override        public Fragment getItem(int position) {            //http://mnews.gw.com.cn/wap/data/news/分类标识/page_页数.json            String[] title = new String[]{"xbsjxw","txs","toutiao","news/mobile/jbgg","news/mobile/mgxw","news/mobile/ggxw","news/mobile/jjxw","news/mobile/lcxw"};            return Fragment_zx.getinstans("http://mnews.gw.com.cn/wap/data/news/"+title[position]+"/page_");        }        @Override        public int getCount() {            return textlist.size();        }    }}


//viewpager——Fragment布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <com.bawei.wzq.wzlibrary.view.XListView        android:id="@+id/xlv"        android:layout_width="match_parent"        android:layout_height="match_parent"        >com.bawei.wzq.wzlibrary.view.XListView>LinearLayout>


//viewpaget_Fragment代码

package com.bwie.skn.conan_lianxi00.feagment;import android.os.AsyncTask;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;import com.bawei.wzq.wzlibrary.view.XListView;import com.bwie.skn.conan_lianxi00.R;import com.bwie.skn.conan_lianxi00.bean.LvBean;import com.nostra13.universalimageloader.core.DisplayImageOptions;import com.nostra13.universalimageloader.core.ImageLoader;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.util.ArrayList;import java.util.List;/** * author:Created by WangZhiQiang on 2017/10/11. *///http://news.xinhuanet.com/politics/2017-09/30/1121752088_15067520678551n.jpgpublic class Fragment_zx extends Fragment implements XListView.IXListViewListener{    View view;    private XListView xlv;    private List<LvBean> list = new ArrayList<>();    private MAdapter adapter;    private String title;    private int n=1;    private DisplayImageOptions options;    public static Fragment_zx getinstans(String title){        Fragment_zx fragment_zx = new Fragment_zx();        Bundle bundle = new Bundle();        bundle.putString("title",title);        fragment_zx.setArguments(bundle);        return fragment_zx;    }    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        view = inflater.inflate(R.layout.fragment_zx,container,false);        return view;    }    @Override    public void onActivityCreated(@Nullable Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        //sd        options = new DisplayImageOptions.Builder()                .cacheOnDisk(true)   //sd                .cacheInMemory(true)                .build();        //接收路径        Bundle arguments = getArguments();        title = arguments.getString("title");        //初始化控件        xlv =(XListView) view.findViewById(R.id.xlv);        //必须写        xlv.setPullLoadEnable(true);        xlv.setXListViewListener(this);        Log.e("Conan", title +"1.json");        adapter = new MAdapter();        xlv.setAdapter(adapter);        new MAsyncTask().execute(title +"1.json");    }    //网络获取    class MAsyncTask extends AsyncTask<String,Void,String>{        private StringBuilder builder;        @Override        protected String doInBackground(String... strings) {            try {                //获取url                URL url = new URL(strings[0]);                //请求网络                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();                int code = urlConnection.getResponseCode();                //判断是否返回成功                if(code==200){                    //获取网络信息                    InputStream inputStream = urlConnection.getInputStream();                    BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));                    builder = new StringBuilder();                    String s = "";                    //拼接                    while((s=bf.readLine())!=null){                        builder.append(s);                    }                }            } catch (Exception e) {                e.printStackTrace();            }            return builder.toString();        }        @Override        protected void onPostExecute(String s) {            super.onPostExecute(s);            Log.e("Conan",s+"+-+-+-+-+-+-+-+-ss");            try {                JSONArray array = new JSONArray(s);                JSONObject object = array.optJSONObject(0);                JSONArray array2 = object.optJSONArray("data");                for(int i=0;i<array2.length();i++){                    String titleJson = array2.optJSONObject(i).optString("title");                    String summaryJson = array2.optJSONObject(i).optString("summary");                    String imgJson = array2.optJSONObject(i).optString("img");                    if (imgJson.equals("")){                        list.add(new LvBean(titleJson,summaryJson,"http://news.xinhuanet.com/politics/2017-09/30/1121752088_15067520678551n.jpg"));                    }else{                        list.add(new LvBean(titleJson,summaryJson,imgJson));                    }                }                adapter.notifyDataSetChanged();            } catch (JSONException e) {                e.printStackTrace();            }        }    }    //适配器    class MAdapter extends BaseAdapter{        @Override        public int getCount() {            return list.size();        }        @Override        public Object getItem(int i) {            return list.get(i);        }        @Override        public long getItemId(int i) {            return i;        }        class ViewHodler{            TextView title;            TextView summary;            ImageView img;        }        @Override        public View getView(int i, View view, ViewGroup viewGroup) {            int type = getItemViewType(i);                switch(type){                        case 0:                            ViewHodler vh;                            if (view==null){                                view = View.inflate(getActivity(),R.layout.lv_item1_layout,null);                                vh = new ViewHodler();                                vh.title =(TextView) view.findViewById(R.id.title);                                vh.summary =(TextView) view.findViewById(R.id.summary);                                vh.img = (ImageView) view.findViewById(R.id.img);                                view.setTag(vh);                            }else{                                vh = (ViewHodler) view.getTag();                            }                            vh.title.setText(list.get(i).getTitle());                            vh.summary.setText(list.get(i).getSummary());                            ImageLoader.getInstance().displayImage(list.get(i).getImg(),vh.img,options);                        break;                        case 1:                            ViewHodler vh1;                            if (view==null){                                view = View.inflate(getActivity(),R.layout.lv_item2_layout,null);                                vh1 = new ViewHodler();                                vh1.title =(TextView) view.findViewById(R.id.title);                                vh1.summary =(TextView) view.findViewById(R.id.summary);                                vh1.img = (ImageView) view.findViewById(R.id.img);                                view.setTag(vh1);                            }else{                                vh1 = (ViewHodler) view.getTag();                            }                            vh1.title.setText(list.get(i).getTitle());                            vh1.summary.setText(list.get(i).getSummary());                            ImageLoader.getInstance().displayImage(list.get(i).getImg(),vh1.img,options);                        break;                    }                    return view;        }        @Override        public int getItemViewType(int position) {            if (position%2==0){                return 0;            }else{                return 1;            }        }        @Override        public int getViewTypeCount() {            return 2;        }    }    public void onLoad(){        xlv.stopRefresh();        xlv.stopLoadMore();        xlv.setRefreshTime("刚刚");    }    //下拉刷新    @Override    public void onRefresh() {        list.clear();        new MAsyncTask().execute(title+"1.json");        onLoad();    }    @Override    public void onLoadMore() {        n++;        new MAsyncTask().execute(title+n+".json");        onLoad();    }}

更多相关文章

  1. Android WebView文件上传(关键代码)
  2. android,java代码设置背景色
  3. 【Android UI】Android开发之View的几种布局方式及实践
  4. Android 开发:(四)Android五大布局及属性详解
  5. 移动应用开发:如何创建自定义Android代码模板
  6. Android 用代码动态添加View(ViewGroup.addView()),并进行动态添加
  7. 关于android中网络图片下载中oom解决开源框架Afinal的探究
  8. Android应用程序进程启动过程的源代码分析(1)

随机推荐

  1. 使用ListView应该注意的地方
  2. Android子线程与子线程的通信
  3. Android实现电话状态监控
  4. android 播放视频保存的一些网页
  5. android manifest.xml中元素含义android
  6. android默认字体大小、高度、宽度
  7. windowSoftInputMode属性设置值
  8. android权限
  9. Android(安卓)获取当前设备的IP地址
  10. android画统计图的chart engine