Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

扣丁音樂(三):UI 框架的實作

編輯:關於 Android 程式設計

一、PagerSlidingTabStrip 的運用

扣丁音樂 1.0 前半段(受限於 gif 大小)演示:

影片教學中直接沿用 PagerSlidingTabStrip 範例的主頁面,並針對用到的部分做出修改。

修改一:activity_main.xml 僅保留以下內容,其餘控件刪除。

 <com.astuetz.pagerslidingtabstrip
    android:id="@+id/tabs" android:layout_width="match_parent"
    android:layout_height="48dip" android:background="@drawable/background_tabs">
<android.support.v4.view.viewpager
    android:id="@+id/pager" android:layout_width="match_parent"
    android:layout_height="wrap_content" android:layout_below="@+id/tabs"
    tools:context=".MainActivity"></android.support.v4.view.viewpager>
</com.astuetz.pagerslidingtabstrip>

修改二:MainActivity.java

預設顏色:

 private int currentColor = 0xFFC74B46;

滑動項名稱:

 private final String[] TITLES = { "我的音樂", "音樂館" };

滑動 Fragment 的相關加載:

 @Override public Fragment getItem(int position) { if(position==0){ if(myMusicFragment==null){ myMusicFragment = MyMusicFragment.newInstance(); } return myMusicFragment; }else if(position==1){ if(netMusicFragment==null){ netMusicFragment = NetMusicFragment.newInstance(); } return netMusicFragment; } return null; }

二、MyMusicFragment

從演示可以看到,「我的」頁面比影片多了個跳轉頁,歌曲加載並非在 MyMusicFragment 中實現,而是透過點擊跳轉,其實就是多了一個頁面。

MyMusicFragment.xml:

 <relativelayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@drawable/skin2">
    <relativelayout android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_marginTop="10dp"
        android:background="#10ffffff">
        <linearlayout android:id="@+id/four_module"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:layout_marginTop="20dp" android:layout_marginBottom="20dp"
            android:orientation="horizontal">
            <!-- 本地歌曲、已下載、最近播放、設定四個模組 -->
        </linearlayout>
    </relativelayout>
</relativelayout>

MyMusicFragment.java:

 public class MyMusicFragment extends Fragment implements View.OnClickListener{
    private LinearLayout layout_local_songs;
    private MainActivity mainActivity;

    public static MyMusicFragment newInstance(){
        MyMusicFragment myMusicFragment = new MyMusicFragment();
        return myMusicFragment;
    }

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.my_music_fragment, null);
        layout_local_songs = (LinearLayout) view.findViewById(R.id.ll_local_songs);
        layout_local_songs.setOnClickListener(this);
        return view;
    }

    @Override public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_local_songs:
                Intent intent = new Intent(getActivity(), LocalSongsActivity.class);
                startActivity(intent);
                break;
        }
    }
}

由於目前只實現了這些功能,畫面上的數據暫時是虛擬的,僅完成了對本地音樂列表的跳轉。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved