tại code mình nó đơn giản như link:
Mình thiên về lỗi ở phần layout. Đây là code phần layout
File activity_main được inflate ở MainActivity :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:id="@+id/my_toolbar"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
>
</android.support.v7.widget.Toolbar>
<fragment
android:id="@+id/fragment_container"
class="app.phucpc.htphuc.mymp3.MainFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"></fragment>
</LinearLayout>
File fragment_main được inflate ở MainFragment :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
>
<ListView
android:id="@+id/song_list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</FrameLayout>
File song_item được inflate ở trong MusicAdapter:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="10dp">
<TextView
android:id="@+id/song_title"
android:textAppearance="?android:textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"/>
<TextView
android:id="@+id/song_artist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Artist"/>
</LinearLayout>
Code của MainActivity :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
transaction = getFragmentManager().beginTransaction();
mainF = new MainFragment();
PlayF = new PlaybackFragment();
transaction.add(R.id.fragment_container, mainF);
transaction.commit();
<another useless code>
}
onCreateView ở MainFragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
listView = (ListView) rootView.findViewById(R.id.song_list_item);
musicAdapter = new MusicAdapter(getActivity(),null,0);
listView.setAdapter(musicAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor = (Cursor) parent.getItemAtPosition(position);
int path = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);
((Callback) getActivity()).onItemSelected(Uri.parse(cursor.getString(path)));
}
});
return rootView;
}
Và MusicAdapter:
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
layoutInflater = LayoutInflater.from(context);
View rootView = (View) LayoutInflater.from(context).inflate(R.layout.song_item,parent,false);
ViewHolder viewHolder = new ViewHolder(rootView);
rootView.setTag(viewHolder);
return rootView;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder viewHolder = (ViewHolder) view.getTag();
viewHolder.tv_title.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));
viewHolder.tv_artist.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)));
}
public static class ViewHolder{
public final TextView tv_title;
public final TextView tv_artist;
public ViewHolder(View view){
tv_title = (TextView) view.findViewById(R.id.song_title);
tv_artist = (TextView) view.findViewById(R.id.song_artist);
}
}
Mình chỉ đưa ra một số code liên quan tới việc hiển thị ListView ( theo suy nghĩ của mình là vậy :smile: ). Nếu đưa hết code lên dài dòng lắm. Mọi người giúp mình với.