Android 如何在Recyclerview上实现SharedReference,以便保存收藏夹列表以便于使用

Android 如何在Recyclerview上实现SharedReference,以便保存收藏夹列表以便于使用,android,android-recyclerview,sharedpreferences,Android,Android Recyclerview,Sharedpreferences,A构建了歌曲列表,单击列表中的某个项目时,会将您带到新活动1,其中会显示完整的歌曲歌词。我将数据存储在String.xml中。所以,通过适配器从字符串传递此数据,并将其显示在NewActivity1中。为此,我使用了Recyclerview。我发现在SharedReference中很难实现这一点。 我希望用户能够从NewActivity1向SharedReference添加歌曲。因为这是用户阅读歌曲完整歌词的地方 我需要你的帮助。我真的不知道怎么开始。如果要将fav_图标放在row.xml中,我

A构建了歌曲列表,单击列表中的某个项目时,会将您带到新活动1,其中会显示完整的歌曲歌词。我将数据存储在String.xml中。所以,通过适配器从字符串传递此数据,并将其显示在NewActivity1中。为此,我使用了Recyclerview。我发现在SharedReference中很难实现这一点。 我希望用户能够从NewActivity1向SharedReference添加歌曲。因为这是用户阅读歌曲完整歌词的地方

我需要你的帮助。我真的不知道怎么开始。如果要将fav_图标放在row.xml中,我可能会设法对其进行配置

这里是activity\u tips1.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:background="@drawable/star_wars"
    tools:context=".Tips_1">



    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/scrollbar_shape_style"
        android:layout_marginBottom="60dp"

        android:id="@+id/recyclerview1">


    </androidx.recyclerview.widget.RecyclerView>
    

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_marginTop="5dp"
    tools:context=".NewActivity1">

    <RelativeLayout
        android:id="@+id/layout_default"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/txt1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="30dp"
                android:layout_marginLeft="30dp"
                android:textSize="10sp"
                android:text="Play song here\n\t\t\there\t\t➝"
                android:textColor="#8c0b0b" />
        </LinearLayout>

        <ImageView
            android:id="@+id/img1"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:onClick="play"
            android:layout_marginLeft="190dp"
            android:src="@drawable/ic_play_circle_outline_black_24dp"
            />

        <ImageView
            android:id="@+id/img2"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:onClick="pause"
            android:layout_toRightOf="@id/img1"
            android:layout_marginHorizontal="30dp"
            android:src="@drawable/ic_pause_circle_outline_black_24dp" />

        <SeekBar
            android:id="@+id/seekBar1"
            android:layout_marginTop="40dp"
            android:layout_width="200dp"
            android:background="#565858"
            android:layout_height="wrap_content"
            android:layout_marginLeft="145dp"
            android:indeterminate="false" />

    </RelativeLayout>


    <View
        android:id="@+id/viewera"
        android:layout_marginTop="6dp"
        android:layout_below="@id/layout_default"
        android:layout_width="match_parent"
        android:layout_height="8dp"
        android:background="#01645A" />


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/viewera"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="60dp"
        style="@style/scrollbar_shape_style">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_margin="18dp">


            <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fontFamily="sans-serif-condensed"
                android:text="Full lyrics details"
                android:textColor="#000"
                android:textIsSelectable="true"
                android:textSize="21sp" />
        </LinearLayout>
    </ScrollView>

    <ImageButton
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:text="How are you"
        android:textColor="#000"
        android:textSize="18sp"
        android:layout_marginBottom="60dp"
        android:layout_alignParentEnd="true"
        android:src="@drawable/ic_heart_o"
        android:layout_alignParentBottom="true"
       />



</RelativeLayout>
<?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="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"
    android:padding="10dp">

    <ImageView
        android:id="@+id/mainIcon"
        android:src="@drawable/song"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="0.1"/>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_weight="0.8"
        android:layout_marginLeft="5dp"
        android:layout_marginStart="5dp">

        <TextView
            android:id="@+id/mainTitle"
            android:textStyle="bold"
            android:textColor="#740303"
            android:text="Title"
            android:textSize="15sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/mainDesc"
            android:text="Description"
            android:textColor="#262626"
            android:textSize="12sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


    </LinearLayout>
