gpt4 book ai didi

android - 如何将 ListView 膨胀为 fragment

转载 作者:搜寻专家 更新时间:2023-11-01 07:37:01 24 4
gpt4 key购买 nike

我在我的应用程序中使用 fragment 。有 5 个选项卡。在 1 个选项卡中,我需要在单击按钮时扩展 ListView 。下面给出的是该选项卡的代码。

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{


}


}

}

我想将商店列表扩展到 storeFlipper。我尝试这样做。但它不起作用。下面给出的是该选项卡的相应布局。

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

任何人都可以帮助我将列表膨胀到 flipper。

最佳答案

很简单,放这段代码就可以了。

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

它正在工作

关于android - 如何将 ListView 膨胀为 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9582994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com