2016-12-05 61 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