[英]Refreshing a fragment on listview item click
我是零碎的新手,已经尝试了好几天了。 我试图让我的片段在listview单击时更新,并且与此非常相似: 片段中的片段不会刷新 ,左侧既有listview,右侧又有选项卡式ui。 这是我的片段之一:
public static class DescriptionFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_description, container, false);
text = ((TextView) rootView.findViewById(R.id.description));
text.setText(Html.fromHtml(description_text));
return rootView;
}
我怎么能称呼这个片段(从主要活动内部),所以onCreateView被更新并且textview的“文本”被更新了? 任何帮助是极大的赞赏
注意:listview不是片段,只是平板电脑的单独布局文件。 我唯一的片段(例如DescriptionFragment)用于选项卡
我假设您想显示listview,并且单击listitem时,值应该更新:
我如何称这种断裂(从主要活动内部),以便onCreateView得以更新并且textview的“文本”得到更新?
要在main活动中调用片段,必须在侧面main xml文件中定义片段,如下所示:
<?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="horizontal" >
<fragment
android:id="@+id/frag_series"
android:layout_width="200dip"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
class="com.example.demo_fragment.MyListFragment" />
</LinearLayout>
现在扩展Listframent并编码以填充listview数据,然后触发如下所示的click事件
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
DetailFragment frag = (DetailFragment) getFragmentManager().findFragmentById(R.id.frag_capt);
if (frag != null && frag.isInLayout()) {
frag.setText(getCapt(item));
}
}
然后检查状况并更新您的自拍,如下所示:
private String getCapt(String ship) {
if (ship.toLowerCase().contains("android")) {
return "Andy Rubin";
}
if (ship.toLowerCase().contains("IOS")) {
return "Steve Jobs";
}
return "???";
}
这是您可以调用片段的方式。
DescriptionFragment descriptionFragment = (DescriptionFragment )
getFragmentManager().findFragmentById(R.id.yourFragmentId);
您必须创建一个类似这样的监听器Interface
public interface FragmentTransactionListener {
public void updateFragment(/*some attributes*/);
}
然后,您必须在Fragment
上实现接口并重写updateFragment
方法。 在这种方法中,您可以添加单击列表项时将在Fragment
执行的代码。 像这样
public static class DescriptionFragment extends Fragment implements FragmentTransactionListener {
...
@Override
public void updateFragment(/*some attributes*/) {
//do some stuff
}
...
在FragmentActivity
创建片段时,请获取此侦听器的实例,并在单击列表项时调用它。 类似于此代码
FragmentTransactionListener listener = instance of your fragment;
//in your list item you then call this
listener.updateFragment(/*some attributes*/);
希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.