它是如何工作的片段可扩展

时间:2014-01-01 09:05:51

标签: android fragment expandablelistview

  • 我怎么能做到这一点?
  • 我可以将适配器和片段分开,还是一切都必须在片段中?
  • 我需要你的帮助 谢谢

    package info.androidhive.slidingmenu;
    import java.text.NumberFormat;
    import java.util.ArrayList;
    import java.util.Locale;
    import android.app.Activity;
    import android.app.Fragment;
    import android.app.SearchManager;
    import android.content.Context;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseExpandableListAdapter;
    import android.widget.ExpandableListView;
    import android.widget.ImageView;
    import android.widget.SearchView;
    import android.widget.TextView;
    
    public class myfragment extends Fragment implements 
    SearchView.OnQueryTextListener, SearchView.OnCloseListener {
    
    private SearchView search; 
    private MyListAdapter listAdapter; 
    private ExpandableListView myList; 
    private ArrayList<Continent> continentList = new ArrayList<Continent>(); 
    
    /*
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.main, null);
    ExpandableListView elv = (ExpandableListView) v.findViewById(R.id.expandableList);
    elv.setAdapter(new MyListAdapter(getActivity(), continentList));
    SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE); 
    search = (SearchView) getActivity().findViewById(R.id.search); 
    search.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName())); 
    search.setIconifiedByDefault(false); 
    search.setOnQueryTextListener(this); 
    search.setOnCloseListener(this); 
    loadSomeData();
    return v;
    }
    
    */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
    //super.onCreate(savedInstanceState); 
    
    
    
    getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
    View v = inflater.inflate(R.layout.main,null);
    //getActivity().setContentView(R.layout.main); 
    SearchManager searchManager = (SearchManager) getActivity().getSystemService(getActivity().SEARCH_SERVICE); 
    search = (SearchView) getActivity().findViewById(R.id.search); 
    search.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName())); 
    search.setIconifiedByDefault(false); 
    search.setOnQueryTextListener(this); 
    search.setOnCloseListener(this); 
    //display the list 
    displayList(); 
    //expand all Groups 
    expandAll(); 
    return v;
    } 
    
    
    
    public boolean onCreateOptionsMenu(Menu menu) { 
    getActivity().getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
    } 
    //method to expand all groups 
    private void expandAll() { 
    int count = listAdapter.getGroupCount(); 
    for (int i = 0; i < count; i++){ 
    myList.expandGroup(i); 
    } 
    } 
    //method to expand all groups 
    private void displayList() { 
    //display the list 
    loadSomeData(); 
    //get reference to the ExpandableListView 
    myList = (ExpandableListView) getActivity().findViewById(R.id.expandableList); 
    //create the adapter by passing your ArrayList data 
    listAdapter = new MyListAdapter(getActivity(), continentList); 
    //attach the adapter to the list 
    myList.setAdapter(listAdapter); 
    
    //myList.setOnChildClickListener(myListItemClicked); 
    
    
    
    /*myList.setOnChildClickListener(new OnChildClickListener() {
    
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            final Country country = (Country) getChild(groupPosition, childPosition); 
    
            String product = country.getName();
            //Toast.makeText(context, product, Toast.LENGTH_SHORT).show();
    
    
    
          Intent i = new Intent(getApplicationContext(), SingleListItem.class);
          //.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
          i.putExtra("product", product );
    
          //i.putExtra("imv", R.id.option_icon); 
          startActivity(i);
        return true;
        }
        });*/
    }
    
    private void loadSomeData() { 
    ArrayList<Country> countryList = new ArrayList<Country>(); 
    Country country = new Country("1","heter",1,"im1"); 
    countryList.add(country); 
    country = new Country("2","bor",2,"im2"); 
    countryList.add(country); 
    country = new Country("3","snakker",3,"im3"); 
    countryList.add(country); 
    Continent continent = new Continent("Side 1",countryList); 
    continentList.add(continent); 
    countryList = new ArrayList<Country>(); 
    country = new Country("4","skriver",4,"im4"); 
    countryList.add(country); 
    country = new Country("5","leser",5,"im5"); 
    countryList.add(country); 
    country = new Country("6","regner",6,"im6"); 
    countryList.add(country); 
    continent = new Continent("Side 2",countryList); 
    continentList.add(continent); 
    } 
    @Override
    public boolean onClose() { 
    listAdapter.filterData(""); 
    expandAll(); 
    return false; 
    } 
    @Override
    public boolean onQueryTextChange(String query) { 
    listAdapter.filterData(query); 
    expandAll(); 
    return false; 
    } 
    @Override
    public boolean onQueryTextSubmit(String query) { 
    listAdapter.filterData(query); 
    expandAll(); 
    return false; 
    }
    
    public Object getChild(int groupPosition, int childPosition) { 
    ArrayList<Country> countryList = continentList.get(groupPosition).getCountryList(); 
    return countryList.get(childPosition); 
    } 
    
    public class MyListAdapter extends BaseExpandableListAdapter { 
    
    
    
    
        Context context =  getActivity().getApplicationContext(); 
        private ArrayList<Continent> continentList; 
        private ArrayList<Continent> originalList; 
    
        public MyListAdapter(Context context, ArrayList<Continent> continentList) { 
        this.context = context; 
        this.continentList = new ArrayList<Continent>(); 
        this.continentList.addAll(continentList); 
        this.originalList = new ArrayList<Continent>(); 
        this.originalList.addAll(continentList); 
    
    
        } 
    
    
    
        @Override
        public Object getChild(int groupPosition, int childPosition) { 
        ArrayList<Country> countryList = continentList.get(groupPosition).getCountryList(); 
        return countryList.get(childPosition); 
        } 
        @Override
        public long getChildId(int groupPosition, int childPosition) { 
        return childPosition; 
        } 
        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 
        View view, ViewGroup parent) { 
        final Country country = (Country) getChild(groupPosition, childPosition); 
        if (view == null) { 
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        view = layoutInflater.inflate(R.layout.child_row, null); 
        } 
        TextView code = (TextView) view.findViewById(R.id.code); 
        TextView name = (TextView) view.findViewById(R.id.name); 
        TextView population = (TextView) view.findViewById(R.id.population); 
        ImageView image=(ImageView) view.findViewById(R.id.imageView1);
        code.setText(country.getCode().trim()); 
        name.setText(country.getName().trim()); 
        population.setText(NumberFormat.getNumberInstance(Locale.US).format(country.getPopulation())); 
    
        image.setImageResource(context.getResources().getIdentifier("info.androidhive.slidingmenu:drawable/"+country.getimag(),null,null));
    
        /*view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String product = country.getName();
                //Toast.makeText(context, product, Toast.LENGTH_SHORT).show();
    
    
    
              Intent i = new Intent(context.getApplicationContext(), SingleListItem.class);
              //.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
              i.putExtra("product", product );
    
              //i.putExtra("imv", R.id.option_icon); 
              context.startActivity(i);
            }
          });
        */
        return view; 
        } 
    
        @Override
        public int getChildrenCount(int groupPosition) { 
        ArrayList<Country> countryList = continentList.get(groupPosition).getCountryList(); 
        return countryList.size(); 
        } 
        @Override
        public Object getGroup(int groupPosition) { 
        return continentList.get(groupPosition); 
        } 
        @Override
        public int getGroupCount() { 
        return continentList.size(); 
        } 
        @Override
        public long getGroupId(int groupPosition) { 
        return groupPosition; 
        } 
        @Override
        public View getGroupView(int groupPosition, boolean isLastChild, View view, 
        ViewGroup parent) { 
        Continent continent = (Continent) getGroup(groupPosition); 
        if (view == null) { 
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        view = layoutInflater.inflate(R.layout.group_row, null); 
        } 
        TextView heading = (TextView) view.findViewById(R.id.heading); 
        heading.setText(continent.getName().trim()); 
        return view; 
        } 
        @Override
        public boolean hasStableIds() { 
        return true; 
        } 
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) { 
        return true; 
        } 
        public void filterData(String query){ 
        query = query.toLowerCase(); 
        Log.v("MyListAdapter", String.valueOf(continentList.size())); 
        continentList.clear(); 
        if(query.isEmpty()){ 
        continentList.addAll(originalList); 
        } 
        else { 
        for(Continent continent: originalList){ 
        ArrayList<Country> countryList = continent.getCountryList(); 
        ArrayList<Country> newList = new ArrayList<Country>(); 
        for(Country country: countryList){ 
        if(country.getCode().toLowerCase().contains(query) || 
        country.getName().toLowerCase().contains(query)){ 
        newList.add(country); 
        } 
        } 
        if(newList.size() > 0){ 
        Continent nContinent = new Continent(continent.getName(),newList); 
        continentList.add(nContinent); 
        } 
        } 
        } 
        Log.v("MyListAdapter", String.valueOf(continentList.size())); 
        notifyDataSetChanged(); `enter code here`
        } 
    
       }
     }
    

1 个答案:

答案 0 :(得分:0)

how i can make that work ?

请说明代码中的问题是什么?

i can separate the adapter and the fragment or everything must be in the fragment?

由您决定是将创建适配器分开还是将其保存在同一个片段中。无论是为适配器类创建单独的文件还是仅将其保存到java文件中,这都没有任何区别。

您只需要将数据绑定到与ExpandableListView内的ListView相同的Fragment