在我的应用程序,我有了两个片段在动作条的标签导航模式,活动就像Android开发者网站的例子。
In my app, I have an activity that has two fragments in actionbar tabs navigation mode, just like the android developer site example.
在我的第一个片段,我有一个列表视图(其中有它自己的适配器)和列表视图中的每一项都有一个称为+1按钮。我要刷新的第二个片段,显示在第一个片段,他们的+1按钮的点击列表视图中的项目。
in my first fragment I have a listview (which has it's own adapter ) and each item of the listview has a button called +1. I want to refresh the second fragment that shows the items in listview in first fragment that their +1 button's clicked.
我知道我必须使用的接口。但我无法弄清楚如何使用它们。我在哪里有定义接口?如何使用它?以及如何从活动的访问刷新第二个片段?
I know i have to use interfaces. but I cant figure how to use them. where do I have to define the interface? how to use it? and how to access it from the activity to refresh the second fragment?
快速帮助将是巨大的。谢谢。
a quick help would be great. thanks.
如果你想让它在列表项单击
If you want it on List Item Click
片段A:
public class FragmentA extends ListFragment {
OnItemSelectedListener mListener;
...
// Container Activity must implement this interface
public interface OnItemSelectedListener {
public void onItemSelected(int position);
}
...
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnItemSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnItemSelectedListener");
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
mCallback.onItemSelected(position);
}
}
ContainerActivity:
ContainerActivity:
public class ContainerActivity extends FragmentActivity
implements FragmentA.OnItemSelectedListener
{
//...
public void onItemSelected(int Position/*pass anything which u want*/)
{
SecondFragment second_fragment = (SecondFragment) getSupportFragmentManager().findFragmentById(R.id.fragmentB);
if(second_fragment !=null)
{
second_fragment.UpdateUI(Position);
}
}
}
第二个片段:
Second Fragment:
public class SecondFragment extends Fragment {
...
public void UpdateUI(Position)
{
}
}
希望这有助于。在点击每个列表项内按钮可能会有点困难,但尝试了同样的方法。可能是你写的界面声明,并在自定义的适配器调用。
Hope this helps. On click of a Button inside each listitem might be bit difficult, but try the same approach. May be you have to write the interface declaration and call in your custom adapter.