代码之家  ›  专栏  ›  技术社区  ›  WQYeo

内存使用突然激增

  •  0
  • WQYeo  · 技术社区  · 7 年前

    main activity enter image description here
    我在我的电脑中也使用了RecyclerView MainActivity 它使用 row_stream_songs.xml .
    我设法发现这是由上的ImageView引起的 row\u stream\u songs.xml (代码在下面进一步),但我确实需要在那里的ImageView。

    //Fetch stream song data
        SongData songData = new SongData();
        ArrayList<Song> streamSong = songData.getSongList(0);
    
        //Setup the recycler view to place
        //song data.
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.streamRecycler);
        StreamSongAdapter songAdapter = new StreamSongAdapter(this, streamSong);
        recyclerView.setAdapter(songAdapter);
    
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), linearLayoutManager.getOrientation());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(dividerItemDecoration);
    
        //When the user selects a song.
        songAdapter.setOnItemClickListener(new StreamSongAdapter.OnItemClickListener(){
            @Override
            public void onItemClick(LinearLayout b, View v, Song obj, int position){
                Intent intent = new Intent(MainActivity.this, PlayMusic.class);
                intent.putExtra("songData",obj.getSongData());
                intent.putExtra("songType", 1);
                startActivity(intent);
            }
        });
    

    我的StreamSongAdapter代码

    public class StreamSongAdapter extends RecyclerView.Adapter<StreamSongAdapter.SongHolder> {
    private ArrayList<Song> song;
    private Context context;
    
    private OnItemClickListener onItemClickListener;
    
    public StreamSongAdapter(Context _context, ArrayList<Song> _song){
        context = _context;
        song = _song;
    }
    
    public interface OnItemClickListener{
        void onItemClick(LinearLayout b, View v, Song obj, int position);
    }
    
    public void setOnItemClickListener(final OnItemClickListener _onItemClickListener){
        onItemClickListener = _onItemClickListener;
    }
    
    @Override
    public StreamSongAdapter.SongHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View myView = LayoutInflater.from(context).inflate(R.layout.row_stream_songs, parent, false);
        return new SongHolder(myView);
    }
    
    //Adds the row layout to the Recycler view.
    @Override
    public void onBindViewHolder(final StreamSongAdapter.SongHolder holder, final int position) {
    
        holder.songName.setText(song.get(position).getSongName());
        int resId = AppUtil.getImageIdFromDrawable(context, song.get(position).getSongImage());
        holder.songImage.setImageResource(resId);
        holder.artistName.setText(song.get(position).getSongArtist());
    
        //Handle song selection
        holder.linearLayout.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                if (onItemClickListener != null){
                    onItemClickListener.onItemClick(holder.linearLayout, v, song.get(position), position);
                }
            }
        });
    }
    @Override
    public int getItemCount() {
        return song.size();
    }
    
    //Gets data for each view in the recyclerview.
    class SongHolder extends RecyclerView.ViewHolder{
        TextView songName , artistName;
        ImageView songImage;
        LinearLayout linearLayout;
    
        SongHolder(View itemView) {
            super(itemView);
            artistName = (TextView) itemView.findViewById(R.id.streamSongArtist);
            songName = (TextView) itemView.findViewById(R.id.streamSongTitle);
            songImage = (ImageView) itemView.findViewById(R.id.streamSongImage);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.rowMusicSelect);
        }
    }
    }
    

    ( Song 是一个类,用于存储songId、songTitle、歌曲艺术家的姓名、drawable的图像名称、songURL、songDuration和一个布尔值,该布尔值表示歌曲是否要在线播放。)

        <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context="sg.edu.tp.melodia.Activities.MainActivity"
        tools:showIn="@layout/app_bar_main">
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/streamRecycler"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:layout_editor_absoluteX="8dp"
                android:layout_marginTop="60dp"
                tools:layout_editor_absoluteY="8dp" />
    </RelativeLayout>
    

        <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:id="@+id/rowMusicSelect"
            android:layout_height="wrap_content"
            android:padding="20dp"
            android:background="@drawable/clickable1"
            android:orientation="horizontal">
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">
    
                <TextView
                    android:id="@+id/streamSongTitle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="16sp"
                    android:text="TextView" />
    
                <TextView
                    android:id="@+id/streamSongArtist"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:text="TextView" />
            </LinearLayout>
    
            <ImageView
                android:id="@+id/streamSongImage"
                android:layout_width="45dp"
                android:layout_height="45dp"
                android:scaleType="centerCrop"
                app:srcCompat="@mipmap/ic_launcher" />
        </LinearLayout>
    
    </android.support.v7.widget.CardView>
    

    如前所述,我发现每当我在 row\u stream\u songs.xml ,内存和CPU使用率将下降到正常水平。但我需要图像视图。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Shashank Udupa    7 年前

    不要在UI线程上设置图像。使用图像加载库,如 Glide Picasso 在后台线程上加载图像,这样主线程就不会被阻塞