2016-12-05 6 views
0

ich ein totaler Neuling bin und ich versuche, android Entwicklung zu lernen, so traurig, wenn diese Frage bereits gefragtAndroid Tabs zeigt neues Fragment auf Listenansicht klicken

ich gerade eine neue App zeigt zwei Registerkarten, eine Listenansicht , und eine Optionen Seite, beide funktionieren gut, aber ich möchte ein neues Fragment (die Artikeldetails) zeigen, wenn der Benutzer ein Element aus der Listenansicht

Ich weiß, ich muss den Code innerhalb der OnClickListener, aber ich weiß nicht, was ich hinzufügen muss ... oder kennt jemand ein Tutorial, das ich lesen kann, um das zu erreichen?

Danke für Ihre Hilfe ist

Hier mein erstes Fragment (hotelsFragment.java)

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ListAdapter; 
import android.widget.ListView; 

public class hotelsFragment extends Fragment { 

    String[] hotels = {"Hotel 1", "Hotel 2", "Hotel 3", "Hotel 4", "Hotel 5", "Hotel 6"}; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.hoteles_fragment, container, false); 
     ListView hotelsListView = (ListView) view.findViewById(R.id.hotelslist); 

     ListAdapter hotelsListAdapter = new hotelsAdapter(getContext(), hotels); 
     hotelsListView.setAdapter(hotelsListAdapter); 

     hotelsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 
       // I want to show a new fragment in here 
      } 
     }); 

     return view; 
    } 

} 

Sein 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" 
    tools:context="com.miempresaenlanube.hoteles360.MainActivity$PlaceholderFragment"> 


    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/hotelslist" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" /> 

</RelativeLayout> 

Dies ist die Klasse des Fragments i zeigen wollen :

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class hotelMainFragment extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.hotel_main, container, false); 

     return view; 
    } 

} 

und seine 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:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:srcCompat="@drawable/hotel" 
     android:id="@+id/imageView2" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" 
     android:scaleType="centerCrop" /> 
</RelativeLayout> 

Haupt XML Aktivität:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 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" 
    android:id="@+id/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.miempresaenlanube.hoteles360.MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/appbar_padding_top" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/AppTheme.PopupOverlay"> 

     </android.support.v7.widget.Toolbar> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginBottom="?attr/actionBarSize" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

</android.support.design.widget.CoordinatorLayout> 

Antwort

0
hotelsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 
      FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction(); 
    fragmentTransaction.add(R.id.container, new hotelMainFragment()).commit(); 
         -or- 
    fragmentTransaction.add(new hotelMainFragment(), tag).commit(); 
     } 
    });  

Container = war Frame-Layout Ihrer Aktivität in dem Sie Fragment

+0

Im immer diese Fehlermeldung drücken wollen: java.lang. IllegalArgumentException: Es wurde keine Ansicht für die ID 0x7f0c0073 (com.miempresaenlanube.hoteles360: id/container) für das fragment hotelMainFragment {131022a # 0 id = 0x7f0c0073} gefunden Seltsam, weil mein Container tatsächlich Container – Chico3001

+0

heißt Ich kann Container Framlayout in 'hotelsFragment' nicht sehen Bitte aktualisieren Sie Ihren Code oder verwenden Sie dieses' fragmentTransaction.add (neues hotelMainFragment(), tag) .commit(); ' –

+0

Es zeigt einen Fehler an ... kann die Methode 'add' nicht auflösen (com.miempresaenlanube.hoteles360.hotelMainFragment, int) ' – Chico3001

Verwandte Themen