2013-10-24 123 views
0

正在从this链接创建主/明细片段。这工作得很好,现在在这里,而不是textview详细片段类我想要实现列表视图,(即)我想显示的详细资料作为一个孩子listview,因此我试图 detail.xml:列表视图在主/详细视图中作为详细片段

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <ListView 
      android:layout_width="120dp" 
      android:layout_height="wrap_content" 
      android:id="@+id/listview1" 
      />" 
<ListView 
      android:layout_width="120dp" 
      android:layout_height="wrap_content" 
      android:id="@+id/listview2" 
      />" 

    </LinearLayout> 

,并在我的detailFragment类

public class DetailFragment1 extends ListFragment { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     String[] values = new String[] { "1", "2", "3", 
       "4" }; 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), 
       android.R.layout.simple_list_item_1, values); 
       setListAdapter(adapter); 

    } 

我的问题是孩子的ListView在初期显示,但我想,当我点击主page.Am新片段的第一行显示此列表视图,帮帮我吧在实现这一点。在此先感谢..

回答

0

而不是这样做,您可以设计一个包含ListView作为主和frameLayout容器的布局。在frameLayout容器中,您可以动态地对布局进行充气,这样设计的方式可以让您拥有子列表视图和详细信息视图。

<LinearLayout 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:orientation="horizontal" 
    tools:context=".NavigationActivity" > 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:background="@color/strip_background" 
     android:layout_weight="1" 
     android:orientation="vertical" > 

     <ListView 
      android:id="@+id/nav_lv_transactions" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:scrollbars="none" > 

     </ListView> 

    </LinearLayout> 

    <FrameLayout 
     android:id="@+id/nav_fl_frag_container" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="14" 
     android:background="@android:color/darker_gray" > 

    </FrameLayout> 

</LinearLayout> 

在的onclick listerner把这个代码,

Bundle arguments = new Bundle(); 
arguments.putString(ARG_ITEM_ID, 
"Some Name"); 
MyClassFragment newFragment = new MyClassFragment(); 
newFragment.setArguments(arguments); 
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
transaction.replace(R.id.nav_fl_frag_container, newFragment); 
transaction.commit(); 

试试这个。它将工作

+0

感谢您的回答..我如何在我的onclick监听器类中实现布局 – AndroidOptimist

+0

为想要膨胀的布局创建一个片段(带有子代),然后在点击监听器中放入我已编辑的代码。 –

1

创建回调接口:

public interface Callbacks { 

     public void onItemSelected(long id); 
} 

让片段inplement它:

public class DetailFragment1 extends ListFragment implements CallBacks { 

在ListFragment OnListitemClick:

@Override 
public void onListItemClick(ListView listView, View view, int position, long id) {  
    mCallbacks.onItemSelected(id); 
} 

在活动:

Public class MyActivity extends FragmentActivity implements Callbacks { 

@Override 
public void onItemSelected(long id) { 
    // Replace the detail fragment with the corresponding item selected in the list 
}