如何将选定的 child 可扩展列表视图的视图数据从片段发送到 parent activity?
How to send selected child view data of expandable list view from fragment to parent activity?
我有一个具有 TextView tvCount 的 ParentActivity,一个具有可扩展列表视图的片段。我需要更新在可扩展列表视图中选择的 children 计数。
在我的片段中我有一个适配器 -
mExpandableListView = (ExpandableListView) view.findViewById(R.id.lvExp);
mAdapter = new ExpandableServiceListViewAdapter(getActivity(),
mGroups);
ExpandableServiceListViewAdapter.java
public class ExpandableServiceListViewAdapter extends BaseExpandableListAdapter {
private static SparseArray<GroupModel> mGroups;
public LayoutInflater mInflater;
public Activity mActivity;
public ExpandableServiceListViewAdapter(Activity act, SparseArray<GroupModel> groups) {
mActivity = act;
this.mGroups = groups;
mInflater = act.getLayoutInflater();
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return mGroups.get(groupPosition).children.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final GroupModel children = (GroupModel) getChild(groupPosition, childPosition);
TextView title, duration, gender, actualprice, finalprice;
final TextView tvCounter, tvTotalCost;
ImageView offericon = null;
final CheckBox checkBox;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.services_item, null);
}
title = (TextView) convertView.findViewById(R.id.tv_title);
duration = (TextView) convertView.findViewById(R.id.tv_duration);
gender = (TextView) convertView.findViewById(R.id.tv_gender);
actualprice = (TextView) convertView.findViewById(R.id.tv_actualprice);
finalprice = (TextView) convertView.findViewById(R.id.tv_finalprice);
offericon = (ImageView) convertView.findViewById(R.id.iv_offer);
checkBox = (CheckBox) convertView.findViewById(R.id.cb_check);
tvTotalCost = (TextView) mActivity.findViewById(R.id.tv_cost);
tvCounter = (TextView) mActivity.findViewById(R.id.tv_counter);
title.setText(children.getmServiceModel().getmServiceTitle());
duration.setText("• " + children.getmServiceModel().getmDuration() + "min");
gender.setText("• " + children.getmServiceModel().getmGender());
actualprice.setText("Rs " + children.getmServiceModel().getmActualPrice());
actualprice.setPaintFlags(actualprice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
finalprice.setText("\u20B9" + children.getmServiceModel().getmFinalPrice());
if (children.getmServiceModel().getmHasOffer()) {
offericon.setVisibility(View.VISIBLE);
} else {
offericon.setVisibility(View.INVISIBLE);
}
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkBox.toggle();
if (checkBox.isChecked()) {
((BaseActivity) mActivity).showToast("Yes");
BusinessActivity.mSelectedMap.put((groupPosition * 1000) + childPosition, children);
} else {
((BaseActivity) mActivity).showToast("No");
BusinessActivity.mSelectedMap.remove((groupPosition * 1000) + childPosition);
}
}
});
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return mGroups.get(groupPosition).children.size();
}
@Override
public Object getGroup(int groupPosition) {
return mGroups.get(groupPosition);
}
@Override
public int getGroupCount() {
return mGroups.size();
}
@Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.services_group_header, null);
}
GroupModel group = (GroupModel) getGroup(groupPosition);
TextView title = (TextView) convertView.findViewById(R.id.tv_title);
title.setText(group.getmTitle());
TextView count = (TextView) convertView.findViewById(R.id.tv_count);
count.setText("(" + group.getmCount() + ")");
ImageView iv = (ImageView) convertView.findViewById(R.id.iv_offer);
if (group.getmSubCategory().getmHasOffers())
iv.setVisibility(View.VISIBLE);
else
iv.setVisibility(View.INVISIBLE);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
我在这里尝试创建一个选定 children 的地图,我需要将其发送到 parent activity 以更新计数。
BusinessActivity.java 是 parent activity 其中有一个片段。
我在 activity.I 中定义了 mSelectedMap,选中时将条目添加到此地图,未选中时删除。
谁能帮我设置此适配器的 parent activity 中的计数值。
有 3 种方法可以解决您的问题。
- 使用全局静态变量,在您的片段中定义它的值并在您的 activity.
中显示它的值
- 在您的 activity 中创建方法并使用适当的参数从片段中调用它。
- 使用EventBus.
EventBus simplifies the communication between components. It performs well with Activities, Fragments, and background threads.
Here 是 EventBus 的一个很好的例子。
试试这个...
ExpandableServiceListViewAdapter.java
public class ExpandableServiceListViewAdapter extends BaseExpandableListAdapter {
private SparseArray<GroupModel> mGroups;
public LayoutInflater mInflater;
public Activity mActivity;
private OnChildClick onChildClick;
public ExpandableServiceListViewAdapter(Activity act, SparseArray<GroupModel> groups,
OnChildClick onChildClick) {
mActivity = act;
this.mGroups = groups;
this.onChildClick = onChildClick;
mInflater = act.getLayoutInflater();
}
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final GroupModel children = (GroupModel) getChild(groupPosition, childPosition);
........
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkBox.toggle();
if (checkBox.isChecked()) {
if (null != onChildClick) {
onChildClick.onClick((groupPosition * 1000) + childPosition, children, "Yes");
}
} else {
if (null != onChildClick) {
onChildClick.onClick((groupPosition * 1000) + childPosition, children, "No");
}
}
}
});
return convertView;
}
//other override methods
......
//interface to get selected item
public interface OnChildClick {
void onClick(int position, GroupModel children, String status);
}
}
YourFragment.java
public class YourFragment extends Fragment{
private OnItemSelectedListener mCallback;
private Activity activity;
ExpandableServiceListViewAdapter mAdapter;
ExpandableListView mExpandableListView;
.....
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnItemSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnItemSelectedListener");
}
}
// interface to update UI changes in BusinessActivity at runtime
public interface OnItemSelectedListener {
void onItemPicked(int position,GroupModel children, String status);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_expandable_list, container, false);
mExpandableListView = (ExpandableListView) view.findViewById(R.id.lvExp);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
mAdapter = new ExpandableServiceListViewAdapter(getActivity(),
mGroup, mOnChildClick);
}
private ExpandableServiceListViewAdapter.OnChildClick mOnChildClick
= new ExpandableServiceListViewAdapter.OnChildClick() {
@Override
public void onClick(int position,GroupModel children, String status) {
// Through interface
try {
((OnItemSelectedListener) activity).onItemPicked(position, children, status);
} catch (Exception cce) {
cce.printStackTrace();
}
//OR accsee directly
if(status.equalsIgnoreCase("Yes")){
((BusinessActivity)activity).mSelectedMap.put(position, children);
}else {
((BusinessActivity)activity).mSelectedMap.remove(position);
}
}
};
}
BusinessActivity.java
public class BusinessActivity extends Activity implements YourFragment.OnItemSelectedListener
{
.....
//Override the method here
@Override
public void onItemPicked(position, children, status)
{
//Do something with the position value passed back
Toast.makeText(activity, "status "+ status, Toast.LENGTH_LONG).show();
//Do something with the position value passed back
Toast.makeText(activity, "status "+ status, Toast.LENGTH_LONG).show();
if(status.equalsIgnoreCase("Yes")){
mSelectedMap.put(position, children)
}else {
mSelectedMap.remove(position);
}
}
.....
}
我有一个具有 TextView tvCount 的 ParentActivity,一个具有可扩展列表视图的片段。我需要更新在可扩展列表视图中选择的 children 计数。
在我的片段中我有一个适配器 -
mExpandableListView = (ExpandableListView) view.findViewById(R.id.lvExp);
mAdapter = new ExpandableServiceListViewAdapter(getActivity(),
mGroups);
ExpandableServiceListViewAdapter.java
public class ExpandableServiceListViewAdapter extends BaseExpandableListAdapter {
private static SparseArray<GroupModel> mGroups;
public LayoutInflater mInflater;
public Activity mActivity;
public ExpandableServiceListViewAdapter(Activity act, SparseArray<GroupModel> groups) {
mActivity = act;
this.mGroups = groups;
mInflater = act.getLayoutInflater();
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return mGroups.get(groupPosition).children.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final GroupModel children = (GroupModel) getChild(groupPosition, childPosition);
TextView title, duration, gender, actualprice, finalprice;
final TextView tvCounter, tvTotalCost;
ImageView offericon = null;
final CheckBox checkBox;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.services_item, null);
}
title = (TextView) convertView.findViewById(R.id.tv_title);
duration = (TextView) convertView.findViewById(R.id.tv_duration);
gender = (TextView) convertView.findViewById(R.id.tv_gender);
actualprice = (TextView) convertView.findViewById(R.id.tv_actualprice);
finalprice = (TextView) convertView.findViewById(R.id.tv_finalprice);
offericon = (ImageView) convertView.findViewById(R.id.iv_offer);
checkBox = (CheckBox) convertView.findViewById(R.id.cb_check);
tvTotalCost = (TextView) mActivity.findViewById(R.id.tv_cost);
tvCounter = (TextView) mActivity.findViewById(R.id.tv_counter);
title.setText(children.getmServiceModel().getmServiceTitle());
duration.setText("• " + children.getmServiceModel().getmDuration() + "min");
gender.setText("• " + children.getmServiceModel().getmGender());
actualprice.setText("Rs " + children.getmServiceModel().getmActualPrice());
actualprice.setPaintFlags(actualprice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
finalprice.setText("\u20B9" + children.getmServiceModel().getmFinalPrice());
if (children.getmServiceModel().getmHasOffer()) {
offericon.setVisibility(View.VISIBLE);
} else {
offericon.setVisibility(View.INVISIBLE);
}
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkBox.toggle();
if (checkBox.isChecked()) {
((BaseActivity) mActivity).showToast("Yes");
BusinessActivity.mSelectedMap.put((groupPosition * 1000) + childPosition, children);
} else {
((BaseActivity) mActivity).showToast("No");
BusinessActivity.mSelectedMap.remove((groupPosition * 1000) + childPosition);
}
}
});
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return mGroups.get(groupPosition).children.size();
}
@Override
public Object getGroup(int groupPosition) {
return mGroups.get(groupPosition);
}
@Override
public int getGroupCount() {
return mGroups.size();
}
@Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.services_group_header, null);
}
GroupModel group = (GroupModel) getGroup(groupPosition);
TextView title = (TextView) convertView.findViewById(R.id.tv_title);
title.setText(group.getmTitle());
TextView count = (TextView) convertView.findViewById(R.id.tv_count);
count.setText("(" + group.getmCount() + ")");
ImageView iv = (ImageView) convertView.findViewById(R.id.iv_offer);
if (group.getmSubCategory().getmHasOffers())
iv.setVisibility(View.VISIBLE);
else
iv.setVisibility(View.INVISIBLE);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
我在这里尝试创建一个选定 children 的地图,我需要将其发送到 parent activity 以更新计数。
BusinessActivity.java 是 parent activity 其中有一个片段。 我在 activity.I 中定义了 mSelectedMap,选中时将条目添加到此地图,未选中时删除。
谁能帮我设置此适配器的 parent activity 中的计数值。
有 3 种方法可以解决您的问题。
- 使用全局静态变量,在您的片段中定义它的值并在您的 activity. 中显示它的值
- 在您的 activity 中创建方法并使用适当的参数从片段中调用它。
- 使用EventBus.
EventBus simplifies the communication between components. It performs well with Activities, Fragments, and background threads.
Here 是 EventBus 的一个很好的例子。
试试这个...
ExpandableServiceListViewAdapter.java
public class ExpandableServiceListViewAdapter extends BaseExpandableListAdapter {
private SparseArray<GroupModel> mGroups;
public LayoutInflater mInflater;
public Activity mActivity;
private OnChildClick onChildClick;
public ExpandableServiceListViewAdapter(Activity act, SparseArray<GroupModel> groups,
OnChildClick onChildClick) {
mActivity = act;
this.mGroups = groups;
this.onChildClick = onChildClick;
mInflater = act.getLayoutInflater();
}
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final GroupModel children = (GroupModel) getChild(groupPosition, childPosition);
........
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkBox.toggle();
if (checkBox.isChecked()) {
if (null != onChildClick) {
onChildClick.onClick((groupPosition * 1000) + childPosition, children, "Yes");
}
} else {
if (null != onChildClick) {
onChildClick.onClick((groupPosition * 1000) + childPosition, children, "No");
}
}
}
});
return convertView;
}
//other override methods
......
//interface to get selected item
public interface OnChildClick {
void onClick(int position, GroupModel children, String status);
}
}
YourFragment.java
public class YourFragment extends Fragment{
private OnItemSelectedListener mCallback;
private Activity activity;
ExpandableServiceListViewAdapter mAdapter;
ExpandableListView mExpandableListView;
.....
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnItemSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnItemSelectedListener");
}
}
// interface to update UI changes in BusinessActivity at runtime
public interface OnItemSelectedListener {
void onItemPicked(int position,GroupModel children, String status);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_expandable_list, container, false);
mExpandableListView = (ExpandableListView) view.findViewById(R.id.lvExp);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
mAdapter = new ExpandableServiceListViewAdapter(getActivity(),
mGroup, mOnChildClick);
}
private ExpandableServiceListViewAdapter.OnChildClick mOnChildClick
= new ExpandableServiceListViewAdapter.OnChildClick() {
@Override
public void onClick(int position,GroupModel children, String status) {
// Through interface
try {
((OnItemSelectedListener) activity).onItemPicked(position, children, status);
} catch (Exception cce) {
cce.printStackTrace();
}
//OR accsee directly
if(status.equalsIgnoreCase("Yes")){
((BusinessActivity)activity).mSelectedMap.put(position, children);
}else {
((BusinessActivity)activity).mSelectedMap.remove(position);
}
}
};
}
BusinessActivity.java
public class BusinessActivity extends Activity implements YourFragment.OnItemSelectedListener
{
.....
//Override the method here
@Override
public void onItemPicked(position, children, status)
{
//Do something with the position value passed back
Toast.makeText(activity, "status "+ status, Toast.LENGTH_LONG).show();
//Do something with the position value passed back
Toast.makeText(activity, "status "+ status, Toast.LENGTH_LONG).show();
if(status.equalsIgnoreCase("Yes")){
mSelectedMap.put(position, children)
}else {
mSelectedMap.remove(position);
}
}
.....
}