編輯:關於Android編程
package com.iwanghang.drmplayer; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.os.AsyncTask; 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.View.OnClickListener; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import com.iwanghang.drmplayer.adapter.MyMusicListAdapter; import com.iwanghang.drmplayer.utils.Constant; import com.iwanghang.drmplayer.vo.Mp3Info; import com.iwanghang.drmplayer.vo.SearchResult; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; import java.io.IOException; import java.util.ArrayList; public class NetMusicListFragment extends Fragment implements OnItemClickListener,OnClickListener { private ListView listView_net_music; private ArrayListmp3Infos; private LinearLayout load_layout; private LinearLayout ll_search_btn_container; private LinearLayout ll_search_container; private ImageButton ib_search_btn; private EditText et_search_content; private ArrayList searchResults = new ArrayList (); private int page = 1;//搜索音樂的頁碼 private MainActivity mainActivity; //private boolean isPause = false;//歌曲播放中的暫停狀態 private int position = 0;//當前播放的位置,提供給PlayActivity //onAttach(),當fragment被綁定到activity時被調用(Activity會被傳入.). @Override public void onAttach(Context context) { super.onAttach(context); //mainActivity = (MainActivity) context; mainActivity = (MainActivity) getActivity(); } public static NetMusicListFragment newInstance() { NetMusicListFragment net = new NetMusicListFragment(); return net; } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //UI組件初始化 View view = inflater.inflate(R.layout.net_music_list_layout,null); //item listView_net_music = (ListView) view.findViewById(R.id.listView_net_music); //findViewById load_layout = (LinearLayout) view.findViewById(R.id.load_layout); ll_search_btn_container = (LinearLayout) view.findViewById(R.id.ll_search_btn_container); ll_search_container = (LinearLayout) view.findViewById(R.id.ll_search_container); ib_search_btn = (ImageButton) view.findViewById(R.id.ib_search_btn); et_search_content = (EditText) view.findViewById(R.id.et_search_content); //Item點擊事件監聽 listView_net_music.setOnItemClickListener(this); //按鈕點擊事件監聽 ll_search_btn_container.setOnClickListener(this); ib_search_btn.setOnClickListener(this); loadNetData();//加載網絡音樂 return view; } private void loadNetData() { load_layout.setVisibility(View.VISIBLE); //加載網絡音樂的異步任務 new LoadNetDataTask().execute(Constant.BAIDU_URL + Constant.BAIDU_DAYHOT); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.ll_search_btn_container: ll_search_btn_container.setVisibility(View.GONE); ll_search_container.setVisibility(View.VISIBLE); break; case R.id.ib_search_btn: //搜索事件 searchMusic(); break; } } //搜索音樂 private void searchMusic() { } //加載網絡音樂的異步任務 class LoadNetDataTask extends AsyncTask { @Override protected void onPreExecute() { super.onPreExecute(); load_layout.setVisibility(View.VISIBLE); listView_net_music.setVisibility(View.GONE); searchResults.clear(); } @Override protected Integer doInBackground(String... params) { String url = params[0]; try { //使用Jsoup組件請求網絡,並解析音樂數據 Document doc = Jsoup.connect(url).userAgent(Constant.USER_AGENT).timeout(6*1000).get(); Elements songTitles = doc.select("span.song-title"); Elements artists = doc.select("span.author_list"); System.out.println(artists); for (int i=0;i parent, View view, int position, long id) { } }
<!--?xml version="1.0" encoding="utf-8"?--> <linearlayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" android:paddingleft="5dp" android:paddingright="5dp" android:paddingtop="5dp" xmlns:android="http://schemas.android.com/apk/res/android"> <linearlayout android:background="@null" android:id="@+id/ll_search_btn_container" android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical" android:padding="5dp"> <textview android:drawableleft="@drawable/search_button" android:id="@+id/tv_search_show" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="搜索" android:textcolor="#FF666666"> </textview></linearlayout> <linearlayout android:background="@null" android:id="@+id/ll_search_container" android:layout_height="wrap_content" android:layout_marginleft="5dp" android:layout_marginright="5dp" android:layout_width="match_parent" android:orientation="horizontal" android:padding="5dp" android:visibility="gone"> <edittext android:background="@null" android:hint="搜索" android:id="@+id/et_search_content" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="0dip" android:padding="2dp" android:textcolor="#FF666666"> <imagebutton android:background="@null" android:id="@+id/ib_search_btn" android:layout_height="match_parent" android:layout_width="wrap_content" android:padding="0dp" android:scaletype="centerInside" android:src="@drawable/search_button"> </imagebutton></edittext></linearlayout> <linearlayout android:gravity="center" android:id="@+id/load_layout" android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical" android:visibility="gone"> <progressbar android:gravity="center" android:id="@+id/pb_search_wait" android:layout_gravity="center" android:layout_height="match_parent" android:layout_width="wrap_content" style="?android:attr/progressBarStyle"> <textview android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="正在努力搜索中" android:textcolor="#8b8a8a" android:textsize="10sp"> </textview></progressbar></linearlayout> <listview android:cachecolorhint="@android:color/transparent" android:id="@+id/listView_net_music" android:layout_height="wrap_content" android:layout_width="match_parent" android:listselector="@android:color/transparent" android:overscrollmode="never" android:scrollbars="none" android:visibility="gone"> </listview></linearlayout>
SearchResult如下:
package com.iwanghang.drmplayer.vo; /** * Created by iwanghang on 16/4/29. * 搜索音樂的對象 */ public class SearchResult { private String musicName; private String url; private String artist; private String album; //set方法,get方法 public String getMusicName() { return musicName; } public void setMusicName(String musicName) { this.musicName = musicName; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getArtist() { return artist; } public void setArtist(String artist) { this.artist = artist; } public String getAlbum() { return album; } public void setAlbum(String album) { this.album = album; } @Override public String toString() { return "SearchResult{" + "musicName='" + musicName + '\'' + ", url='" + url + '\'' + ", artist='" + artist + '\'' + ", album='" + album + '\'' + '}'; } }
在Android中,除了我們之前說過的sharedpreference可以用來保存數據之外,SQLiteDatabase應該是使用得最廣泛的一種數據保存方式。 SQLit
最近又有大片上映了,前幾天剛看完《末日崩塌》,《侏羅紀世界》又來了,對於大片迷來說是一種福利,所以這幾天手機上裝了各種電影票團購軟件,沒辦法,同樣的電影同樣的電影院同樣的
小編一直任務將web和android組件結合起來做應用可以事半功倍,html5一來就更有說服力了,特別是對於以前從事web開發的兄弟來說 1. webview加入布局文件
做過Android開發都會陸續用到這個開源庫EventBus。EventBus是一款針對Android優化的發布/訂閱事件總線。主要功能是替代Intent,Handler