51dev.com IT技术开发者社区

51dev.com 技术开发者社区

仿微信的滑动和改变字体图标颜色

Android互联网报道阅读(31)2019-09-18 收藏0次评论

今天在慕课上学了仿微信的滑动,于是就重新敲了代码在原有的图形上又增加了改变字体的颜色。这里将代码放在这里便于以后学习

整个过程用了ViewPager与PagerAdapter,思路捋清了还是挺简单的。

tZ喎?https://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcC54bWw8L3N0cm9uZz48L3A+DQo8cHJlIGNsYXNzPQ=="brush:java;"> android:background="@drawable/title_bar" android:gravity="center" android:layout_height="45dp" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="https://schemas.android.com/apk/res/android">

bottom.xml




    

        

        
    

    

    

        

        
    

    

    

        

        
    
    

    

        

        
    

activity_main.xml



    

    
    

    

tab01.xml




    

tab02.xml




    

tab03.xml




    

tab04.xml




    

颜色

 #558B2F

MainActivity.java

package com.example.weixintest01;

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

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

    private ViewPager mPager;
    List mlist = new ArrayList();

    private LinearLayout mLayout1;
    private LinearLayout mLayout2;
    private LinearLayout mLayout3;
    private LinearLayout mLayout4;

    private ImageView img1;
    private ImageView img2;
    private ImageView img3;
    private ImageView img4;

    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private TextView tv4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        init();

        initEvent();
    }

    private void initEvent() {
        mLayout1.setOnClickListener(this);
        mLayout2.setOnClickListener(this);
        mLayout3.setOnClickListener(this);
        mLayout4.setOnClickListener(this);
        mPager.setAdapter(adapter);//设置适配器
        mPager.setOnPageChangeListener(new OnPageChangeListener() {//设置页面滑动监听器

            @Override
            public void onPageSelected(int arg0) {
                int current = mPager.getCurrentItem();
                resetImage();
                switch (current) {
                case 0:
  //设置按下去的图片和字体颜色                   img1.setImageResource(R.drawable.tab_weixin_pressed);
                    tv1.setTextColor(Color.GREEN);
                    break;
                case 1:
                    img2.setImageResource(R.drawable.tab_address_pressed);
                    tv2.setTextColor(Color.GREEN);
                    break;
                case 2:
                    img3.setImageResource(R.drawable.tab_find_frd_pressed);
                    tv3.setTextColor(Color.GREEN);
                    break;
                case 3:
                    img4.setImageResource(R.drawable.tab_settings_pressed);
                    tv4.setTextColor(Color.GREEN);
                    break;
                default:
                    break;
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    private void init() {
        // 实例化ViewPager
        mPager = (ViewPager) findViewById(R.id.content);
        // 实例化bottom布局
        mLayout1 = (LinearLayout) findViewById(R.id.tab_weixin);
        mLayout2 = (LinearLayout) findViewById(R.id.tab_address);
        mLayout3 = (LinearLayout) findViewById(R.id.tab_friend);
        mLayout4 = (LinearLayout) findViewById(R.id.tab_setting);

        // 实例化bottom的imageView
        img1 = (ImageView) findViewById(R.id.bg_img1);
        img2 = (ImageView) findViewById(R.id.bg_img2);
        img3 = (ImageView) findViewById(R.id.bg_img3);
        img4 = (ImageView) findViewById(R.id.bg_img4);

        tv1 = (TextView) findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);
        tv3 = (TextView) findViewById(R.id.tv3);
        tv4 = (TextView) findViewById(R.id.tv4);

        // 加载布局
        LayoutInflater inflater = LayoutInflater.from(this);
        View tab01 = inflater.inflate(R.layout.tab01, null);
        View tab02 = inflater.inflate(R.layout.tab02, null);
        View tab03 = inflater.inflate(R.layout.tab03, null);
        View tab04 = inflater.inflate(R.layout.tab04, null);

        // 添加到容器中
        mlist.add(tab01);
        mlist.add(tab02);
        mlist.add(tab03);
        mlist.add(tab04);

    }

    PagerAdapter adapter = new PagerAdapter() {

        // 初始化布局
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
        //添加当前位置的布局
            container.addView(mlist.get(position));
            return mlist.get(position);
        }

        // 移除当前位置的布局
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mlist.get(position));
        }

        // 获得容器中的个数
        @Override
        public int getCount() {
            return mlist.size();
        }

        // 判断View==object
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }
    };

    // 布局的点击事件
    @Override
    public void onClick(View v) {

        resetImage();//前提将所有的图片设置为暗色,字体颜色设置为白色,然后在下面的点击事件中重新设置图片和字体颜色
        switch (v.getId()) {
        case R.id.tab_weixin:
            mPager.setCurrentItem(0);
            img1.setImageResource(R.drawable.tab_weixin_pressed);
            tv1.setTextColor(Color.GREEN);
            break;
        case R.id.tab_address:
            mPager.setCurrentItem(1);
            img2.setImageResource(R.drawable.tab_address_pressed);
            tv2.setTextColor(Color.GREEN);
            break;
        case R.id.tab_friend:
            mPager.setCurrentItem(2);
            img3.setImageResource(R.drawable.tab_find_frd_pressed);
            tv3.setTextColor(Color.GREEN);
            break;
        case R.id.tab_setting:
            mPager.setCurrentItem(3);
            img4.setImageResource(R.drawable.tab_settings_pressed);
            tv4.setTextColor(Color.GREEN);
            break;
        default:
            break;
        }
    }

//设置图片和字体的颜色
    private void resetImage() {
        img1.setImageResource(R.drawable.tab_weixin_normal);
        img2.setImageResource(R.drawable.tab_address_normal);
        img3.setImageResource(R.drawable.tab_find_frd_normal);
        img4.setImageResource(R.drawable.tab_settings_normal);

        tv1.setTextColor(Color.WHITE);
        tv2.setTextColor(Color.WHITE);
        tv3.setTextColor(Color.WHITE);
        tv4.setTextColor(Color.WHITE);

    }
}

以上就是仿微信的滑动和改变字体图标颜色的全部内容,请多关注【51DEV】IT技术开发者社区。