我正在使用片段并在 actionBar标签内创建一个包含列表视图和展开列表视图的简单应用列表视图可以正常工作,但我对可扩展列表视图有疑问我无法通过 MainActivity
调用它如果有人可以帮我解决我的问题,我会很感激
package com.exercise.AndroidNavigationTabs;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
public class AndroidNavigationTabsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tabA = actionBar.newTab();
tabA.setText("دليل الخدمة");
tabA.setTabListener(new TabListener<MyFragmentA>(this, "Tag A", MyFragmentA.class));
actionBar.addTab(tabA);
Tab tabB = actionBar.newTab();
tabB.setText("دليل التسّوق");
// tabB.setTabListener(new TabListener<MyFragmentB>(this, "Tag B", MyFragmentB.class));
// Intent intent = new Intent(getApplicationContext(), MyFragmentB.class);
// startActivity(intent);
// actionBar.addTab(tabB);
Tab tabC = actionBar.newTab();
tabC.setText("Tab C");
tabC.setTabListener(new TabListener<MyFragmentC>(this, "Tag C", MyFragmentC.class));
actionBar.addTab(tabC);
if (savedInstanceState != null) {
int savedIndex = savedInstanceState.getInt("SAVED_INDEX");
getActionBar().setSelectedNavigationItem(savedIndex);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putInt("SAVED_INDEX", getActionBar().getSelectedNavigationIndex());
}
public static class TabListener<T extends Fragment>
implements ActionBar.TabListener{
private final Activity myActivity;
private final String myTag;
private final Class<T> myClass;
public TabListener(Activity activity, String tag, Class<T> cls) {
myActivity = activity;
myTag = tag;
myClass = cls;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);
// Check if the fragment is already initialized
if (myFragment == null) {
// If not, instantiate and add it to the activity
myFragment = Fragment.instantiate(myActivity, myClass.getName());
ft.add(android.R.id.content, myFragment, myTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(myFragment);
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);
if (myFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(myFragment);
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
}
package com.exercise.AndroidNavigationTabs;
import android.app.Fragment;
import android.app.ListFragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MyFragmentA extends ListFragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] values = new String[] { "وظائف", "عقارات", "سيارات", "تجارة","ثقافة" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
/*
* @Override public View onCreateView(LayoutInflater inflater, ViewGroup
* container, Bundle savedInstanceState) { View myFragmentView =
* inflater.inflate(R.layout.fragment_a, container, false); return
* myFragmentView; }
*/
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Log.e("the List", "this is the list");
}
}
package com.exercise.AndroidNavigationTabs;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class MyFragmentB extends Activity {
List<String> groupList;
List<String> childList;
Map<String, List<String>> accessory_collection;
ExpandableListView expListView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_b);
createGroupList();
createCollection();
expListView = (ExpandableListView) findViewById(R.id.tasawouk_list);
final ExpandableListAdapter expListAdapter = new ExpandableListAdapter(
this, groupList, accessory_collection);
expListView.setAdapter(expListAdapter);
expListView.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
final String selected = (String) expListAdapter.getChild(
groupPosition, childPosition);
Toast.makeText(getBaseContext(), selected, Toast.LENGTH_LONG)
.show();
return true;
}
});
}
private void createGroupList() {
groupList = new ArrayList<String>();
groupList.add("أدوات _كهربائيّة");
groupList.add("رياضة");
groupList.add("أكسّسوار");
groupList.add("مطاعم");
groupList.add("ملابس");
}
private void createCollection() {
// preparing laptops collection(child)
String[] أكسّسوار = { "مجوهرات", "ساعات",
"نظارات" };
String[] electronic_gadget = { "كمبيوتر", "أدوات منزليّة", "هواتف" };
String[] restorant = {"مطاعم عائليّة", "fast-food"};
String[] clothes = {"رجالي", "نسائي", "ولاّدي"};
String[] sport = {"ملابس", "أدوات"};
accessory_collection = new LinkedHashMap<String, List<String>>();
for (String tasawouk_list : groupList) {
if (tasawouk_list.equals("أكسّسوار")) {
loadChild(أكسّسوار);
} else if (tasawouk_list.equals("أدوات _كهربائيّة"))
loadChild(electronic_gadget);
else if (tasawouk_list.equals("مطاعم"))
loadChild(restorant);
else if (tasawouk_list.equals("ملابس"))
loadChild(clothes);
else
loadChild(sport);
accessory_collection.put(tasawouk_list, childList);
}
}
private void loadChild(String[] tasawouk_list) {
childList = new ArrayList<String>();
for (String list : tasawouk_list)
childList.add(list);
}
}
package com.exercise.AndroidNavigationTabs;
import java.util.List;
import java.util.Map;
import com.exercise.AndroidNavigationTabs.R;
import com.exercise.AndroidNavigationTabs.R.layout;
import com.exercise.AndroidNavigationTabs.R.menu;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Activity context;
private Map<String, List<String>> accessories_collection;
private List<String> accessory;
// constructor
public ExpandableListAdapter(Activity context, List<String> ACCESSORY,
Map<String, List<String>> collection) {
this.context = context;
this.accessories_collection = collection;
this.accessory = ACCESSORY;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return accessories_collection.get(accessory.get(groupPosition)).get(
childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final String accessory = (String) getChild(groupPosition, childPosition);
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.child_item_tasawouk_list, null);
}
TextView item = (TextView) convertView.findViewById(R.id.tasawouk_list);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return accessories_collection.get(accessory.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return accessory.get(groupPosition);
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return accessory.size();
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
String accessory_name = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.group_item_tasawouk_list,
null);
}
TextView item = (TextView) convertView.findViewById(R.id.tasawouk_list);
item.setTypeface(null, Typeface.BOLD);
item.setText(accessory_name);
return convertView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}