Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 導航界面的搭建(完整demo)

導航界面的搭建(完整demo)

編輯:關於Android編程

manifest:

 




    
        
            
                

                
            
        
    


 

corlors:

 



    #3F51B5
    #303F9F
    #FF4081
    #ffffff 
    #fffff0 
    #ffffe0 
    #ffff00 
    #fffafa 
    #fffaf0 
    #fffacd 
    #fff8dc 
    #fff5ee 
    #fff0f5 
    #ffefd5 
    #ffebcd 
    #ffe4e1 
    #ffe4c4 
    #ffe4b5 
    #ffdead 
    #ffdab9 
    #ffd700 
    #ffc0cb 
    #ffb6c1 
    #ffa500 
    #ffa07a 
    #ff8c00 
    #ff7f50 
    #ff69b4 
    #ff6347 
    #ff4500 
    #ff1493 
    #ff00ff 
    #ff00ff 
    #ff0000 
    #fdf5e6 
    #fafad2 
    #faf0e6 
    #faebd7 
    #fa8072 
    #f8f8ff 
    #f5fffa 
    #f5f5f5 
    #f5f5dc 
    #f5deb3 
    #f4a460 
    #f0ffff 
    #f0fff0 
    #f0f8ff 
    #f0e68c 
    #f08080 
    #eee8aa 
    #ee82ee 
    #e9967a 
    #e6e6fa 
    #e0ffff 
    #deb887 
    #dda0dd 
    #dcdcdc 
    #dc143c 
    #db7093 
    #daa520 
    #da70d6 
    #d8bfd8 
    #d3d3d3 
    #d3d3d3 
    #d2b48c 
    #d2691e 
    #cd853f 
    #cd5c5c 
    #c71585 
    #c0c0c0 
    #bdb76b 
    #bc8f8f 
    #ba55d3 
    #b8860b 
    #b22222 
    #b0e0e6 
    #b0c4de 
    #afeeee 
    #adff2f 
    #add8e6 
    #a9a9a9 
    #a9a9a9 
    #a52a2a 
    #a0522d 
    #9932cc 
    #98fb98 
    #9400d3 
    #9370db 
    #90ee90 
    #8fbc8f 
    #8b4513 
    #8b008b 
    #8b0000 
    #8a2be2 
    #87cefa 
    #87ceeb 
    #808080 
    #808080 
    #808000 
    #800080 
    #800000 
    #7fffd4 
    #7fff00 
    #7cfc00 
    #7b68ee 
    #778899 
    #778899 
    #708090 
    #708090 
    #6b8e23 
    #6a5acd 
    #696969 
    #696969 
    #66cdaa 
    #6495ed 
    #5f9ea0 
    #556b2f 
    #4b0082 
    #48d1cc 
    #483d8b 
    #4682b4 
    #4169e1 
    #40e0d0 
    #3cb371 
    #32cd32 
    #2f4f4f 
    #2f4f4f 
    #2e8b57 
    #228b22 
    #20b2aa 
    #1e90ff 
    #191970 
    #00ffff 
    #00ffff 
    #00ff7f 
    #00ff00 
    #00fa9a 
    #00ced1 
    #00bfff 
    #008b8b 
    #008080 
    #008000 
    #006400 
    #0000ff 
    #0000cd 
    #00008b 
    #000080 
    #000000 




 

Strings:

 


    ZNDJ
    感謝使用本app
    關於
    新聞
    聊天
    測試
    
    
    Hello blank fragment

 

styles:

 


    

 

menu:

 



 

layout-chat:

 






 

layout-news

 






 

layout-test:

 






 

layout-recyclerview-item:

 






\

 

selector-chat:

 



    
    

 

selector-news:

 



    
    

 

selector-test:

 


   

 


 

ChatFragment:

 

package com.example.swing.zndj;

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

/**
 * Created by swing on 2016/9/16.
 */
public class ChatFragment extends Fragment {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.layout_chat,null);
        return view;
    }
}

 

NewsFragment:

 

package com.example.swing.zndj;

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

public class NewsFragment extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_news,null);
        return view;
    }
}

 

TestFragment:

 

package com.example.swing.zndj;

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

/**
 * Created by swing on 2016/9/16.
 */
public class TestFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.layout_test,null);
    }
}

 

MainActivity:

 

package com.example.swing.zndj;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.TextView;


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


