tablayout导航栏

tablayout的依赖
compile 'com.android.support:design:23.4.0'


package com.example.myapplication;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    TabLayout ta;
    private ViewPager vp;
    private List<String> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        vp = (ViewPager) findViewById(R.id.vp);

        ta= (TabLayout) findViewById(R.id.tab);
        list=new ArrayList<>();
        list.add("头条");
        list.add("社会");
        list.add("国内");
        list.add("国际");
        list.add("娱乐");
        list.add("体育");
        list.add("军事");
        list.add("科技");
        list.add("财经");
        list.add("时尚");

        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {

            @Override
            public CharSequence getPageTitle(int position) {
                return list.get(position);
            }
            @Override
            public Fragment getItem(int position) {
                Fragments fragments = new Fragments();
                Bundle bundle = new Bundle();
                if(list.get(position).endsWith("头条")){
                    bundle.putString("name","top");
                }else if (list.get(position).equals("社会")){
                    bundle.putString("name","shehui");
                }else if (list.get(position).equals("国内")){
                    bundle.putString("name","guonei");
                }else if (list.get(position).equals("国际")){
                    bundle.putString("name","guoji");
                }else if (list.get(position).equals("娱乐")){
                    bundle.putString("name","yule");
                }else if (list.get(position).equals("体育")){
                    bundle.putString("name","tiyu");
                }else if (list.get(position).equals("军事")){
                    bundle.putString("name","junshi");
                }else if (list.get(position).equals("科技")){
                    bundle.putString("name","keji");
                }else if (list.get(position).equals("财经")){
                    bundle.putString("name","caijing");
                }else if (list.get(position).equals("时尚")){
                    bundle.putString("name","shishang");
                }
                fragments.setArguments(bundle);
                return fragments;
            }
            @Override
            public int getCount() {
                return list.size();
            }
        });

        ta.setupWithViewPager(vp);
    }

}
//fragment加网络请求数据
package com.example.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
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.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

public class Fragments extends Fragment {

    private ListView lv;

    private Handler handler=new Handler(){

        @Override
        public void handleMessage(Message msg) {
            if(msg.what==0){
                JsonRootBean bean= (JsonRootBean) msg.obj;
                list = bean.getResult().getData();
                Log.i("aaa",list.toString());
//                Myad myad=new Myad();
//                lv.setAdapter(myad);
            }
        }
    };
    private List<Data> list;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v=View.inflate(getActivity(),R.layout.ragments,null);
        lv = v.findViewById(R.id.lv);
        return v;
    }

    //适配
    class Myad extends BaseAdapter{

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

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            He he=null;
            if(view==null){
                view=View.inflate(getActivity(),R.layout.tem,null);
                he=new He();
                 he.img= view.findViewById(R.id.img);
                he.wenben=view.findViewById(R.id.wenzi);
                view.setTag(he);
            }else{
                he= (He) view.getTag();

            }
            he.wenben.setText(list.get(i).getTitle());
            ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s(),he.img);
            return view;
        }
        class He{
            ImageView img;
            TextView wenben;
        }
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();
        String string=bundle.getString("name","top");
        getData(string);
    }

    private void getData(final String string) {
        new Thread(){
            @Override
            public void run() {
                String path="http://v.juhe.cn/toutiao/index?type="+string+"&key=597b4f9dcb50e051fd725a9ec54d6653";
                try {
                    URL url=new URL(path);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(5000);
                    connection.setReadTimeout(5000);
                    if(connection.getResponseCode()==200){
                        InputStream stream = connection.getInputStream();
                        String json=zhuan(stream,"utf-8");
                        Gson gson = new Gson();
                        JsonRootBean jsonRootBean = gson.fromJson(json, JsonRootBean.class);
                        Message message=Message.obtain();
                        message.what=0;
                        message.obj=jsonRootBean;
                        handler.sendMessage(message);

                    }

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private String zhuan(InputStream stream, String s) {
        try {
            InputStreamReader in=new InputStreamReader(stream,s);
            BufferedReader re=new BufferedReader(in);
            String ss=null;
            StringBuilder builder=new StringBuilder();
            while((ss=re.readLine())!=null){
                builder.append(ss);

            }
            return builder.toString();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


}
//加载网络图片
@Override
public void onCreate() {
    DisplayImageOptions options=new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.jiazai).displayer(new FadeInBitmapDisplayer(2000)).cacheOnDisc(true).cacheInMemory(true).build();
    ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this).defaultDisplayImageOptions(options).threadPoolSize(5).build();
    ImageLoader.getInstance().init(configuration);
    super.onCreate();
}


 
//关于导航布局
<android.support.design.widget.TabLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tab"
    app:tabIndicatorColor="#f5e20d"
    app:tabIndicatorHeight="5dp"
    app:tabGravity="center"
    app:tabSelectedTextColor="#090df7"
    app:tabTextColor="#000000"
    app:tabMode="scrollable">
</android.support.design.widget.TabLayout>



  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值