用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

tabLayout实现导航栏

2017-02-28 作者: xjh举报

[android]代码库

///////////////////////////////布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.bwie.xujiahao20170213.MainActivity">
    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/backgroundColor"
        app:tabIndicatorColor="@color/tabIndicatorColor"
        app:tabTextColor="@color/TextColor"
        app:tabSelectedTextColor="@color/colorAccent"
        app:tabMode="scrollable"
        ></android.support.design.widget.TabLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#ffF456"
        ></View>
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></android.support.v4.view.ViewPager>
</LinearLayout>







<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
   <ListView
       android:id="@+id/listview"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       ></ListView>
</LinearLayout>






<?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">

    <ImageView
        android:layout_width="140dp"
        android:layout_height="130dp"
        android:id="@+id/img"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/title2"
        android:text="TextView"
        android:layout_toRightOf="@+id/img"
        android:textSize="20dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/wang2"
        android:text="TextView"
        android:layout_toRightOf="@+id/img"
        android:textSize="20dp"
        android:layout_below="@+id/title2"
        android:layout_marginTop="15dp"/>
</RelativeLayout>




、、、、、、、、、、、、、主要代码MainActivity
public class MainActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private String[] title=new String[]{"头条","社会","国内","国际","娱乐","体育","军事","游戏","女性"};
    private List<Fragment> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        tabLayout = (TabLayout) findViewById(R.id.tablayout);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        initData();
        //设置适配器
        MyAdapter adapter = new MyAdapter(getSupportFragmentManager(), MainActivity.this, title);
        adapter.setList(list);
        //关联viewpager和tablayout
        viewPager.setAdapter(adapter);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        tabLayout.setupWithViewPager(viewPager);
    }
    //根据数组添加fragment界面
    private void initData() {
        list = new ArrayList<>();
        for(int i=0;i<title.length;i++){
            TitleFragment fragment = new TitleFragment();
            list.add(fragment);
        }
    }
}





public class MyApplication extends Application{
    @Override
    public void onCreate() {
        super.onCreate();
        ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this).build();
        ImageLoader.getInstance().init(configuration);
    }
}





public class MyAdapter extends FragmentPagerAdapter{
    private List<Fragment> list;
    private Context context;
    private String[] title;
    public MyAdapter(FragmentManager fm,Context context,String[] title) {
        super(fm);
        this.context=context;
        this.title=title;
    }

    public void setList(List<Fragment> list) {
        this.list = list;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return title[position];
    }

    @Override
    public Fragment getItem(int position) {
        return list.get(position);
    }

    @Override
    public int getCount() {
        return list.size();
    }
}







public class MyBase extends BaseAdapter {
    private Context context;
    private List<Daa> list;

    public MyBase(Context context, List<Daa> list) {
        this.context = context;
        this.list = list;
    }

    @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;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if(view==null){
             viewHolder = new ViewHolder();
            view=View.inflate(context, R.layout.lv_layout,null);
            viewHolder.img = (ImageView) view.findViewById(R.id.img);
            viewHolder.title2 = (TextView) view.findViewById(R.id.title2);
            viewHolder.wang2 = (TextView) view.findViewById(R.id.wang2);
            view.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) view.getTag();
        }

        Daa dd=list.get(i);
        //设置缓存图片
        DisplayImageOptions options=new DisplayImageOptions.Builder()
                .cacheOnDisk(true)
                .cacheInMemory(true).build();

        viewHolder.title2.setText(dd.title);
        viewHolder.wang2.setText(dd.author_name);
        ImageLoader.getInstance().displayImage(dd.thumbnail_pic_s,viewHolder.img,options);
        return view;
    }

    class ViewHolder{

        public ImageView img;
        public TextView title2;
        public TextView wang2;
    }
}







public class StreamUtils {
    public static String parser(InputStream inputStream){
        StringBuilder sbb=new StringBuilder();
        String str;
        BufferedReader br=new BufferedReader(new InputStreamReader(inputStream));
        try {
            while((str=br.readLine())!=null){
                sbb.append(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return sbb.toString();
    }
}


public class TitleFragment extends Fragment {

    private ListView lv;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view1=View.inflate(getActivity(), R.layout.fragment,null);
        //初始化控件
        lv = (ListView) view1.findViewById(R.id.listview);
        //设置地址
        String path="http://v.juhe.cn/toutiao/index?type=%22+mParam2+%22&key=ebe6062b9049b169b8a611b5051c41c9";
        //实例化AsuyncTask
        MyTask myTask = new MyTask();
        myTask.execute(path);
        return view1;
    }

    class MyTask extends AsyncTask<String,Void,String>{
        private String result;
        private List<Daa> list;
        @Override
        protected String doInBackground(String... strings) {
            try {
                //请求数据
                URL url=new URL(strings[0]);
                HttpURLConnection connection= (HttpURLConnection) url.openConnection();
                int code=connection.getResponseCode();
                if(code==200){
                    InputStream inputStream=connection.getInputStream();
                    result = StreamUtils.parser(inputStream);
                }
                //开始解析数据
                Gson gson=new Gson();
                JsonBean jsonBean = gson.fromJson(result, JsonBean.class);
                list = new ArrayList<>();
                //循环解析
                for(int i=0;i<jsonBean.result.data.size();i++){
                    Daa da=new Daa(jsonBean.result.data.get(i).author_name,
                            jsonBean.result.data.get(i).thumbnail_pic_s,
                            jsonBean.result.data.get(i).title);
                    list.add(da);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            MyBase myBase = new MyBase(getActivity(), list);
            lv.setAdapter(myBase);
        }
    }
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...