从 Listview 回到 MainActivity
Back from Listwiew to MainActivity
我从Fragment打开ExpandableListView,它是从MainActivity打开的。
在 ListView 中,在 Item 中单击一个按钮时会挂起一个侦听器。
通过单击此按钮,我想将一些值传输到 MainActivity 并将其发送到 WebView。
片段用beginTransaction()打开,写入addToBackStack(null)
当我按下系统按钮的后退按钮时,一切正常,但我只是想不通在我的 ExpandableAdapter class 中如何打开 ExpandableListView 以为此 Fragment 调用 OnBackPress .. ..
现在我只是在onClick 中调用MainActivity 并通过putExtra 传递值。但是这每次都会重新启动我的 Main Activity,但我不需要它,因为它已经存在,我只需要 return 就可以了。
从主调用片段Activity:
Fragment fragmentWeb = new MainFragment();
FragmentManager ft = getSupportFragmentManager();
ft.beginTransaction().replace(R.id.mainlayout, fragmentWeb, dataFrag).addToBackStack(null).commit();
MainFragment 的一部分:
public class MainFragment extends Fragment implements OnBackPressed {
......
getActivity().findViewById(R.id.list).setVisibility(ViewGroup.VISIBLE);
expListView = (ExpandableListView) getActivity().findViewById(R.id.lvExp);
new DownloadJason().execute();
private class DownloadJason extends AsyncTask<Void, Void, Void> {
// Get Json...
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
listAdapter = new myExpandableListAdapter(getActivity().getApplicationContext(), listHeader, listDataChild, listDataHeader, getActivity());
expListView.setAdapter(listAdapter);
}
}
myExpandableListAdapter 的一部分:
public class myExpandableListAdapter extends BaseExpandableListAdapter {
private Activity _activity;
private Context _context;
private List<String> _listDataHeader; // header titles
private List<Integer> imgHeader;
private HashMap<String, List<String>> _listDataChild;
private HashMap<String, List<String>> _listData;
private Integer imgType1,imgType2,imgType3,imgType4,imgType5;
private FragmentManager manager;
public myExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData,
HashMap<String, List<String>> listData,
Activity activity) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
this._listData = listData;
this._activity = activity;
}
......
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
final String markerId = (String) getData(groupPosition, 0);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item2, null);
}
TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
ImageButton btnMarker = (ImageButton) convertView.findViewById(R.id.imageButton);
btnMarker.setImageResource(R.drawable.ic_place_black);
btnMarker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(_activity, MainActivity.class);
intent.putExtra("markerId", markerId);
_activity.startActivity(intent);
}
});
return convertView;
}
.....
}
创建两个接口。
1.MainFragmentListener 用于与 activity/fragment
通信
2.AdapterClickListener 用于与 adapter/fragment.
通信
您必须像这样初始化为 MainFragmentListener。
MainActivity implements MainFragmentListener{
..
}
在 MainFragment 中创建 AdapterClickListener。
像这样。
MainFragmentListener mainFragmentListener = null;
然后给MainFragmentListener片段。
Fragment fragmentWeb = new MainFragment();
fragmentWeb.adapterClickListener = this;
在 MainFragment 中创建 AdapterClickListener 并 在 onCreatedView() 中初始化
然后把这个接口给适配器。
主要片段
AdapterClickListener AdapterClickListener = null;
适配器构造函数
public myExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData,
HashMap<String, List<String>> listData,
Activity activity,AdapterClickListener adapterClickListener) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
this._listData = listData;
this._activity = activity;
this.adapterClickListener = adapterClickListener;
}
现在你将你想要的数据发送到主activity接口。
btnMarker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
adapterClickListener.markerClicked(*****);
}
});
非常感谢@denizs!!!
他的提示提示我朝着正确的方向前进。但并不是所有的东西都适合我,例如,我不明白为什么:
Fragment fragmentWeb = new MainFragment();
fragmentWeb.adapterClickListener = this;
但总的来说,他写的是真的,解决了我的问题。
我认为他的回答很详细,并将其标记为已接受。
这里是我 post 我的 最终版本:.
创建界面:
OnMarkerListener
public interface OnMarkerListener{
public void onMarkerRead(String marker);
}
主要活动:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, OnMarkerListener {
@Override
public void onMarkerRead(String marker) {
onBackPressed();
inject(WebView, "openMarker(" + marker + ");", null);
}
}
主要片段:
public class MainFragment extends Fragment implements OnBackPressed {
OnMarkerListener markerListener;
myExpandableListAdapter listAdapter;
....
@Override
public void onAttach(Context context){
super.onAttach(context);
Activity activity = (Activity) context;
try {
markerListener = (OnMarkerListener) activity;
}catch (ClassCastException e){
throw new ClassCastException(activity.toString()+ " must override onMarkerListener..");
}
}
private class DownloadJason extends AsyncTask<Void, Void, Void> {
......
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
pDialog.dismiss();
//call constructor
listAdapter = new myExpandableListAdapter(getActivity().getApplicationContext(),
listHeader, listDataChild, listDataHeader, getActivity(),
markerListener);
// setting list adapter
expListView.setAdapter(listAdapter);
}
}
和我的ExpanableListAdapter:
private OnMarkerListener _markerListener;
public myExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData,
HashMap<String, List<String>> listData,
Activity activity, OnMarkerListener markerListener){
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
this._listData = listData;
this._activity = activity;
this._markerListener = markerListener;
}
.....
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
final String markerId = (String) getData(groupPosition, 0);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item2, null);
}
ImageButton btnMarker = (ImageButton) convertView.findViewById(R.id.imageButton);
btnMarker.setImageResource(R.drawable.ic_place_black);
btnMarker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
_markerListener.onMarkerRead(markerId);
}
});
return convertView;
}
我从Fragment打开ExpandableListView,它是从MainActivity打开的。
在 ListView 中,在 Item 中单击一个按钮时会挂起一个侦听器。
通过单击此按钮,我想将一些值传输到 MainActivity 并将其发送到 WebView。
片段用beginTransaction()打开,写入addToBackStack(null)
当我按下系统按钮的后退按钮时,一切正常,但我只是想不通在我的 ExpandableAdapter class 中如何打开 ExpandableListView 以为此 Fragment 调用 OnBackPress .. ..
现在我只是在onClick 中调用MainActivity 并通过putExtra 传递值。但是这每次都会重新启动我的 Main Activity,但我不需要它,因为它已经存在,我只需要 return 就可以了。
从主调用片段Activity:
Fragment fragmentWeb = new MainFragment();
FragmentManager ft = getSupportFragmentManager();
ft.beginTransaction().replace(R.id.mainlayout, fragmentWeb, dataFrag).addToBackStack(null).commit();
MainFragment 的一部分:
public class MainFragment extends Fragment implements OnBackPressed {
......
getActivity().findViewById(R.id.list).setVisibility(ViewGroup.VISIBLE);
expListView = (ExpandableListView) getActivity().findViewById(R.id.lvExp);
new DownloadJason().execute();
private class DownloadJason extends AsyncTask<Void, Void, Void> {
// Get Json...
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
listAdapter = new myExpandableListAdapter(getActivity().getApplicationContext(), listHeader, listDataChild, listDataHeader, getActivity());
expListView.setAdapter(listAdapter);
}
}
myExpandableListAdapter 的一部分:
public class myExpandableListAdapter extends BaseExpandableListAdapter {
private Activity _activity;
private Context _context;
private List<String> _listDataHeader; // header titles
private List<Integer> imgHeader;
private HashMap<String, List<String>> _listDataChild;
private HashMap<String, List<String>> _listData;
private Integer imgType1,imgType2,imgType3,imgType4,imgType5;
private FragmentManager manager;
public myExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData,
HashMap<String, List<String>> listData,
Activity activity) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
this._listData = listData;
this._activity = activity;
}
......
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
final String markerId = (String) getData(groupPosition, 0);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item2, null);
}
TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
ImageButton btnMarker = (ImageButton) convertView.findViewById(R.id.imageButton);
btnMarker.setImageResource(R.drawable.ic_place_black);
btnMarker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(_activity, MainActivity.class);
intent.putExtra("markerId", markerId);
_activity.startActivity(intent);
}
});
return convertView;
}
.....
}
创建两个接口。
1.MainFragmentListener 用于与 activity/fragment
通信2.AdapterClickListener 用于与 adapter/fragment.
通信您必须像这样初始化为 MainFragmentListener。
MainActivity implements MainFragmentListener{
..
}
在 MainFragment 中创建 AdapterClickListener。 像这样。
MainFragmentListener mainFragmentListener = null;
然后给MainFragmentListener片段。
Fragment fragmentWeb = new MainFragment();
fragmentWeb.adapterClickListener = this;
在 MainFragment 中创建 AdapterClickListener 并 在 onCreatedView() 中初始化 然后把这个接口给适配器。
主要片段
AdapterClickListener AdapterClickListener = null;
适配器构造函数
public myExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData,
HashMap<String, List<String>> listData,
Activity activity,AdapterClickListener adapterClickListener) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
this._listData = listData;
this._activity = activity;
this.adapterClickListener = adapterClickListener;
}
现在你将你想要的数据发送到主activity接口。
btnMarker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
adapterClickListener.markerClicked(*****);
}
});
非常感谢@denizs!!!
他的提示提示我朝着正确的方向前进。但并不是所有的东西都适合我,例如,我不明白为什么:
Fragment fragmentWeb = new MainFragment();
fragmentWeb.adapterClickListener = this;
但总的来说,他写的是真的,解决了我的问题。 我认为他的回答很详细,并将其标记为已接受。
这里是我 post 我的 最终版本:.
创建界面: OnMarkerListener
public interface OnMarkerListener{
public void onMarkerRead(String marker);
}
主要活动:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, OnMarkerListener {
@Override
public void onMarkerRead(String marker) {
onBackPressed();
inject(WebView, "openMarker(" + marker + ");", null);
}
}
主要片段:
public class MainFragment extends Fragment implements OnBackPressed {
OnMarkerListener markerListener;
myExpandableListAdapter listAdapter;
....
@Override
public void onAttach(Context context){
super.onAttach(context);
Activity activity = (Activity) context;
try {
markerListener = (OnMarkerListener) activity;
}catch (ClassCastException e){
throw new ClassCastException(activity.toString()+ " must override onMarkerListener..");
}
}
private class DownloadJason extends AsyncTask<Void, Void, Void> {
......
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
pDialog.dismiss();
//call constructor
listAdapter = new myExpandableListAdapter(getActivity().getApplicationContext(),
listHeader, listDataChild, listDataHeader, getActivity(),
markerListener);
// setting list adapter
expListView.setAdapter(listAdapter);
}
}
和我的ExpanableListAdapter:
private OnMarkerListener _markerListener;
public myExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData,
HashMap<String, List<String>> listData,
Activity activity, OnMarkerListener markerListener){
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
this._listData = listData;
this._activity = activity;
this._markerListener = markerListener;
}
.....
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
final String markerId = (String) getData(groupPosition, 0);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item2, null);
}
ImageButton btnMarker = (ImageButton) convertView.findViewById(R.id.imageButton);
btnMarker.setImageResource(R.drawable.ic_place_black);
btnMarker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
_markerListener.onMarkerRead(markerId);
}
});
return convertView;
}