Android 为什么这个自定义列表视图扩展游标适配器滞后?

Android 为什么这个自定义列表视图扩展游标适配器滞后?,android,listview,android-cursoradapter,custom-lists,Android,Listview,Android Cursoradapter,Custom Lists,这就是自定义列表视图的实现方式。此列表视图具有图像视图和文本视图 public class TracksActivityFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> { // Songs list public ArrayList<HashMap<String, String>> songsList = new ArrayList&

这就是自定义
列表视图的实现方式。此
列表视图
具有
图像视图
文本视图

public class TracksActivityFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> 
{
    // Songs list
    public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
    ListView lv;
    public SongsManager manager = new SongsManager();    

    SongsAdapter mAdapter;
    ListView lc;
    private static final String ARG_POSITION = "position";

    private int position;

    public static TracksActivityFragment newInstance(int position) 
    {
        TracksActivityFragment f = new TracksActivityFragment();
        Bundle b = new Bundle();
        b.putInt(ARG_POSITION, position);
        f.setArguments(b);
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_tracks_activity, container, false);

        return v;

    }@Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);

        mAdapter = new SongsAdapter(getActivity(), null);
        setListAdapter(mAdapter);
        getLoaderManager().initLoader(0, null, this);
    }

    static final String[] SONGS_SUMMARY_PROJECTION = { MediaStore.Audio.Media._ID,MediaStore.Audio.Media.ALBUM_ID,MediaStore.Audio.Media.TITLE};


    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        String select = null;
        return new CursorLoader(getActivity(), MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,SONGS_SUMMARY_PROJECTION, select, null, MediaStore.Audio.Media.TITLE + " ASC");
    }

    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        mAdapter.swapCursor(data);
    }

    public void onLoaderReset(Loader<Cursor> loader) {
        mAdapter.swapCursor(null);
    }

    @Override
    public void onResume() {
        //onResume happens after onStart and onActivityCreate
        lv = getListView();
        setListAdapter(mAdapter);
        registerForContextMenu(lv);
        getLoaderManager().initLoader(0, null, this);
        //final String[] F= new String[size];
        ArrayList<String> F = new ArrayList<String>();
        Cursor c= getActivity().getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,SONGS_SUMMARY_PROJECTION, null, null, MediaStore.Audio.Media.TITLE + " ASC");
        //String[] Song  = new String[size];
        int i=0;
        if (c.moveToFirst()) {
            do {
                F.add(c.getString(c.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)));//MediaStore.Audio.Media.TITLE;
                //Toast.makeText(getApplicationContext(),F[i] +"   ", Toast.LENGTH_LONG).show();
                i++;
            } while (c.moveToNext());
        }
        final int size = F.size();//mAdapter.getCount();
        final int Q[] = new int[size];

        for(int j=0;j<size;j++){
            Q[j] = manager.getSongIndex(F.get(j),getContext());
        }

        // listening to single listitem click
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

//Another way around to play a song
// getting listitem index
                TextView textView = (TextView) view.findViewById(R.id.title);

                // Starting new intent
                Intent i = new Intent(getContext(), MusicPlayerActivity.class);
                Log.d("TAG", "onItemClick");
                //// Sending songIndex to PlayerActivity
                i.putExtra("size",size);
                i.putExtra("queue",Q);
                i.putExtra("filtered",true);
                //  Toast.makeText(getApplicationContext(), "Qi:" + Q[0], Toast.LENGTH_LONG).show();
                i.putExtra("start", position);
                startActivityForResult(i, 7);

            }
            //Another way around
        });
         lc= lv;
        super.onResume() ;
    }
}
它本身“有效”,但它滞后。我认为这是由于背景图像的缩放,然后我在删除背景图像后运行它,但它仍然滞后


你能建议我怎么做吗?

你的
getAlbumArt
方法直接创建位图,你应该使用像Glideso这样的图像加载程序库,而不使用
getAlbumArt
,它仍然滞后吗?EpicpandaForce我照你说的做了。这会让它稍微好一点,但不会好很多@pskink它在我的手机中不再落后,但在只有1GB的手机中仍然落后ram@EpicPandaForce现在很晚了,但谢谢你。它现在已经足够好了,:)你的
getAlbumArt
方法直接创建位图,你应该使用像Glideso一样的图像加载程序库,而不使用
getAlbumArt
,它仍然滞后吗?EpicpandaForce我按照你说的做了。这会让它稍微好一点,但不会好很多@pskink它在我的手机中不再落后,但在只有1GB的手机中仍然落后ram@EpicPandaForce现在很晚了,但谢谢你。现在已经足够好了,:)
public class SongsAdapter extends CursorAdapter {
    private final LayoutInflater nInflater;

    public SongsAdapter(Context context, Cursor c) {
        super(context, c);
        nInflater = LayoutInflater.from(context);
    }
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
//        cursor.moveToFirst();
        TextView songTitle = (TextView) view.findViewById(R.id.title);
        songTitle.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));
        //TextView artist = (TextView) view.findViewById(R.id.songArtist);
        //artist.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)));

        ImageView albumArt = (ImageView) view.findViewById(R.id.icon);
        albumArt.setScaleType(ImageView.ScaleType.FIT_XY);

        Long albumId = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));

        Bitmap img = getAlbumart(context,albumId);
        if(img != null)
            albumArt.setImageBitmap(img);
        else{
            Bitmap def = getDefaultAlbumArt(context);
            albumArt.setImageBitmap(def);

        }

    }


    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        final View view = nInflater.inflate(R.layout.custom_list, parent, false);
        return view;
    }

    public Bitmap getAlbumart(Context context, Long album_id)
    {
        Bitmap bm = null;
        try
        {
            final Uri sArtworkUri = Uri
                    .parse("content://media/external/audio/albumart");

            Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);

            ParcelFileDescriptor pfd = context.getContentResolver()
                    .openFileDescriptor(uri, "r");

            if (pfd != null)
            {
                FileDescriptor fd = pfd.getFileDescriptor();
                bm = BitmapFactory.decodeFileDescriptor(fd);
            }
        } catch (Exception e) {
        }
        return bm;
    }


    public Bitmap getDefaultAlbumArt(Context context) {
        Bitmap bm = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        try {
            bm = BitmapFactory.decodeResource(context.getResources(),
                    R.drawable.default_art, options);
        } catch (Error ee) {
        } catch (Exception e) {
        }
        return bm;
    }    
}