如何将列表视图扩展到片段

时间:2012-03-06 11:40:54

标签: android viewflipper fragment inflate

我在我的应用程序中使用片段。有5个tabs.in 1选项卡我需要在按钮上点击列表视图。下面是我的代码。

public class TabStoreLocatorFragment extends Fragment implements OnClickListener {

private Button mapSwitcher;
private LinearLayout lastSelectedStoreButton;
private LinearLayout noSelectedStoreSection;
private TextView storeInfoName;
private TextView storeInfoAddress;
private TextView storeInfoCity;
private RelativeLayout phoneLocationButton;
private EditText searchStoreLocation;
private ListView storeList;
private ViewFlipper storeFlipper;
LinearLayout theLayout;
Store store;

@SuppressWarnings("static-access")
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if (container == null) {
        }

     theLayout = (LinearLayout) inflater.inflate(
            R.layout.tab_store_locator_layout, container, false);

    phoneLocationButton = (RelativeLayout) theLayout.findViewById(R.id.phone_location_button);
    mapSwitcher=(Button)theLayout.findViewById(R.id.switch_state_button);
    lastSelectedStoreButton=(LinearLayout)theLayout.findViewById(R.id.last_selected_store_button);
    noSelectedStoreSection=(LinearLayout)theLayout.findViewById(R.id.no_selected_store);
    storeInfoName=(TextView)theLayout.findViewById(R.id.store_name);
    storeInfoAddress=(TextView)theLayout.findViewById(R.id.store_address);
    storeInfoCity=(TextView)theLayout.findViewById(R.id.store_city);
    searchStoreLocation=(EditText)theLayout.findViewById(R.id.search_store);
    storeFlipper = (ViewFlipper)theLayout.findViewById(R.id.store_flipper);


    store=new Store(); 
    store.setName("store1");
    store.setAddress("california");
    store.setCity("newyork");
    store.setZipCode("23");
    store.setState("california");

    phoneLocationButton.setOnClickListener(this);
    mapSwitcher.setOnClickListener(this);
    lastSelectedStoreButton.setOnClickListener(this);
    return theLayout;

}


@Override
public void onClick(View view) {
    int id = view.getId();
    if (id == R.id.phone_location_button) {

        storeList = (ListView)storeFlipper.inflate(getActivity(),   R.layout.store_list, storeFlipper);
        storeFlipper.addView(storeList);
        StoreAdapter adapter = new StoreAdapter(getActivity(), R.layout.store_list_item);
        storeList.setAdapter(adapter);


    }
    else if(id == R.id.switch_state_button){

    }
    else{


    }


}

     }

我想将storelist扩展到storeFlipper.i试图这样做。但它不起作用。下面是该标签的相应布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@android:color/white" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/find_a_store_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="15dp"
            android:layout_alignParentLeft="true"
            android:text="@string/tab_find_a_store_title"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:textStyle="bold" />

        <Button android:id="@+id/switch_state_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp"
            android:background="@drawable/switch_selector" />       

    </RelativeLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#E8E2D3" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#C5BFAD" />

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <LinearLayout android:id="@+id/last_selected_store_button"  android:baselineAligned="false" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            style="@android:style/Widget.Button"
            android:orientation="horizontal"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:paddingRight="5dp"
            android:background="@drawable/last_selected_store_background" >

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1" >        

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"              
                    android:textColor="#8E8672"
                    android:text="@string/last_selected_store_label"
                    android:textSize="12sp" />

            </RelativeLayout>        

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="5dp"            
                android:orientation="vertical" >

                <TextView android:id="@+id/store_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@android:color/black"
                    android:textSize="12sp" />

                <TextView android:id="@+id/store_address"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@android:color/black"
                    android:textSize="12sp" />         

                <TextView android:id="@+id/store_city"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@android:color/black"
                    android:textSize="12sp" />          

            </LinearLayout>      

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1" >

                <ImageView 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="10dp"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:background="@drawable/arrow" />

            </RelativeLayout>

        </LinearLayout>         

        <LinearLayout android:id="@+id/no_selected_store"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:paddingRight="5dp"
            android:background="@drawable/last_selected_store_background"  >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"                
                android:textColor="#8E8672"
                android:text="@string/no_selected_store_label"
                android:textSize="12sp" />

        </LinearLayout>             

    </FrameLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dp" >

        <EditText android:id="@+id/search_store"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:singleLine="true"
            android:textSize="14sp"
            android:hint="@string/search_store_placeholder"
            android:layout_toLeftOf="@+id/phone_location_button"
            android:background="@drawable/search_store_background" />

        <RelativeLayout android:id="@id/phone_location_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:background="@drawable/get_location_background" >

            <ImageView              
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/get_location" />

        </RelativeLayout>

    </RelativeLayout>        

    <ViewFlipper android:id="@+id/store_flipper"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>       

</LinearLayout>    

任何人都可以帮我把清单扩充到鳍状肢。

1 个答案:

答案 0 :(得分:5)

很简单。只需输入此代码。

 storeList = (ListView) inflater.inflate(R.layout.store_list, null);

它正在运作