1. 程式人生 > >tabLayout+頻道管理

tabLayout+頻道管理

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    >
    <LinearLayout
        android:id="@+id/ll_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true">
        <TextView
            android:id="@+id/tuijian"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="10dp"
            android:gravity="center"
            android:text="推薦"/>
        <TextView
            android:id="@+id/rexian"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="10dp"
            android:gravity="center"
            android:text="熱線"/>
        <TextView
            android:id="@+id/shenghuo"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="10dp"
            android:gravity="center"
            android:text="生活幫"/>
        <TextView
            android:id="@+id/tongcheng"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="10dp"
            android:gravity="center"
            android:text="同城"/>
    </LinearLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_above="@id/ll_bottom"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v4.view.ViewPager>

</RelativeLayout>

fragment

package com.example.newsmonth2;

import android.graphics.Color;
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 android.os.Bundle;
import android.view.View;
import android.widget.TextView;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ViewPager pager;
    private TextView tuijian;
    private TextView rexian;
    private TextView shenghuo;
    private TextView tongcheng;
    private TuiJianFragment tuiJianFragment;
    private ReDianFragment reDianFragment;
    private ShengHuoFragment shengHuoFragment;
    private TongChengFragment tongChengFragment;
    private List<Fragment> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pager = findViewById(R.id.pager);
        tuijian = findViewById(R.id.tuijian);
        rexian = findViewById(R.id.rexian);
        shenghuo = findViewById(R.id.shenghuo);
        tongcheng = findViewById(R.id.tongcheng);
        tuiJianFragment = new TuiJianFragment();
        reDianFragment = new ReDianFragment();
        shengHuoFragment = new ShengHuoFragment();
        tongChengFragment = new TongChengFragment();
        list = new ArrayList<>();
        list.add(tuiJianFragment);
        list.add(reDianFragment);
        list.add(shengHuoFragment);
        list.add(tongChengFragment);
        tuijian.setOnClickListener(this);
        rexian.setOnClickListener(this);
        shenghuo.setOnClickListener(this);
        tongcheng.setOnClickListener(this);
        tuijian.setBackgroundColor(Color.RED);
        pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }

            @Override
            public int getCount() {
                return list.size();
            }
        });
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                color(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tuijian:
                pager.setCurrentItem(0);
                color(0);
                break;
            case R.id.rexian:
                pager.setCurrentItem(1);
                color(1);
                break;
            case R.id.shenghuo:
                pager.setCurrentItem(2);
                color(2);
                break;
            case R.id.tongcheng:
                pager.setCurrentItem(3);
                color(3);
                break;
        }
    }
    private void color(int index){
        tuijian.setBackgroundColor(index==0?Color.RED:Color.WHITE);
        rexian.setBackgroundColor(index==1?Color.RED:Color.WHITE);
        shenghuo.setBackgroundColor(index==2?Color.RED:Color.WHITE);
        tongcheng.setBackgroundColor(index==3?Color.RED:Color.WHITE);
    }
}

第一個fragment頁面

package com.example.newsmonth2;

import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

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


public class TuiJianFragment extends Fragment implements View.OnClickListener {

    private TabLayout tabLayout;
    private ViewPager pager;
    private List<String> list;
    private List<Fragment> fragmentList;
    private ImageView img_add;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_tui_jian, container, false);
        tabLayout = v.findViewById(R.id.tbl_news);
        pager = v.findViewById(R.id.viewpager);
        img_add = v.findViewById(R.id.img_add);
        img_add.setOnClickListener(this);
        list = new ArrayList<>();
        list.add("全部");
        list.add("國內");
        list.add("國際");
        list.add("科技");
        list.add("股市");
        list.add("美股");
        list.add("財經");
        list.add("軍事");
        list.add("科技");
        List<String> list1=new ArrayList<>();
        list1.add("http://www.xieast.com/api/news/news.php?type=top&page=");
        list1.add("http://www.xieast.com/api/news/news.php?type=shehui&page=");
        list1.add("http://www.xieast.com/api/news/news.php?type=guonei&page=");
        list1.add("http://www.xieast.com/api/news/news.php?type=guoji&page=");
        list1.add("http://www.xieast.com/api/news/news.php?type=yule&page=");
        list1.add("http://www.xieast.com/api/news/news.php?type=tiyu&page=");
        list1.add("http://www.xieast.com/api/news/news.php?type=junshi&page=");
        list1.add("http://www.xieast.com/api/news/news.php?type=keji&page=");
        fragmentList = new ArrayList<>();
        for (int i = 0; i <list1.size() ; i++) {
            NewsFragment newsFragment=new NewsFragment();
            Bundle bundle=new Bundle();
            bundle.putString("url",list1.get(i));
            newsFragment.setArguments(bundle);
            fragmentList.add(newsFragment);
        }
        tabLayout.setTabMode(tabLayout.MODE_SCROLLABLE);
        tabLayout.setTabTextColors(ColorStateList.valueOf(Color.BLACK));
        pager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

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

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return list.get(position);
            }
        });
        tabLayout.setupWithViewPager(pager);
        return v;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.img_add:
                Intent intent=new Intent(getActivity(),ChannelActivity.class);
                startActivity(intent);
                Toast.makeText(getActivity(),"點選",Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

tablayout佈局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/img_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:padding="10dp"
            android:src="@mipmap/btn_add" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tbl_news"
            android:layout_width="560dp"
            android:layout_height="wrap_content"></android.support.design.widget.TabLayout>
    </RelativeLayout>

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

傳值

package com.example.newsmonth2;

import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bwie.xlistview.XListView;
import com.google.gson.Gson;

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


public class NewsFragment extends Fragment {

    private XListView xListView;
    private ViewPager pager;
    private List<News.DataBean> list;
    private NewsAdapter adapter;
    private int page=1;
    Handler handler=new Handler();
    private String url;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle=getArguments();
        url= (String) bundle.get("url");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v= inflater.inflate(R.layout.fragment_news, container, false);
        xListView = v.findViewById(R.id.xlistview);
        pager = v.findViewById(R.id.viewpager);
        list = new ArrayList<>();
        adapter = new NewsAdapter(getActivity(), list);
        xListView.setAdapter(adapter);
        xListView.setPullLoadEnable(true);
        xListView.setPullRefreshEnable(true);
        getData(1,false);
        xListView.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                page=1;
                getData(page,false);
            }

            @Override
            public void onLoadMore() {
                page++;
                getData(page,true);
            }
        });
        return v;
    }

    private void getData(int i, final boolean b) {
        new AsyncTask<String, Integer, String>() {
            @Override
            protected String doInBackground(String... strings) {
                String result=HttpUtils.getHttpFroment(strings[0]);
                return result;
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                if(!TextUtils.isEmpty(s)){
                    Gson gson=new Gson();
                    News news=gson.fromJson(s,News.class);
                    if(news!=null){
                       if(news.getData()!=null){
                            if(!b){
                                list.clear();
                            }
                            list.addAll(news.getData());
                            adapter.notifyDataSetChanged();
                       }
                   }
                }
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if(b){
                            xListView.stopLoadMore();
                        }else{
                            xListView.stopRefresh();
                            xListView.setRefreshTime("剛剛");
                        }
                    }
                },2000);
            }
        }.execute(url+page);
    }
}

頻道管理

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#8c5d5d"
        android:padding="10dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="排序"/>
        <TextView
            android:id="@+id/txt_finish"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="編輯"/>
    </RelativeLayout>
    <com.bawei.channelmanager.ui.ChannelView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/channel_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

package com.example.newsmonth2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;

import com.bawei.channelmanager.bean.ChannelBean;
import com.bawei.channelmanager.ui.ChannelView;

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

public class ChannelActivity extends AppCompatActivity {
    private ChannelView channelView;
    private List<ChannelBean> channelBeans = new ArrayList<>();     // 我的頻道資料集合
    private List<ChannelBean> moreChannelBeans = new ArrayList<>();     // 更多頻道資料集合

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_channel);
        channelView = findViewById(R.id.channel_view);      // 查詢控制元件
        channelView.showDefaultHeadView(false);     // 設定隱藏預設的HeadView
        channelView.showDefaultMoreView(false);     // 設定隱藏預設的MoreView
//        channelView.addHead(LayoutInflater.from(this).inflate(R.layout.love_channel, null, false));     // 新增自定義的head
//        channelVieww.addMore(LayoutInflater.from(this).inflate(R.layout.more_channel, null, false));     // 新增自定義的more


        for (int i = 0; i < 20; i++) {
            if (i % 2 == 0) {
                channelBeans.add(new ChannelBean("頻道" + i, i, "url", true));
            } else {
                moreChannelBeans.add(new ChannelBean("頻道" + i, i, "url", false));
            }

        }

        channelView.init(channelBeans, moreChannelBeans, new ChannelView.ChannelCallback() {
            @Override
            public void onMyChannelRemove(ChannelBean channelBean) {

            }

            @Override
            public void moMoreChannelRemove(ChannelBean channelBean) {

            }
        });

    }
}