简体   繁体   English

一个layout.xml用于多个活动

[英]one layout.xml for multiple activities

I'm new to Java and Android. 我是Java和Android的新手。 I'm making an app (mostly for use by me and to get some knowledge), where I have a list view width 101 items. 我正在制作一个应用程序(主要供我使用并获得一些知识),其中我有一个列表视图宽度101项。 When I click one item, it starts a new activity, with different layout. 当我单击一个项目时,它会启动一个具有不同布局的新活动。 The layout for each of the 101 items is the same, but the content is different. 101个项目中每个项目的布局相同,但内容不同。 My question is: Can I make one .xml file for all the items, but make the content differ, based on what item is clicked? 我的问题是:我可以为所有项目制作一个.xml文件,但根据点击的项目使内容不同?

I'll add the my code below (very experimental, and will certainly change the layout) :) Thanks for any answers! 我将在下面添加我的代码(非常实验性,并且肯定会改变布局):)谢谢你的答案!

MainAvtivity.java MainAvtivity.java

public class MainActivity  extends AppCompatActivity {
ListView listView;

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

    listView = (ListView) findViewById(R.id.list);
    String[] values = new String[]{"§01. Kongle", "§02. Binders",
            "§03. Plaster", "§04. Kvitering", "§05. 'Ruter'-logo", "§06. Ølkork", "§07. Hjerteutklipp av russekort",
            "§08. Ølkork i gull", "§09. Bussbillett", "§10. Bit av et sitteunderlag", "§11. Signatur fra helsesøster",
            "§12. Skolens logo", "§13. Første bokstav", "§14. Bit av skilt", "§15. Bit av burgereske", "§16. Fjær", "§16. Bilde av Barney Stinson",
            "§17. Stoffbit", "§18. Champagnekork i gull", "§19. Billettarmbånd", "§20. Amnesty-logo", "§21. Plastskje", "§22. Mobildeksel",
            "§24. Legokloss", "§25. Tampong", "§26. Kritt", "§27. Undertøy", //Plus more
    };

    ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
            android.R.layout.simple_list_item_activated_2, android.R.id.text1, values);

    listView.setAdapter(adapter);

    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            if (position == 0) {
                Intent myIntent = new Intent(view.getContext(), fastnokkel.class);
                startActivityForResult(myIntent, 0);
            }

            if (position == 1) {
                Intent myIntent = new Intent(view.getContext(), binders.class); //These classes will differ from witch item is clicked.
                startActivityForResult(myIntent, 0);
            }

            if (position == 2) {
                Intent myIntent = new Intent(view.getContext(), plaster.class);
                startActivityForResult(myIntent, 0);
            }

            if (position == 3) {
                Intent myIntent = new Intent(view.getContext(), plaster.class);
                startActivityForResult(myIntent, 0);
            }

            if (position == 4) {
                Intent myIntent = new Intent(view.getContext(), kongle.class);
                startActivityForResult(myIntent, 0);
            }

            if (position == 5) {
                Intent myIntent = new Intent(view.getContext(), binders.class);
                startActivityForResult(myIntent, 0);
            }

            if (position == 6) {
                Intent myIntent = new Intent(view.getContext(), kongle.class);
                startActivityForResult(myIntent, 0);
            }

            if (position == 7) {
                Intent myIntent = new Intent(view.getContext(), binders.class);
                startActivityForResult(myIntent, 0);
            }
        }
    });
}

Avtivity_Main.xml Avtivity_Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
android:orientation="vertical"
tools:context="com.example.sande.listview8.MainActivity"
android:weightSum="1">

<ListView
    android:id="@+id/list"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">
</ListView>

Same_Layout_Different_Content.xml Same_Layout_Different_Content.xml

<?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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:contentDescription="@string/bildeBesk"
    app:srcCompat="@drawable/kongle"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/imageView2"
    android:layout_centerHorizontal="true">

    <TextView
        android:id="@+id/gjenstand"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:text="@string/kongle"
        android:textSize="35sp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/gjoremaal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/gjenstand"
        android:layout_marginTop="19dp"
        android:textSize="30sp"
        android:text="@string/kongleBeskrivelse"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

Yeah, put all type of view in one xml file. 是的,将所有类型的视图放在一个xml文件中。 Like in this structure : 就像在这个结构中:

<ParentLayout>
     <Type1/>
     <Type2/>
     ......
 </ParentLayout>

Keep visibility of all Type layout as Gone. 保持所有类型布局的可见性为Gone。 Then when item is clicked in parent activity, pass a unique ID to identify the layout type. 然后,当在父活动中单击项目时,传递唯一ID以标识布局类型。 Then use a switch case in the second activity to activate the necessary layout. 然后在第二个活动中使用开关盒来激活必要的布局。

What you need to do is use a custom adapter that inherits BaseAdapter . 您需要做的是使用继承BaseAdapter的自定义适配器。

This adapter will be able to update the visual part of each cell according to the your position and show/hide elements of a single xml to do so. 此适配器将能够根据您的位置更新每个单元格的可视部分,并显示/隐藏单个xml的元素。

For example: 例如:

public class MyAdapter extends BaseAdapter {
        String[] values = new String[]{"§01. Kongle", "§02. Binders",
        "§03. Plaster", "§04. Kvitering", "§05. 'Ruter'-logo", "§06. Ølkork", "§07. Hjerteutklipp av russekort",
        "§08. Ølkork i gull", "§09. Bussbillett", "§10. Bit av et sitteunderlag", "§11. Signatur fra helsesøster",
        "§12. Skolens logo", "§13. Første bokstav", "§14. Bit av skilt", "§15. Bit av burgereske", "§16. Fjær", "§16. Bilde av Barney Stinson",
        "§17. Stoffbit", "§18. Champagnekork i gull", "§19. Billettarmbånd", "§20. Amnesty-logo", "§21. Plastskje", "§22. Mobildeksel",
        "§24. Legokloss", "§25. Tampong", "§26. Kritt", "§27. Undertøy", //Plus more
        };

    @Override
    public int getCount() {
        return values.length;
    }

    @Override
    public Object getItem(int i) {
        return values[i];
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        if (convertView == null) {
             Context context = parent.getContext();
             convertView = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
             convertView.setTag(new ViewHolder());
        }

        // Here you set the view - set the text hide views etc... it would be beneficial to use ViewHolder pattern as well 
        ViewHolder holder = convertView.getTag();
        holder.setData(i, values[i]);
    }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM