Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android,底部導航+viewpager+fragment+drawerlayout+toolbar+recyclerview

Android,底部導航+viewpager+fragment+drawerlayout+toolbar+recyclerview

編輯:關於Android編程

將自己的編程經歷寫出來是個好習慣

先來效果圖:

\\\\

項目結構:

\

1、底部導航

底部導航的實現思路,使用button等可點擊控件並置於父布局底部,配合viewpage或fragmentr實現點擊切換視圖(本項目使用viewpager)。

需要3個xml文件

\

圖片選擇器selector:

 



    
    

 

樣式style:

 

 

字符串string:

 

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


底部導航xml:

 



                

                

                

                

                
            

底部導航的點擊監聽器:(完成布局加載,findviewbyid,onclickedlistener之後)

 

 public void onClick(View v) {
        if (v == tag_1) {
            tag_2.setChecked(false);
            tag_3.setChecked(false);
           
        } else if (v == tag_2) {
            tag_1.setChecked(false);
            tag_3.setChecked(false);
        
        } else if (v == tag_3) {
            tag_1.setChecked(false);
            tag_2.setChecked(false);
           
        }


2、viewpager+fragment

實現思路:在布局中加入viewpager並使用fragmentpageradapter進行設置適配器(viewpager是view的切換控件,基本適配器為pageradapter)

需要自定義fragment並加載布局:

\

fragment的xml:

 




fragment的java:

 

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

具體實現:(完成布局加載,findviewbyid之後)

 

     
private ViewPager viewpager; private List fragmentlist;

//viewpager初始化(寫入oncreate方法內) FragmentManager manager = getSupportFragmentManager(); fragmentlist = new ArrayList(); fragmentlist.add(new NewsFragment()); fragmentlist.add(new ChatFragment()); fragmentlist.add(new TestFragment()); viewpager.setAdapter(new MyAdapter(manager, fragmentlist));

/** *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(); } }


 

toolbar的java:

 

        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_info);
        toolbar.setTitle(R.string.app_name);
        toolbar.setSubtitle(R.string.subtitle);


3、toolbar

實現思路:使用5.0版本的新控件toolbar(先關閉應用自帶的actionbar)

 


或直接寫入manifest文件中:
android:windowActionBar=false

 

toolbar的xml:

 

      

 

toolbar的java:

 

        
//toolbar初始化,正常是先初始化toolbar再setsupportactionbar我這運行沒問題,如果出錯可以換下先後


setSupportActionBar(toolbar); toolbar.setNavigationIcon(R.drawable.ic_info); toolbar.setTitle(R.string.app_name); toolbar.setSubtitle(R.string.subtitle);

 




 

4、drawerlayout

實現思路:使用新控件drawerlayout實現側滑

drawerlayout的xml:

 
        
       

       

 



drawerlayout的java:
//手動打開關閉側滑
 drawerLayout.closeDrawer(left_dawer);
 drawerLayout.openDrawer(left_dawer);
//是否打開了側滑
drawerLayout.isDrawerOpen(left_dawer)

 

5、recyclerview

實現思路:使用新控件recyclerview

需要layoutmanager+adapter+decoration+移除添加動畫

 

 

  left_dawer.setLayoutManager(new LinearLayoutManager(this));
        left_dawer.setAdapter(new MyRecyclerViewAdapter(data));

 /**
     * 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;
        }

 

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