我是第一次问问题,请解决我的问题。我正在开发小型音乐播放器应用程序。因为我面临一个愚蠢的问题,我在谷歌上搜索了很多,但它仍然是。
我正在使用 listfragment 显示歌曲列表。现在的问题是,当我将列表项背景设置为透明时,它可以工作。但是一旦我滚动列表,列表背景就会更改为系统默认背景。
我是android新手,请帮助我...
我已经在我的布局文件中尝试了这些:
android:background="@android:color/transparent"
android:cacheColorHint="#00000000"
我的主要活动代码是:
package com.android.player;
import in.wptrafficanalyzer.viewpagerdemo.R;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/** Getting a reference to the ViewPager defined the layout file */
ViewPager pager = (ViewPager) findViewById(R.id.pager);
/** Getting fragment manager */
FragmentManager fm = getSupportFragmentManager();
/** Instantiating FragmentPagerAdapter */
MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);
/** Setting the pagerAdapter to the pager object */
pager.setAdapter(pagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
/ * ** * ** * ** * ** * ** * ** * ** * ** * ** * *** 这是我的 SongsList.java 代码:-
package com.android.player;
import in.wptrafficanalyzer.viewpagerdemo.R;
import java.io.FileDescriptor;
import java.math.BigDecimal;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class SongsList extends ListFragment{
int mCurrentPage;
private MediaCursorAdapter mediaAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Getting the arguments to the Bundle object */
Bundle data = getArguments();
/** Getting integer data of the key current_page from the bundle */
mCurrentPage = data.getInt("current_page", 0);
String sortOrder = MediaStore.MediaColumns.DISPLAY_NAME + " ASC";
Cursor cursor = getActivity().getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null,sortOrder);
if(null != cursor)
{
mediaAdapter = new MediaCursorAdapter(getActivity(), R.layout.listitem, cursor);
setListAdapter(mediaAdapter);
}
}
public Bitmap getAlbumart(Long album_id)
{
Bitmap bm = null;
try
{
final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
//album_id = ()album_id;
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
ParcelFileDescriptor pfd = getActivity().getContentResolver().openFileDescriptor(uri, "r");
if (pfd != null)
{
FileDescriptor fd = pfd.getFileDescriptor();
bm = BitmapFactory.decodeFileDescriptor(fd);
}
} catch (Exception e) {
}
return bm;
}
public class MediaCursorAdapter extends SimpleCursorAdapter{
@SuppressWarnings("deprecation")
public MediaCursorAdapter(Context context, int layout, Cursor c) {
super(context, layout, c,new String[] { MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.TITLE, MediaStore.Audio.AudioColumns.DURATION},
new int[] { R.id.displayname, R.id.title, R.id.duration });
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView title = (TextView)view.findViewById(R.id.title);
TextView name = (TextView)view.findViewById(R.id.displayname);
TextView duration = (TextView)view.findViewById(R.id.duration);
ImageView imageView1 = (ImageView)view.findViewById(R.id.imageView1);
Bitmap bm = getAlbumart(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.ALBUM_ID)));
if(bm!=null)
{
imageView1.setImageBitmap(bm);
}
else
{
imageView1.setImageResource(R.drawable.ic_launcher);
}
name.setText(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)));
title.setText(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.TITLE)));
long durationInMs = Long.parseLong(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION)));
int seconds = (int) (durationInMs / 1000) % 60 ;
int minutes = (int) ((durationInMs / (1000*60)) % 60);
int hours = (int) ((durationInMs / (1000*60*60)) % 24);
double durationInMin = ((double)durationInMs/1000.0)/60.0;
String durationTxt = "";
if(hours==0)
{
if(minutes<10)
{
durationTxt = "0"+minutes+":"+seconds;
}
else
{
durationTxt = minutes+":"+seconds;
}
}
else
{
durationTxt = hours+":"+"0"+minutes+":"+seconds;
}
durationInMin = new BigDecimal(Double.toString(durationInMin)).setScale(2, BigDecimal.ROUND_UP).doubleValue();
duration.setText(durationTxt);
view.setTag(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)));
}
}
public void onActivityCreated(View view, Bundle savedInstanceState) {
getListView().setCacheColorHint(android.R.color.transparent);
super.onViewCreated(view, savedInstanceState);
}
}
/ * ** * ** * ** * ** * ** * ** * ** * ** * **
这是activity_mail.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/sparkle">
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:textColor="#fff"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
</android.support.v4.view.ViewPager>
</RelativeLayout>
/ * ** * ** * ** * ** * ** * ** * ** * ** * ** * * 这里是listitem.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:cacheColorHint="#00000000"
android:orientation="horizontal"
android:padding="5dip"
android:id="@+id/list_item" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:background="@drawable/transparent_bg"
android:cacheColorHint="#00000000"
android:layout_marginRight="5dip">
<ImageView
android:id="@+id/imageView1"
android:layout_width="60dip"
android:layout_height="60dip"
android:src="@drawable/ic_launcher"/>
</LinearLayout>
<!-- Title Of Song-->
<TextView
android:id="@+id/displayname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:textColor="#fff"
android:typeface="sans"
android:textSize="15dip"
android:textStyle="bold"/>
<!-- Artist Name -->
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/displayname"
android:textColor="#fff"
android:textSize="10dip"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/thumbnail"
android:singleLine="true" />
<!-- Rightend Duration -->
<TextView
android:id="@+id/duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/displayname"
android:gravity="right"
android:layout_marginRight="5dip"
android:textSize="12dip"
android:textColor="#fff"
android:textStyle="bold"/>
</RelativeLayout>
请看一下并帮助我。提前致谢。