将多个活动转换为片段

时间:2018-11-20 23:01:52

标签: android android-studio android-fragments android-arrayadapter

我刚想到要为我的应用实现BottomNavigationView

但是问题是我如何/在哪里开始将现有活动,类和适配器转换为片段?

Here is a picture demonstrating what i have now

我的BottomNavigationView中有3个属性:主要(歌曲列表),广播(空-尚未开始)和收藏集(我的播放列表将出现在这里)

These are my java classes i want to convert to a fragment

These are my layout(XML) files

对于任何帮助,我都非常困惑,因为我非常困惑如何使用当前设置完成此任务。

这是我的CustomAdapter班:

public class ListViewAdapter extends BaseAdapter {

//Create variables
MediaPlayer mediaPlayer;
int layout;
Song currentSong;
ArrayList<Song> arrayList;
Context context;

public ListViewAdapter(int layout, ArrayList<Song> arrayList, Context 
context) {
    this.layout = layout;
    this.arrayList = arrayList;
    this.context = context;
}

private class Viewholder {
    TextView artistTxt, songNameTxt;
    ImageView playB, stopB;
}

@Override
public int getCount() {
    return arrayList.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View view, ViewGroup parent) {

    final Viewholder viewholder;

    if (view == null) {
        viewholder = new Viewholder();

        LayoutInflater layoutInflater = (LayoutInflater) 
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(layout, null);

        viewholder.artistTxt = (TextView) 
view.findViewById(R.id.artistTxt);
        viewholder.songNameTxt = (TextView) 
view.findViewById(R.id.songNameTxt);
        viewholder.playB = (ImageView) view.findViewById(R.id.playB);
        viewholder.stopB = (ImageView) view.findViewById(R.id.stopB);
        view.setTag(viewholder);
    } else {
        viewholder = (Viewholder) view.getTag();
    }

    final Song song = arrayList.get(position);

    viewholder.artistTxt.setText(song.getArtist());
    viewholder.songNameTxt.setText(song.getSongName());

    //get all songs
    mediaPlayer = MediaPlayer.create(context, song.getSong());

    viewholder.playB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (currentSong == null) {
                mediaPlayer = MediaPlayer.create(context, song.getSong());
            }

            //if mediaplayer is not null and my current song is not equal 
to the new song i clicked on
            if (mediaPlayer != null && currentSong != song) {

                //resets the mediaplayer and creates a new song from the 
position in the list
                mediaPlayer.reset();

                mediaPlayer = MediaPlayer.create(context, song.getSong());
                viewholder.playB.setImageResource(R.drawable.play_black);

                mediaPlayer.start();
                viewholder.playB.setImageResource(R.drawable.pause_black);
            } else {
                mediaPlayer.pause();
                viewholder.playB.setImageResource(R.drawable.play_black);
            }

            //check if current song is null or the newly clicked song is 
equal to my current song
            //if true then assign the newly clicked song as my CURRENT one
            //--so it doesnt play the same song for every single one
            if (currentSong == null || song != currentSong) {
                currentSong = song;
            }
        }
    });

    //stop song
    viewholder.stopB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //stops my current song and make it null
            if (currentSong != null) {
                mediaPlayer.stop();
                mediaPlayer.release();

                currentSong = null;
                viewholder.playB.setImageResource(R.drawable.play_black);
            }
        }
    });
    return view;
}

这是我的SongListActivity,在其中添加歌曲并设置适配器:

public class SongListActivity extends AppCompatActivity {

ArrayList<Song> arrayList;
ListView listView;
ListViewAdapter adapter;

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

    BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigationBar);
    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.action_main:
                    startActivity(new Intent(getApplicationContext(), SongListActivity.class));
                    break;

                case R.id.action_collection:
                    startActivity(new Intent(getApplicationContext(), CollectionActivity.class));
                    break;
            }

            return true;
        }
    });

    //Actionbar modified
    getSupportActionBar().setTitle("Song list");

    //Find my listview
    listView = (ListView) findViewById(R.id.listView);

    //create a new arraylist object
    arrayList = new ArrayList<>();

    //Add songs to my list
    arrayList.add(new Song("Beyonce", "-Formation", R.raw.beyonce_formation));
    arrayList.add(new Song("Chris Brown", "-Hope You Do", R.raw.chrisbrown_hopeyoudo));
    arrayList.add(new Song("Akon ft. Colby'O'Donis", "-Beautiful", R.raw.akon_beautiful_ft_colbyodonis_kardinaloffishall));
    arrayList.add(new Song("Akon", "-Beautiful", R.raw.akon_dontmatter));
    arrayList.add(new Song("Akon", "-Locked Up", R.raw.akon_lockedup_ft_stylesp));
    arrayList.add(new Song("Ava Max", "-Sweet but Psycho", R.raw.avamax_sweetbutpsycho));
    arrayList.add(new Song("Tupac and Biggie ft. Akon Remix", "-Ghetto", R.raw.tupacbiggieakon_ghetto));

    //Create a new adapter of my custom adapter and assign its values
    adapter = new ListViewAdapter(R.layout.listview_customlayout, arrayList, this);

    //Set my listview to my custom adapter
    listView.setAdapter(adapter);
}

}

我的CustomListViewLayout是这样的:

<?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:background="@color/white">

<ImageView
    android:id="@+id/musicLogo"
    android:layout_width="40sp"
    android:layout_height="40sp"
    android:src="@drawable/musiclogo" />

<TextView
    android:id="@+id/artistTxt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/musicLogo"
    android:text="Artist"
    android:textColor="@color/black"
    android:textSize="15sp" />

<TextView
    android:id="@+id/songNameTxt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/artistTxt"
    android:layout_toRightOf="@id/musicLogo"
    android:text="Song Name"
    android:textColor="#11ca6b" />

<ImageView
    android:id="@+id/playB"
    android:layout_width="30sp"
    android:layout_height="30sp"
    android:layout_marginTop="4sp"
    android:layout_toLeftOf="@id/stopB"
    android:src="@drawable/play_black" />

<ImageView
    android:id="@+id/stopB"
    android:layout_width="30sp"
    android:layout_height="30sp"
    android:layout_alignParentRight="true"
    android:layout_marginTop="4sp"
    android:src="@drawable/stop_black" />

 </RelativeLayout>

谢谢。

1 个答案:

答案 0 :(得分:1)

这个问题可能会根据人们的看法而产生不同的答案,例如,一个人想要务实,或者一个人想要做建筑上好,等等。

我可以建议一种快速(且容易)且不太快(但相当容易且很好)的方法来解决您的问题。

  1. 快速简便的方法;进行一个名为NavigationActivity的新活动。其中可能包括以下活动:
    • 将是您的主要启动器活动。
    • 将使用底部导航栏和一个容器来实现一个UI 子片段。现在,底部导航栏将是 BottomNavigationView,并且容器可以是ViewPager。

之后,将您现有的活动转换为片段。它应该是相当简单的。

然后主要的技巧是将这些转换后的片段用作您在上面创建的NavigationActivity的子代。

总而言之,您将现有活动直接转换为片段。然后,为它们创建一个新的父对象,在该父对象中放置它们,并使用底部导航栏为他们提供导航。

  1. 不太快,但是建议的方法;这将需要您在项目上投入更多时间,但从长远来看将是有益的。它涉及为您的应用程序投资良好的体系结构。

首先,如果您还没有,请花一些时间使自己熟悉MVP,MVVM等应用程序体系结构,并决定将其用于您的应用程序。如果您不想全部阅读,请参阅Android Arch Components

我确定一旦完成上述操作,您将拥有正确的方式来转换片段。您仍然可以使用方法(1)重构应用程序,但是您会做的更多。根据您在各个活动中容纳的业务逻辑的数量,有必要保持NavigationActivity非常精简,然后为每个新功能(可能是每个新片段)创建ViewModel(或Presenter或类似的东西)。

总而言之,从本质上讲,这种方法就是采取更成熟的工程路线。