<ImageView
    android:id="@+id/favorite"
    android:layout_width="0dp"
    android:layout_height="30dp"
    android:layout_weight="0.1"
    android:src="@drawable/ic_heart_o"/>
</LinearLayout>

您必须在适配器中创建arraylist,就像您需要定义数组的数据类型一样,例如
公共arraylist SelectedImage
然后在BindView上,然后在fav图标上,像这样单击listener

myViewHolder.favBtn.setOnClickListener(new View.OnClickListener() {
            if (selectedImages.contains(images.get(i))) {
                myViewHolder.mylinear.setBackgroundColor(Color.WHITE);
                selectedImages.remove(images.get(i));
            } else {
                myViewHolder.favBtn.setBackgroundColor(Color.BLACK);
                selectedImages.add(images.get(i)); //this is the list which you passing to the adapter
                Log.e("selected",selectedImages.toString());
            }
            return true;
    });
public ArrayList<String> getSelectedImages() {
    Log.e("selected",selectedImages.toString());
    return selectedImages;
}
ImageAdapter applicationimagesadapter;
然后为在适配器中创建的数组列表创建getter函数,如下所示

myViewHolder.favBtn.setOnClickListener(new View.OnClickListener() {
            if (selectedImages.contains(images.get(i))) {
                myViewHolder.mylinear.setBackgroundColor(Color.WHITE);
                selectedImages.remove(images.get(i));
            } else {
                myViewHolder.favBtn.setBackgroundColor(Color.BLACK);
                selectedImages.add(images.get(i)); //this is the list which you passing to the adapter
                Log.e("selected",selectedImages.toString());
            }
            return true;
    });
public ArrayList<String> getSelectedImages() {
    Log.e("selected",selectedImages.toString());
    return selectedImages;
}
ImageAdapter applicationimagesadapter;
然后创建数组列表以保存适配器getter函数的结果

 ArrayList<String> images;

也就是说

您必须在适配器中创建arraylist,就像您需要定义数组的数据类型一样,例如
公共arraylist SelectedImage
然后在BindView上,然后在fav图标上,像这样单击listener

myViewHolder.favBtn.setOnClickListener(new View.OnClickListener() {
            if (selectedImages.contains(images.get(i))) {
                myViewHolder.mylinear.setBackgroundColor(Color.WHITE);
                selectedImages.remove(images.get(i));
            } else {
                myViewHolder.favBtn.setBackgroundColor(Color.BLACK);
                selectedImages.add(images.get(i)); //this is the list which you passing to the adapter
                Log.e("selected",selectedImages.toString());
            }
            return true;
    });
public ArrayList<String> getSelectedImages() {
    Log.e("selected",selectedImages.toString());
    return selectedImages;
}
ImageAdapter applicationimagesadapter;
然后为在适配器中创建的数组列表创建getter函数,如下所示

myViewHolder.favBtn.setOnClickListener(new View.OnClickListener() {
            if (selectedImages.contains(images.get(i))) {
                myViewHolder.mylinear.setBackgroundColor(Color.WHITE);
                selectedImages.remove(images.get(i));
            } else {
                myViewHolder.favBtn.setBackgroundColor(Color.BLACK);
                selectedImages.add(images.get(i)); //this is the list which you passing to the adapter
                Log.e("selected",selectedImages.toString());
            }
            return true;
    });
public ArrayList<String> getSelectedImages() {
    Log.e("selected",selectedImages.toString());
    return selectedImages;
}
ImageAdapter applicationimagesadapter;
然后创建数组列表以保存适配器getter函数的结果

 ArrayList<String> images;

就是这样

是的,您可以将fav图标放在行中,然后在fav上单击将歌曲的所有细节放入共享首选项中,然后在需要的位置使用单击您必须更改图标颜色,将其放置在共享首选项中,然后再次单击从中删除并更改图标颜色fav btn单击这一切都发生在Adapter中您可以将fav图标放置在行中,然后在fav单击将歌曲的所有细节放入共享首选项中,然后在单击时使用您需要更改的位置图标颜色,将其放置在共享首选项中,然后在另一个屏幕上单击“从中删除”并更改图标颜色fav btn上您想要什么单击适配器中发生的一切