[英]Android - Inflate fragment depending on listview item clicked
How can I display a string/text inside my Fragment
, depending on the item clicked in my ListView
? 如何根据我的ListView
单击的项目在Fragment
显示字符串/文本?
This is how my ListView
looks visually--- 这就是我的ListView
外观---
When I click Say Hello
I want to display hello
and when I click Say Bye
I want to display bye... 当我单击Say Hello
我想显示hello
,当我单击Say Bye
我想显示再见...
MainActivity.java MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager vp = (ViewPager) findViewById(R.id.n_Viewpager);
this.addPages(vp);
}
//ADD ALL PAGES
private void addPages(ViewPager pager) {
MyFragPagerAdapter adapter = new MyFragPagerAdapter(getSupportFragmentManager());
adapter.addPage(new ContainerFragment());
//SET ADAPTER TO PAGER
pager.setAdapter(adapter);
}
}
TestFragment.java TestFragment.java
public class TestFragment extends ListFragment {
//Setting the name of each event
String[] options = {"Say Hello", "Say Bye"};
ArrayAdapter<String> adapter;
//Inflating the view with the fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
adapter = new ArrayAdapter<>(getActivity(), R.layout.listitem, R.id.textview, options);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
//Click events
@Override
public void onStart() {
super.onStart();
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View v, int pos, long l) {
switch (pos) {
case 0:
getParentFragment().getChildFragmentManager().beginTransaction().replace(R.id.fragment_container_root_view, new SaySomethingFragment()).addToBackStack("").commit();
break;
case 1:
break;
}
}
});
}
}
ContainerFragment.java ContainerFragment.java
public class ContainerFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_container, container, false);
getChildFragmentManager().beginTransaction().replace(R.id.fragment_container_root_view, new TestFragment()).commit();
return view;
}
}
saysomething_fragment.xml saidomething_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center_vertical"
android:id="@+id/speech_textview"
android:text="HELLLLOOO"
android:textSize="40dp" />
</LinearLayout>
Move new SaySomethingFragment()
to a separate variable. 将new SaySomethingFragment()
移动到单独的变量。
Use the setArguments method of that instance to pass a string extra 使用该实例的setArguments方法额外传递一个字符串
Within your Fragment, you can use getArguments() and get that string extra 在您的Fragment中,您可以使用getArguments()并额外获取该字符串
you can create a bundle and then set a string to pass as an argument to your fragment. 您可以创建一个包,然后设置一个字符串作为参数传递给您的片段。
Bundle bundle =new Bundle();
bundle.putString("variable","value");
you will need to set the arguments for the fragment 您将需要设置片段的参数
.setArguments(bundle);
then in your fragment´s onCreate method you can retrieve the data 然后在片段的onCreate方法中可以检索数据
Bundle bundle=getArguments();
String value=bundle.getString("variable");
and then you just set the text to your textView 然后将文本设置为textView
Textview textview=(TextView)view.findViewById(R.id.textview);
textView.setText(value);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.