目前我有项目A(作为Android库)和项目B.
在Project B中,我使用页面片段进行设计。在页面片段1中,片段实际上是从项目A(库)中调用的。
如何在项目A上收到项目B的onclick值。
//在项目B上声明片段页面
CustFragment fragment1 = new CustFragment ();
//项目A上的onclick监听器
mContactList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Cursor cursor = (Cursor) parent.getAdapter().getItem(position);
long rawId = cursor.getLong(cursor
.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
// how do i pass the value to B?
}
编辑:我用我这边的更多代码编辑了这篇文章
//在Project B上声明片段
public class MainPagerFragmentAdapter extends FragmentPagerAdapter {
ListFragment list;
SearchFragment search;
public MainPagerFragmentAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public int getCount() {
return 2;
}
public String getPageTitle(int position) {
switch (position) {
case 0: return AppSingleton.getCurrentActivity().getString(R.string.pager_appointments_tab);
case 1: return AppSingleton.getCurrentActivity().getString(R.string.explist_filter_text);
}
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
if (customerList == null)
list = new ListFragment();
return customerList;
case 1:
if (contactSearch == null)
search = new SearchFragment();
return contactSearch;
}
return null;
}
}
//项目A(库项目)上的onclick监听器
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
log.info("ContactsFragment onCreateView");
mContactFragmentView = inflater.inflate(R.layout.fragment_contacts,
container, false);
mContactList = (ListView) mContactFragmentView
.findViewById(R.id.contacts_list);
mContactList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Cursor cursor = (Cursor) parent.getAdapter().getItem(position);
long rawId = cursor.getLong(cursor
.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
// what have to done here to pass back to B dynamically
}
});
getLoaderManager().initLoader(0, null, this);
return mContactFragmentView;
}
答案 0 :(得分:0)
您想要设置在片段A上触发的回调。类似于:
在您的库中创建一个界面:
public interface MyInterface {
public void onRawIdAcquired(long rawId);
}
在FragmentA中的某个地方创建一个实例(并创建一个可以设置其值的setter):
public class FragmentA extends Fragment {
...
MyInterface i;
...
public void setListener(MyInterface i) {
this.i = i;
}
然后在rowId获取值时调用它:
long rawId = cursor.getLong(cursor.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
i.onRawIdAcquired(rawId);
现在在非库激活的PageFragment中,实现MyInterface并覆盖onRawIdAcquired:
public PageFragment extends Fragment implements MyInterface {
@Override
public void onRawIdAcquired(long rawId) {
**// you can now use the value of rawId here.**
}
最后,在实例化FragmentA时,在setListener中传递PageFragment:
FragmentA f = ...;
PageFragment myPageFragment = ...;
f.setListener(myPageFragment);
答案 1 :(得分:0)
在您的库项目中,创建从片段中调用变量的全局变量。如果你想轻松一点,你应该尝试这种方式。只是我的想法。希望得到帮助。 Android main project with library project - how to pass setting between projects