2016-05-15 15 views
0

私は自分のコードでexpandablelistViewを使用しようとしていますが、いくつかの問題があります。このコードをフラグメントに実装しようとしています。このコードをフラグメントで使用できるようにするには?私は断片でそれを使用する必要があります。フラグメント上のExpandableListビュー

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()で行っている操作は、フラグメンテーションの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

これを試してください:あなたは(rootView.findViewByIdにonCreateViewとfindViewBy IDにのonCreate変更する必要がある)と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; 
     } 
    }); 
} 
} 

のための活動を得るでしょう、それが動作願っています。

関連する問題