简单的fragement无法正常工作

时间:2016-02-16 09:09:52

标签: android android-fragments

我正在使用片段练习多窗格屏幕,但它无法正常工作。我想在单个活动中使用两个框架。一个应该是静态的,另一个是动态列表。

main_biddingwindow.xml

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="200dp"
    android:weightSum="1"
    android:id="@+id/ag">


    <fragment
        android:layout_width="163dp"
        android:layout_height="match_parent"
        android:name="driver.project_detail"
        android:id="@+id/fragment"
        android:layout_row="0"
        android:layout_column="0" />

    <fragment
        android:layout_width="184dp"
        android:layout_height="match_parent"
        android:name="driver.bid_list"
        android:id="@+id/fragment2"
        android:layout_row="0"
        android:layout_column="19" />
</GridLayout>` 

main.class

  public class biddingWindow extends Activity{

        protected void OnCreate(Bundle b){
            super.onCreate(b);
            setContentView(R.layout.main_biddingwindow);

        }

    }

bidlist.xml

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

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_gravity="center_horizontal"
       />
</LinearLayout>

bid_list.java

public class bid_list extends Fragment {
    public View onCreateView(LayoutInflater inflater ,ViewGroup container ,Bundle s){
        return inflater.inflate(R.layout.bidlist,container, false);
    }
}

output screen

2 个答案:

答案 0 :(得分:0)

    Just Create the objects of both the fragments in activity and you can access everything in fragment

public class bid_list extends Fragment {
public ListView listView;

    public View onCreateView(LayoutInflater inflater ,ViewGroup container ,Bundle s){

View view = inflater.inflate(R.layout.bidlist,container, false);

listView = (ListView)view.findViewById(R.id.listView);

return view ;
    }
}

答案 1 :(得分:0)

public class bid_list extends Fragment {
public ListView listView;

    public View onCreateView(LayoutInflater inflater ,ViewGroup container ,Bundle s){

View view = inflater.inflate(R.layout.bidlist,container, false);

listView = (ListView)view.findViewById(R.id.listView);

return view ;
    }
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/ag">

<FrameLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:name="driver.project_detail"
    android:id="@+id/fragment"/>

<FrameLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2"
    android:name="driver.bid_list"
    android:id="@+id/fragment2"/>

</LinearLayout>

class Main extends Activity
{


protected void OnCreate(Bundle b){
            super.onCreate(b);
            setContentView(R.layout.main_biddingwindow);
FrameLayout frame1 = (FrameLayout)findViewById(R.id.fragment);
FrameLayout frame2 = (FrameLayout)findViewById(R.id.fragment);

Fragment1 fragment1 = new Fragment1();
Fragment2 fragment2 = new Fragment2();
frame1.removeAllView();
frame1.addView(fragment1)
frame2.removeAllView();
frame2.addView(fragment2)
        }



}