2016-05-15 76 views
0

我想在我的代碼中使用expandablelistView,但我有一些問題。我試圖在片段中實現此代碼。可以幫助我將這段代碼用於片段嗎?我需要在片段中使用它。ExpandablelistView片段

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ExpandableListAdapter; 
import android.widget.ExpandableListView; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 

public class TextExpandable extends AppCompatActivity { 

    ExpandableListView expandableListView; 
    ExpandableListAdapter expandableListAdapter; 
    List<String> expandableListTitle; 
    HashMap<String, List<String>> expandableListDetail; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     expandableListView = (ExpandableListView) findViewById(R.id.expandableListView); 
     expandableListDetail = ExpandableListDataPump.getData(); 
     expandableListTitle = new ArrayList<String>(expandableListDetail.keySet()); 
     expandableListAdapter = new CustomExpandableListAdapter(this, expandableListTitle, expandableListDetail); 
     expandableListView.setAdapter(expandableListAdapter); 
     expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { 

      @Override 
      public void onGroupExpand(int groupPosition) { 
       Toast.makeText(getApplicationContext(), 
         expandableListTitle.get(groupPosition) + " List Expanded.", 
         Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() { 

      @Override 
      public void onGroupCollapse(int groupPosition) { 
       Toast.makeText(getApplicationContext(), 
         expandableListTitle.get(groupPosition) + " List Collapsed.", 
         Toast.LENGTH_SHORT).show(); 

      } 
     }); 

     expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
      @Override 
      public boolean onChildClick(ExpandableListView parent, View v, 
             int groupPosition, int childPosition, long id) { 
       Toast.makeText(
         getApplicationContext(), 
         expandableListTitle.get(groupPosition) 
           + " -> " 
           + expandableListDetail.get(
           expandableListTitle.get(groupPosition)).get(
           childPosition), Toast.LENGTH_SHORT 
       ).show(); 
       return false; 
      } 
     }); 
    } 

}

回答

0

只是片段擴展您的類。您在onCreate()中所做的操作與fragement的oncreateView()相同。

import android.app.Fragment; 
import android.os.Bundle; 

import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

import com.aksu.R; 

/** 
* A simple {@link Fragment} subclass. 
*/ 
public class BlankFragment extends Fragment { 


    ExpandableListView expandableListView; 
    ExpandableListAdapter expandableListAdapter; 
    List<String> expandableListTitle; 
    HashMap<String, List<String>> expandableListDetail; 
    public BlankFragment() { 
     // Required empty public constructor 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     return init(inflater.inflate(R.layout.activity_main, container, false)); 
    } 

    private View init(View inflate) { 
     expandableListView = (ExpandableListView) inflate.findViewById(R.id.expandableListView); 
     expandableListDetail = ExpandableListDataPump.getData(); 
     expandableListTitle = new ArrayList<String>(expandableListDetail.keySet()); 
     expandableListAdapter = new CustomExpandableListAdapter(inflate., expandableListTitle, expandableListDetail); 
     expandableListView.setAdapter(expandableListAdapter); 
     expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { 

      @Override 
      public void onGroupExpand(int groupPosition) { 
       Toast.makeText(inflate., 
         expandableListTitle.get(groupPosition) + " List Expanded.", 
         Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() { 

      @Override 
      public void onGroupCollapse(int groupPosition) { 
       Toast.makeText(inflate., 
         expandableListTitle.get(groupPosition) + " List Collapsed.", 
         Toast.LENGTH_SHORT).show(); 

      } 
     }); 

     expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
      @Override 
      public boolean onChildClick(ExpandableListView parent, View v, 
             int groupPosition, int childPosition, long id) { 
       Toast.makeText(
         inflate., 
         expandableListTitle.get(groupPosition) 
           + " -> " 
           + expandableListDetail.get(
           expandableListTitle.get(groupPosition)).get(
           childPosition), Toast.LENGTH_SHORT 
       ).show(); 
       return false; 
      } 
     }); 
    } 
} 
0

試試這個:你需要的onCreate更改爲onCreateView和findViewBy ID來rootView.findViewById()和getActivity將獲得活動爲您的自定義適配器

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ExpandableListAdapter; 
import android.widget.ExpandableListView; 
import android.widget.Toast; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 

public class TextExpandable extends Fragment { 

ExpandableListView expandableListView; 
ExpandableListAdapter expandableListAdapter; 
List<String> expandableListTitle; 
HashMap<String, List<String>> expandableListDetail; 
view rootView; 


public TextExpandable(){ 
//default constructor 
} 
@Override 
protected void onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    rootView = inflater.inflate(R.layout.history_courses,container,false); 


    expandableListView = (ExpandableListView) rootView.findViewById(R.id.expandableListView); 
    expandableListDetail = ExpandableListDataPump.getData(); 
    expandableListTitle = new ArrayList<String>(expandableListDetail.keySet()); 
    expandableListAdapter = new CustomExpandableListAdapter(getActivity(), expandableListTitle, expandableListDetail); 
    expandableListView.setAdapter(expandableListAdapter); 
    expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { 

     @Override 
     public void onGroupExpand(int groupPosition) { 
      Toast.makeText(getActivity(), 
        expandableListTitle.get(groupPosition) + " List Expanded.", 
        Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() { 

     @Override 
     public void onGroupCollapse(int groupPosition) { 
      Toast.makeText(getActivity(), 
        expandableListTitle.get(groupPosition) + " List Collapsed.", 
        Toast.LENGTH_SHORT).show(); 

     } 
    }); 

    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
     @Override 
     public boolean onChildClick(ExpandableListView parent, View v, 
            int groupPosition, int childPosition, long id) { 
      Toast.makeText(
        getActivity(), 
        expandableListTitle.get(groupPosition) 
          + " -> " 
          + expandableListDetail.get(
          expandableListTitle.get(groupPosition)).get(
          childPosition), Toast.LENGTH_SHORT 
      ).show(); 
      return false; 
     } 
    }); 
} 
} 

希望工程。