在我的第一个片段中,我有2个微调器和1个按钮
spinner1(Type of Foods)
spinner2(Color)
我想要的是,如果用户在第一个微调器中选择一种食物并选择什么颜色,然后单击按钮,他们应该使用listview定向到FragmentTwo
取决于所选值
例如:用户在spinner1
中选择蔬菜并选择绿色spinner2
颜色后,点击按钮后,他们将转到FragmentTwo
,其中包含蔬菜的ListView,其颜色为绿色
FragmentOne()
import android.app.Fragment;
import android.os.Bundle;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class FragmentOne extends Fragment {
Spinner spinner1, spinner2;
public FragmentOne() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout_two, container,
false);
// Spinner
// Spinner
spinner1 = (Spinner) view.findViewById(R.id.spFood);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(getActivity(), R.array.food_arrays, android.R.layout.simple_spinner_item);
spinner1.setAdapter(adapter1);
spinner2 = (Spinner) view.findViewById(R.id.spColor);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(getActivity(), R.array.color_array, android.R.layout.simple_spinner_item);
spinner2.setAdapter(adapter2);
// Button
Button btnSearch = (Button) view.findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//what to do
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content_frame, new FragmentTwo());
ft.commit();
}
});
return view;
}
}
tvType
spinner1
的值,
tvColor
的值spinner2
FragmentTwo()
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
public class FragmentTwo extends Fragment {
TextView tv1, tv2;
ListView listView;
public FragmentTwo() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.foodlist, container,
false);
//TextView
tv1 = (TextView) view.findViewById(R.id.tvType);
tv2 = (TextView) view.findViewById(R.id.tvColor);
//ListView
listView = (ListView) view.findViewById(R.id.listView1);
return view;
}
}
FragmentTwo XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Type of Food"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tvColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="@string/Color"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" >
</ListView>
答案 0 :(得分:1)
试试这个..
在FragmentOne.class
Button btnSearch = (Button) view.findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
String type = spinner1.getSelectedItem().toString().trim();
String color = spinner2.getSelectedItem().toString().trim();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
FragmentTwo fragment = new FragmentTwo();
Bundle bundle = new Bundle();
bundle.putString("type", type);
bundle.putString("color", color);
fragment.setArguments(bundle);
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
});
和FragmentTwo.class
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.foodlist, container,
false);
//TextView
tv1 = (TextView) view.findViewById(R.id.tvType);
tv2 = (TextView) view.findViewById(R.id.tvColor);
String type = "",color = "";
Bundle args = getArguments();
if (args != null && args.containsKey("type"))
type = args.getString("type");
if (args != null && args.containsKey("color"))
color = args.getString("color");
tv1.setText(type);
tv2.setText(color);
//ListView
listView = (ListView) view.findViewById(R.id.listView1);
return view;
}