2016-12-05 60 views
0

我是一個總的新手,我想學習Android開發很抱歉,如果這個問題已經問Android的標籤顯示在列表視圖中的新片段點擊

我只是做了顯示2個選項卡的新的應用程序,列表視圖,和一個選項頁,兩者都工作正常,但我想顯示一個新的片段(項目的詳細信息),當用戶從列表視圖中選擇一個項目

我知道我必須在onClickListener中添加代碼,但我不知道我需要添加...或者任何人都知道一個教程我可以閱讀做到這一點?

感謝您的幫助

這是我的第一個片段(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; 
    } 

} 

其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> 

這是類片段的我想說明:

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; 
    } 

} 

和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> 

主要活動XML:

<?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> 

回答

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(); 
     } 
    });  

容器=是要推動片段

+0

即時得到這個錯誤你的活動框架佈局:java.lang中。拋出:IllegalArgumentException:未發現ID 0x7f0c0073(com.miempresaenlanube.hoteles360:ID /容器)視圖中的片段hotelMainFragment {131022a#0的id = 0x7f0c0073},其奇怪,因爲我的容器確實命名容器 – Chico3001

+0

我不能看到容器內Framlayout'hotelsFragment'請更新您的代碼或使用該'fragmentTransaction.add(新hotelMainFragment(),標籤).commit();' –

+0

它顯示了錯誤......無法解析法「添加(com.miempresaenlanube.hoteles360.hotelMainFragment,INT)」 – Chico3001