如何实现像这样的视差GridLayout?

时间:2017-10-25 05:51:38

标签: android gridview android-recyclerview parallax

我想与parallax经理(GridLayout)实施RecyclerView效果。我尝试使用各种库,但没有使用它们。

我想要的是:我想在ImageView的标题上显示TextViewGridLayout,如GIF所示,它应该显示视差效果。

我能得到的最好:使用这个库https://github.com/kanytu/android-parallax-recyclerview,我可以实现视差效果,但标题的文本和图像是硬编码的(我想根据recyclerView项目更改它们)。

ArtistSong.java:

public class ArtistSong extends Activity {

RecyclerView recyclerView_artistSong;

ArrayList<ArtistSongModel> ArtistSongList = new ArrayList<>();





@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_artistsong);

    recyclerView_artistSong = findViewById(R.id.recyclerView_artistSong);


    GridLayoutManager arGridLayoutManager = new GridLayoutManager(getApplicationContext(), 2);

    arGridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            return position == 0 ? 2:1;
        }
    });

    recyclerView_artistSong.setLayoutManager(arGridLayoutManager);
    recyclerView_artistSong.setHasFixedSize(true);





    Long ArtistId = getIntent().getExtras().getLong("artist_id");




    Uri uri = MediaStore.Audio.Artists.Albums.getContentUri("external",ArtistId);

    String[] projection ={MediaStore.Audio.Albums.ALBUM, MediaStore.Audio.Albums.ARTIST, MediaStore.Audio.Albums._ID };

    Cursor cursor = getApplicationContext().getContentResolver().query(uri, projection, null, null, null);
    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do {

                String AlbumName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM));
                String AlbumArtistName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ARTIST));
                Long AlbumID = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Albums._ID));
                Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
                Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, AlbumID);




                    ArtistSongModel s = new ArtistSongModel(AlbumArtistName, AlbumName, AlbumID, albumArtUri);
                    ArtistSongList.add(s);



            } while (cursor.moveToNext());
        }
        cursor.close();
    }

   final ParallaxRecyclerAdapter<ArtistSongModel> madapter = new ParallaxRecyclerAdapter<ArtistSongModel>(ArtistSongList) {
       @Override
       public void onBindViewHolderImpl(final RecyclerView.ViewHolder viewHolder, ParallaxRecyclerAdapter<ArtistSongModel> parallaxRecyclerAdapter, int i) {


           final ArtistSongModel artistSongModel = ArtistSongList.get(i);



           ((SimpleViewHolder)viewHolder).tv_albumName.setText(artistSongModel.getAlbumName());
           ((SimpleViewHolder)viewHolder).tv_artistName.setText(artistSongModel.getArtistName());

           Picasso.with(getApplicationContext()).load(artistSongModel.getAlbumArt()).placeholder(R.drawable.ic_launcher).into(((SimpleViewHolder)viewHolder).iv_albumArt);



       }

       @Override
       public RecyclerView.ViewHolder onCreateViewHolderImpl(ViewGroup viewGroup, ParallaxRecyclerAdapter<ArtistSongModel> parallaxRecyclerAdapter, int i) {
           return new SimpleViewHolder(getLayoutInflater().inflate(R.layout.row_artistsong, viewGroup, false));
       }

       @Override
       public int getItemCountImpl(ParallaxRecyclerAdapter<ArtistSongModel> parallaxRecyclerAdapter) {
           return ArtistSongList.size();
       }


   };




    madapter.setParallaxHeader(getLayoutInflater().inflate(R.layout.my_header, recyclerView_artistSong, false), recyclerView_artistSong);


    recyclerView_artistSong.setAdapter(madapter);

  }

static class SimpleViewHolder extends RecyclerView.ViewHolder {

    TextView tv_albumName ;
    TextView tv_artistName ;
    ImageView iv_albumArt;

    public SimpleViewHolder(View itemView) {
        super(itemView);

        tv_albumName = itemView.findViewById(R.id.tv_albumName);
        tv_artistName = itemView.findViewById(R.id.tv_artistName);
        iv_albumArt = itemView.findViewById(R.id.iv_albumArt);
    }
}
}

activity_artist.xml :( ArtistSong.java的xml文件:)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>



<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView_artistSong"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    android:scrollbarSize="10dp">


</android.support.v7.widget.RecyclerView>

row_artist.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="210dp">


<ImageView
    android:id="@+id/iv_albumArt"
    android:layout_width="match_parent"
    android:layout_height="210dp"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop"
    android:src="@drawable/party"/>

<TextView
    android:id="@+id/tv_artistName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:text="Hello2"
    android:textColor="#ffffff"
    android:textSize="12sp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="#8c000000"/>

<TextView
    android:id="@+id/tv_albumName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:text="Hello1"
    android:textColor="#ffffff"
    android:textSize="14sp"
    android:layout_above="@+id/tv_artistName"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="#8c000000"/>

这是标题:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>


<ImageView
    android:id="@+id/imageView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop"
    android:src="@drawable/party" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:textColor="#ffffff"
    android:background="#54000000"
    android:text="Take a Chill pill"

    android:layout_alignBottom="@+id/imageView2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

enter image description here

0 个答案:

没有答案