前言:
小白的我在经历了两周的Android Studio实训课的学习后,编辑了简易音乐播放器的半成品,通过两周的学习让我对打码又激起了兴趣,很感谢实训老师江哥(博客ID:3052world)的悉心教导,下面我将在我两周学习分享给大家。
正文:
- 建立Activity,给它布局。
下面是我的布局代码:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:background="#fd3131">
<TextView
android:id="@+id/main_local_tv"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="本地音乐"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="#000"
android:gravity="center" />
<TextView
android:id="@+id/main_online_tv"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="在线音乐"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="#000"
android:gravity="center" />
</LinearLayout>
//ViewPager是使视图滑动。
<android.support.v4.view.ViewPager
android:id="@+id/main_vp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
如图示:
2.建立Fragment,布局
为了实现滑动,这里建立了两个Fragment,LocalFragment和OnlineFragment。
LocalFragment里的布局代码为:
//
<ListView
android:id="@+id/local_listview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
图示:
OnlineFragment里放置一个文本就行。
3.给LocalFragment建立适配器adapter(MusicListAdapter和)和实体类entity(Music)。
适配器MusicListAdapter代码:
public class MusicListAdapter extends BaseAdapter {
//继承BaseAdapter
private Context context;
private List<Music> musicList;
public MusicListAdapter(List<Music> musicList,Context context){
this.context=context;
this.musicList=musicList;
}
@Override
public int getCount() {
return musicList.size();
}
@Override
public Object getItem(int i) {
return null;
}