简体   繁体   中英

Fragment with a ListView should be replaced with another Fragment with another ListView

I created a new Android project with a Blank Activity (MainActivity.java and fragment_main.xml). I add a Fragment (Liste1.java, fragment_list1.xml and list_item_list1.xml).

If an item is clicked the ListView should replace with another fragment and in this fragment should be a ListView too.

How can I do this?

MainActivity.java:

public class MainActivity extends AppCompatActivity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    if (id == R.id.action_settings) {

        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment"
android:name="de.programmierenlernenhq.ersteapp.Liste1"
tools:layout="@layout/fragment_liste1"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Liste1.java:

public class Liste1 extends Fragment {

public Liste1() { }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    String [] aktienlisteArray = {
            "item 1",  "item 2",  "item 3",  "item 4",  "item 5",  
            "item 6",  "item 7",  "item 8",  "item 9"
    };

    List<String> aktienListe = new ArrayList<>(Arrays.asList(aktienlisteArray));

    ArrayAdapter <String> aktienlisteAdapter =
            new ArrayAdapter<>(
                    getActivity(), // Die aktuelle Umgebung (diese Activity)
                    R.layout.list_item_liste1, // ID der XML-Layout Datei
                    R.id.list_item_aktienliste_textview, // ID des TextViews
                    aktienListe); // Beispieldaten in einer ArrayList

    View rootView = inflater.inflate(R.layout.fragment_liste1, container, false);

    ListView aktienlisteListView = (ListView) rootView.findViewById(R.id.listview_aktienliste);
    aktienlisteListView.setAdapter(aktienlisteAdapter);

    aktienlisteListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            // Handle items here
            switch (position) {
                case 0: {

                    break;
                }
            }
        }
    });

    return rootView;
}
}

fragment_liste1.xml:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout
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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivityFragment">

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

</FrameLayout>

list_item_liste1.xml:

<?xml version="1.0" encoding="utf-8"?>

<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:id="@+id/list_item_aktienliste_textview" >

</TextView>

Question 2: Below should be R.string.item1..., but it does not work?!

String [] aktienlisteArray = {
            "item 1",  "item 2",  "item 3",  "item 4",  "item 5",  
            "item 6",  "item 7",  "item 8",  "item 9" };

Try use FragmentTransaction . Here's the docs : https://developer.android.com/training/basics/fragments/fragment-ui.html#Replace
Here's the most relevant code:

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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