如何压缩XML和Java代码,使我的应用程序运行更顺畅

如何压缩XML和Java代码,使我的应用程序运行更顺畅,java,android,xml,Java,Android,Xml,我已经为我的Facebook群组制作了一个应用程序。XML使用了很多导航抽屉,WebView,ListView,Java使用了很多适配器来运行ListView。我想知道的是,有一种方法可以压缩我的代码,使应用程序运行得更好。我添加了一个XML和java片段供您查看。任何帮助都将不胜感激 这是一段java代码,它对每个阵列适配器都重复,正如您所看到的,我有一些代码 ListView CastingAppsDlNav,FireStickDlNav,RootingToolsDlNav,Modding

我已经为我的Facebook群组制作了一个应用程序。XML使用了很多导航抽屉,
WebView
ListView
,Java使用了很多适配器来运行ListView。我想知道的是,有一种方法可以压缩我的代码,使应用程序运行得更好。我添加了一个XML和java片段供您查看。任何帮助都将不胜感激

这是一段java代码,它对每个阵列适配器都重复,正如您所看到的,我有一些代码

 ListView CastingAppsDlNav,FireStickDlNav,RootingToolsDlNav,ModdingToolsDlNav,LauncherDlNav,FlimnTvDlNav,ShowBoxDlNav,
            KodiDlNav,LiveTvDlNav,MediaPlayerDlNav,MxPlayerNav,CodecNav,MusicAppNav,MusicDlAppsNav,AnimeNav,TvGuideDlsNav;

    ArrayAdapter<String> CastingAdapter,FireStickAdapter,RootingToolAdapter,ModdingToolsAdapter,LauncherAdapter,FilmnTvappAdapter,
            ShowBoxAdapter,KodiDlAdapter,LiveTvAdapter,MediaPlayerAdapter, MxPlayerAdapter, CodecAdapter, MusicAppsAdapter, MusicDlAppsAdapter,
    AnimeAdapter,TvGuidesAdapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_downloads);

        DownloadDrawer = (DrawerLayout) findViewById(R.id.DownloadDrawer);
        SvDownloads = (ScrollView) findViewById(R.id.SvDownloads);
        SvDownloads.setScrollbarFadingEnabled(false);

        // Tv Guide Array setup
        TvGuideDlsNav = (ListView) findViewById(R.id.TvGuideDlsNav);
        TvGuideDlsNav.setScrollbarFadingEnabled(false);

        String[] TvGuideArray = {"Sky+ Tv Guide", "TvGuide.co.uk","Tv Shows Favs","Tv ShowTimes","HomeScreen"};
        TvGuidesAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,TvGuideArray);
        TvGuideDlsNav.setAdapter(TvGuidesAdapter);
        TvGuideDlsNav.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        //Sky Plus
                        Intent iSkyplusH20 = new Intent(Intent.ACTION_VIEW, Uri.parse(SkyplusplayDl));
                        startActivity(iSkyplusH20);
                        break;

                    case 1:
                        //Tvguide.co.uk
                        Intent iTvguidecouk = new Intent(Intent.ACTION_VIEW, Uri.parse(TvGuideUkPlayDl));
                        startActivity(iTvguidecouk);
                        break;

                    case 2:
                        //Tv Shows favs
                        Intent iTvshowsFavs = new Intent(Intent.ACTION_VIEW, Uri.parse(TvshowsFavPlayDl));
                        startActivity(iTvshowsFavs);

                        break;

                    case 3:
                        //Tv Showtimes
                        Intent iTvShowtimes = new Intent(Intent.ACTION_VIEW, Uri.parse(TVShowtimePlayDl));
                        startActivity(iTvShowtimes);

                        break;

                    case 4:
                        DownloadDrawer.closeDrawer(TvGuideDlsNav);

                        break;

                }
            }
        });
ListView CastingAppsDlNav、FireStickDlNav、RootingToolsDlNav、ModdingToolsDlNav、LauncherDlNav、FlimnTvDlNav、ShowBoxDlNav、,
KodiDlNav、LiveTvDlNav、MediaPlayerDlNav、MxPlayerNav、CodecNav、MusicCapPNav、MusicLappsNav、AnimeNav、TvGuideDlsNav;
阵列适配器CastingAdapter、FireStickAdapter、RootingToolAdapter、ModdingToolsAdapter、LauncherAdapter、FilmnTvappAdapter、,
ShowBoxAdapter、KodidAdapter、LiveTvAdapter、MediaPlayerAdapter、MxPlayerAdapter、CodeAdapter、MusicCappAdapter、MusicLappsAdapter、,
AnimeAdapter、TvGuidesDapter;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,WindowManager.LayoutParams.FLAG_全屏);
setContentView(R.layout.activity_下载);
DownloadDrawer=(抽屉布局)findViewById(R.id.DownloadDrawer);
SvDownloads=(滚动视图)findviewbyd(R.id.SvDownloads);
SvDownloads.setScrollBarFadinEnabled(false);
//电视指南阵列设置
TvGuideDlsNav=(列表视图)findViewById(R.id.TvGuideDlsNav);
TvGuideDlsNav.SetScrollBarFadinEnabled(假);
字符串[]TvGuideArray={“天空+电视指南”,“TvGuide.co.uk”,“电视节目FAV”,“电视节目时间”,“主屏幕”};
TvGuidesAdapter=newArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,TvGuideArray);
TvGuideDlsNav.setAdapter(TvGuidesAdapter);
TvGuideDlsNav.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
开关(位置){
案例0:
//天空加号
Intent iSkyplusH20=新的Intent(Intent.ACTION_视图,Uri.parse(SkyplusplayDl));
星触觉(iSkyplusH20);
打破
案例1:
//Tvguide.co.uk
Intent iTvguidecouk=新的Intent(Intent.ACTION_视图,Uri.parse(TvGuideUkPlayDl));
startActivity(iTvguidecouk);
打破
案例2:
//电视节目
Intent iTvshowsFavs=新的Intent(Intent.ACTION_视图,Uri.parse(TvshowsFavPlayDl));
startActivity(iTvshowsFavs);
打破
案例3:
//电视节目时间
Intent iTvShowtimes=新的Intent(Intent.ACTION_视图,Uri.parse(TVShowtimePlayDl));
星触觉(iTvShowtimes);
打破
案例4:
下载抽屉。关闭抽屉(TvGuideDlsNav);
打破
}
}
});
接下来是我的XML,这是我想压缩它的主要原因

<android.support.v4.widget.DrawerLayout 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"
    tools:context="com.example.harrops.h20droidapp.downloads"
    android:background="@drawable/splashbkground"
    android:id="@+id/DownloadDrawer">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">

        <ImageView
            android:contentDescription="@string/desc"
            android:background="@drawable/h20logo"
            android:layout_width="fill_parent"
            android:layout_height="80sp"
            android:id="@+id/imageView12"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_margin="10dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="H20 Downloads"
            android:id="@+id/textView12"
            android:layout_below="@+id/imageView12"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40sp"
            android:textColor="#ffffff" />

        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/SvDownloads"
            android:layout_below="@+id/textView12"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="10dp">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/CastingDl"
            android:id="@+id/bCastingAppsDl"
            android:layout_below="@+id/textView12"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10sp"
            android:textSize="20sp"
            android:background="#4b48ac"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/FireStickDl"
            android:id="@+id/bFireStickDl"
            android:layout_below="@+id/bCastingAppsDl"
            android:layout_centerHorizontal="true"
            android:textSize="20sp"
            android:background="#4b48ac"
            android:layout_marginTop="10sp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" />

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/RootingDl"
            android:id="@+id/bRootingToolsDl"
            android:layout_below="@+id/bFireStickDl"
            android:layout_centerHorizontal="true"
            android:textSize="20dp"
            android:background="#4b48ac"
            android:layout_marginTop="10sp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" />

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/ModdingDl"
            android:id="@+id/bModdingToolsDl"
            android:layout_below="@+id/bRootingToolsDl"
            android:layout_centerHorizontal="true"
            android:textSize="20dp"
            android:background="#4b48ac"
            android:layout_marginTop="10sp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" />

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/LauncherDl"
            android:id="@+id/bLauncherAppDl"
            android:layout_below="@+id/bRootingToolsDl"
            android:layout_centerHorizontal="true"
            android:textSize="20sp"
            android:background="#4b48ac"
            android:layout_marginTop="10sp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" />

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/FilmnTvDl"
            android:id="@+id/bFilmnTvDl"
            android:layout_below="@+id/bLauncherAppDl"
            android:layout_centerHorizontal="true"
            android:textSize="20sp"
            android:background="#4b48ac"
            android:layout_marginTop="10sp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" />

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/LiveTvDl"
            android:id="@+id/bLiveTvDl"
            android:layout_below="@+id/bFilmnTvDl"
            android:layout_centerHorizontal="true"
            android:textSize="20sp"
            android:background="#4b48ac"
            android:layout_marginTop="10sp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" />

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/MediaplayerDl"
            android:id="@+id/bMediaPlayerDl"
            android:layout_below="@+id/bLiveTvDl"
            android:layout_centerHorizontal="true"
            android:textSize="20sp"
            android:background="#4b48ac"
            android:layout_marginTop="10sp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" />

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Anime Apps"
            android:id="@+id/bAnimeDl"
            android:layout_below="@+id/bMediaPlayerDl"
            android:layout_centerHorizontal="true"
            android:textSize="20sp"
            android:background="#4b48ac"
            android:layout_marginTop="10sp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" />

                <Button
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/MusicAppsDl"
                    android:id="@+id/bMusicApps"
                    android:layout_below="@+id/bAnimeDl"
                    android:layout_centerHorizontal="true"
                    android:textSize="20sp"
                    android:background="#4b48ac"
                    android:layout_marginTop="10sp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp" />


                <Button
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="Tv Guides"
                    android:id="@+id/bTvGuideDl"
                    android:layout_below="@+id/bMusicApps"
                    android:layout_centerHorizontal="true"
                    android:textSize="20sp"
                    android:background="#4b48ac"
                    android:layout_marginTop="10sp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp" />

                <Button
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/homescreenDl"
                    android:id="@+id/bHomescreenDl"
                    android:layout_below="@+id/bMusicApps"
                    android:layout_centerHorizontal="true"
                    android:textSize="20sp"
                    android:background="#4b48ac"
                    android:layout_marginTop="10sp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp" />


            </LinearLayout>
        </ScrollView>

    </RelativeLayout>

    <ListView
        android:layout_width="200sp"
        android:layout_height="fill_parent"
        android:id="@+id/CastingAppsDlNav"
        android:background="#4b48ac"
        android:layout_gravity="start"
        >

    </ListView>

    <ListView
        android:layout_width="200sp"
        android:layout_height="fill_parent"
        android:id="@+id/FireStickDlNav"
        android:background="#4b48ac"
        android:layout_gravity="start"
        >

    </ListView>

    <ListView
        android:layout_width="200sp"
        android:layout_height="fill_parent"
        android:id="@+id/RootingToolsDlNav"
        android:background="#4b48ac"
        android:layout_gravity="start"
        >

    </ListView>

    <ListView
        android:layout_width="200sp"
        android:layout_height="fill_parent"
        android:id="@+id/ModdingToolsDlNav"
        android:background="#4b48ac"
        android:layout_gravity="start"
        >

    </ListView>

    <ListView
        android:layout_width="200sp"
        android:layout_height="fill_parent"
        android:id="@+id/LauncherDlNav"
        android:background="#4b48ac"
        android:layout_gravity="start"
        >

    </ListView>

    <ListView
        android:layout_width="200sp"
        android:layout_height="fill_parent"
        android:id="@+id/FlimnTvDlNav"
        android:background="#4b48ac"
        android:layout_gravity="start"
        >

    </ListView>

    <ListView
        android:layout_width="200sp"
        android:layout_height="fill_parent"
        android:id="@+id/LiveTvDlNav"
        android:background="#4b48ac"
        android:layout_gravity="start"
        >

    </ListView>

    <ListView
        android:layout_width="200sp"
        android:layout_height="fill_parent"
        android:id="@+id/MediaPlayerDlNav"
        android:background="#4b48ac"
        android:layout_gravity="start"
        >

    </ListView>

    <ListView
        android:layout_width="200sp"
        android:layout_height="fill_parent"
        android:id="@+id/MusicAppNav"
        android:background="#4b48ac"
        android:layout_gravity="start"
        >

    </ListView>

    <ListView
        android:layout_width="200sp"
        android:layout_height="fill_parent"
        android:id="@+id/MemeAppDlNav"
        android:background="#4b48ac"
        android:layout_gravity="start"
        >

    </ListView>

    <ListView
        android:layout_width="200sp"
        android:layout_height="fill_parent"
        android:id="@+id/ShowBoxDlNav"
        android:background="#4b48ac"
        android:layout_gravity="end"
        >

    </ListView>

    <ListView
        android:layout_width="200sp"
        android:layout_height="fill_parent"
        android:id="@+id/KodiDlNav"
        android:background="#4b48ac"
        android:layout_gravity="end"
        >

    </ListView>

    <ListView
        android:layout_width="200sp"
        android:layout_height="fill_parent"
        android:id="@+id/MxPlayerNav"
        android:background="#4b48ac"
        android:layout_gravity="end"
        >

    </ListView>

    <ListView
        android:layout_width="200sp"
        android:layout_height="fill_parent"
        android:id="@+id/MusicDlAppsNav"
        android:background="#4b48ac"
        android:layout_gravity="end"
        >

    </ListView>

    <ListView
        android:layout_width="200sp"
        android:layout_height="fill_parent"
        android:id="@+id/CodecNav"
        android:background="#4b48ac"
        android:layout_gravity="start"
        >

    </ListView>

    <ListView
        android:layout_width="200sp"
        android:layout_height="fill_parent"
        android:id="@+id/AnimeNav"
        android:background="#4b48ac"
        android:layout_gravity="start"
        >
        </ListView>

    <ListView
        android:layout_width="200sp"
        android:layout_height="fill_parent"
        android:id="@+id/TvGuideDlsNav"
        android:background="#4b48ac"
        android:layout_gravity="start"
        >

    </ListView>
</android.support.v4.widget.DrawerLayout>

一个选项是使用片段来包含单独的列表视图

是碎片的样子

假设其他ListView具有与TvGuideDlsNav类似的功能,类似的功能可能会在同时处理所有视图的活动中发挥作用:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.yourpackage.AListOfListViews"
        android:id="@+id/list"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent" >

<!-- THE LIST TITLES HERE -->

</fragment>

<fragment android:name="com.yourpackage.HereIsAListViewOrActivity"
        android:id="@+id/viewer"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent" >

<!-- LISTVIEW OR ACTIVITY HERE -->
</fragment>
</LinearLayout>

在这种情况下,R.id.fragment\u容器将是R.id.viewer,其中包含第二个片段。另外,newFragment将是处理所选ListView的片段类。

您肯定应该将这一个活动分解为多个。太多
ListView
在不了解应用程序流程的情况下,很难给出具体的步骤,但看起来It’将一个大型活动分解为多个片段可能会有所帮助。17个ListView的用途是什么?您可以只使用一个ListView并根据需要更改适配器吗?(您不是一次显示所有17个,是吗?)这是我不明白的。我该如何实现每个片段。假设我点击电视指南列表视图,我该如何让java理解使用哪个fragent。很抱歉,这很痛苦。你需要为每个列表视图创建一个片段类,并在你的OnItemClick中创建一个片段管理器。查看编辑过的答案OK,这样我就知道了如何创建片段。但是,当我尝试添加这行代码TvGuidesAdapter=new ArrayAdapter(这是android.R.layout.simple_list_item_1,TvGuideArray)时,我得到了一个错误。我假设它是this station。但是我不知道如何更改它。错误是什么?你是否将这行放在片段或活动中?如果它在片段中,你需要替换“this”使用getActivity()获取上下文
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment
transaction.replace(R.id.fragment_container, newFragment);

// Commit the transaction
transaction.commit();