public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private Toolbar toolbar;
    private ViewPager viewpager;
    private List fragmentlist;
    private RadioButton tag_1, tag_2, tag_3;
    private RecyclerView left_dawer;
    private TextView right_dawer;
    private DrawerLayout drawerLayout;
    private String[] data ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        findviewbyid();
        initData();
        init();
        clicked();
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

            if (drawerLayout.isDrawerOpen(right_dawer)){
                drawerLayout.closeDrawer(right_dawer);
            }else{
                if (drawerLayout.isDrawerOpen(left_dawer)){
                    drawerLayout.closeDrawer(left_dawer);
                }
                drawerLayout.openDrawer(right_dawer);
            }
            return true;

    }
    /*
        findviewbyid
         */
    public void findviewbyid() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        viewpager = (ViewPager) findViewById(R.id.viewpager);
        tag_1 = (RadioButton) findViewById(R.id.tag_1);
        tag_2 = (RadioButton) findViewById(R.id.tag_2);
        tag_3 = (RadioButton) findViewById(R.id.tag_3);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);
        left_dawer = (RecyclerView) findViewById(R.id.left_drawer);
        right_dawer = (TextView) findViewById(R.id.right_drawer);
    }
    /*
    原始數據的獲取
     */
    public void initData(){
        data = new String[]{"1","2","3","1","2","3","1","2","3"};
    }
    /*
    控件初始化
     */
    public void init() {

        //toolbar初始化
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_info);
        toolbar.setTitle(R.string.app_name);
        toolbar.setSubtitle(R.string.subtitle);
        //viewpager初始化
        FragmentManager manager = getSupportFragmentManager();
        fragmentlist = new ArrayList();
        fragmentlist.add(new NewsFragment());
        fragmentlist.add(new ChatFragment());
        fragmentlist.add(new TestFragment());
        viewpager.setAdapter(new MyAdapter(manager, fragmentlist));
        //left_dawer初始化
        left_dawer.setLayoutManager(new LinearLayoutManager(this));
        left_dawer.setAdapter(new MyRecyclerViewAdapter(data));

    }
    /*
    控件的監聽器注冊
     */
    public void clicked() {
        toolbar.setNavigationOnClickListener(this);
        tag_1.setOnClickListener(this);
        tag_2.setOnClickListener(this);
        tag_3.setOnClickListener(this);
        viewpager.setOnPageChangeListener(new MyChangeListener(tag_1, tag_2, tag_3));
        drawerLayout.setDrawerListener(new MyDrawerLayoutListener());
    }
    /*
    點擊監聽器的監聽邏輯(導航按鈕,底部導航)
     */
    @Override
    public void onClick(View v) {
        if (v == tag_1) {
            tag_2.setChecked(false);
            tag_3.setChecked(false);
            viewpager.setCurrentItem(0);
        } else if (v == tag_2) {
            tag_1.setChecked(false);
            tag_3.setChecked(false);
            viewpager.setCurrentItem(1);
        } else if (v == tag_3) {
            tag_1.setChecked(false);
            tag_2.setChecked(false);
            viewpager.setCurrentItem(2);
        }else{

            if (drawerLayout.isDrawerOpen(left_dawer)){
                drawerLayout.closeDrawer(left_dawer);
                toolbar.setNavigationIcon(R.drawable.ic_info);
            }else {
                if (drawerLayout.isDrawerOpen(right_dawer)){
                    drawerLayout.closeDrawer(right_dawer);
                }
                drawerLayout.openDrawer(left_dawer);
                toolbar.setNavigationIcon(R.drawable.ic_info1);
            }
        }
    }

    /**
     *viewpager的適配器類,參數FragmentManager,List
     */
    class MyAdapter extends FragmentPagerAdapter {
        List fragmentList = new ArrayList<>();

        public MyAdapter(FragmentManager fm, List fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;
        }

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

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

    /**
     *viewpager的頁面選擇監聽器
     */
    class MyChangeListener implements ViewPager.OnPageChangeListener {
        RadioButton tag_1, tag_2, tag_3;

        public MyChangeListener(RadioButton tag_1, RadioButton tag_2, RadioButton tag_3) {
            this.tag_1 = tag_1;
            this.tag_2 = tag_2;
            this.tag_3 = tag_3;
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            if (position == 0) {
                tag_1.setChecked(true);
                tag_2.setChecked(false);
                tag_3.setChecked(false);
            } else if (position == 1) {
                tag_1.setChecked(false);
                tag_2.setChecked(true);
                tag_3.setChecked(false);
            } else if (position == 2) {
                tag_1.setChecked(false);
                tag_2.setChecked(false);
                tag_3.setChecked(true);
            }
        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
    /**
     * RecyclerView的適配器類
     */
    class MyRecyclerViewAdapter extends RecyclerView.Adapter{
        String[] data;
        public MyRecyclerViewAdapter(String[] data) {
            this.data = data;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_revyvlerview_item,viewGroup,false);
            MyViewHolder myviewholder = new MyViewHolder(view);
            return myviewholder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder myViewHolder, int i) {
            myViewHolder.textview.setText(data[i]);
        }

        @Override
        public int getItemCount() {
            return data.length;
        }

        class MyViewHolder extends RecyclerView.ViewHolder{
            TextView textview;
            public MyViewHolder(View itemView) {
                super(itemView);
               textview = (TextView) itemView.findViewById(R.id.recyclerview_item);
            }
        }
    }
    /**
     * drawerlayout開閉狀態監聽器
     */
    class MyDrawerLayoutListener extends DrawerLayout.SimpleDrawerListener{
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            if (drawerView == left_dawer) {
                toolbar.setNavigationIcon(R.drawable.ic_info1);
            }else{
                //right_drawer判斷
            }
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            if (drawerView == left_dawer) {
                toolbar.setNavigationIcon(R.drawable.ic_info);
            }else {
                //right_drawer判斷
            }
        }
    }
}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved