1. 程式人生 > >仿今日頭條

仿今日頭條

在這裡插入圖片描述

main xml

<?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:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/hsv">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal"
            android:id="@+id/ll_header">

        </LinearLayout>
    </HorizontalScrollView>
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>

main java

package com.example.demo1;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends FragmentActivity implements View.OnClickListener {
    private LinearLayout ll_head;
    private ViewPager viewPager;
    private HorizontalScrollView hsv;
    private String[] titles;
    private ArrayList<TextView> titlesView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ll_head = (LinearLayout) findViewById(R.id.ll_header);
        hsv = (HorizontalScrollView) findViewById(R.id.hsv);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        titles = new String[]{"頭條", "娛樂", "科技", "資訊", "八卦", "北京", "上海", "天津",
                "重慶", "大燕網"};
        initTitles();
        viewPager.setAdapter(new MPagerAdapter(getSupportFragmentManager()));
        setOnClickListener();
    }

    private void setOnClickListener() {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < titlesView.size(); i++) {
                    if (i == position) {
                        titlesView.get(position).setTextColor(Color.RED);

                        Log.e("wzq","點選標題:"+position+"----size:"+titlesView.size());
                    }else {
                        titlesView.get(i).setTextColor(Color.GREEN);
                    }

                }
                //得到距離左側父控制元件 的長度;
                float mMargin = titlesView.get(position).getX();
                hsv.scrollTo((int) mMargin,0); //自動滑動;
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    private void initTitles() {
        titlesView=new ArrayList<>();
        for (int i=0;i<titles.length;i++){
            TextView textView=new TextView(this);
            textView.setTextSize(24);
            textView.setText(titles[i]);
            if (i==0){
                textView.setTextColor(Color.RED);
            }else {}
            textView.setOnClickListener(this);
            textView.setId(i);
            LinearLayout.LayoutParams layoutParams =
                    new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(10,10,10,10);
            ll_head.addView(textView,layoutParams);

            titlesView.add(textView);
        }
    }

    @Override
    public void onClick(View v) {
        int position=v.getId();
        viewPager.setCurrentItem(position);
    }

    private class MPagerAdapter extends FragmentPagerAdapter {
        public MPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return frag_01.getInstance(titles[i]);
        }

        @Override
        public int getCount() {
            return titles.length;
        }
    }
}

frag xml

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

    <TextView
        android:id="@+id/tv_item"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Fragment0"
        android:textSize="30sp" />

</LinearLayout>

java frag

package com.example.demo1;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class frag_01 extends Fragment{
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Bundle bundle=getArguments();
        TextView textView=new TextView(getActivity());
        textView.setTextSize(100);
        textView.setText(bundle.getString("title"));
        return textView;
    }
    public static Fragment getInstance(String title){
        frag_01 one=new frag_01();
        Bundle bundle=new Bundle();
        bundle.putString("title",title);
        one.setArguments(bundle);
        return one;
    }
}