2014-06-16 20 views
6

Tworzę rozwijany widok listy w systemie Android. Dodałem trzy widoki tekstowe w układzie względnym, a dwa przyciski w układzie poziomym liniowym oznaczają, że te dwa przyciski są dodawane w jednym wierszu, więc jak mogę uruchomić zdarzenie kliknięcia na tych dwóch przyciskach?Jak obsługiwać zdarzenie typu "dziecko" w Expandablelistview w systemie Android?

Chcę wywołać inną czynność po kliknięciu przycisku w widoku listy rozwijanej. Ale nie miałem pojęcia, jak kliknąć na dziecko w ExpandableListView.

Oto mój plik XML.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@android:color/white" 
    android:padding="2dp" > 

    <TextView 
     android:id="@+id/TextView_Projectdetails" 
     android:layout_width="match_parent" 
     android:layout_height="25dp" 
     android:background="@drawable/button_shape" 
     android:paddingLeft="10sp" 
     android:textColor="@android:color/black" /> 

    <TextView 
     android:id="@+id/textOne" 
     android:layout_width="match_parent" 
     android:layout_height="25dp" 
     android:layout_below="@+id/TextView_Projectdetails" 
     android:layout_marginTop="2dp" 
     android:background="@drawable/button_shape" 
     android:paddingLeft="10sp" 
     android:textColor="@android:color/black" > 
    </TextView> 

    <TextView 
     android:id="@+id/textTwo" 
     android:layout_width="match_parent" 
     android:layout_height="25dp" 
     android:layout_below="@+id/textOne" 
     android:layout_marginTop="2dp" 
     android:background="@drawable/button_shape" 
     android:paddingLeft="10sp" 
     android:textColor="@android:color/black" > 
    </TextView> 

    <LinearLayout 
     android:id="@+id/linearlAYOUT" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textTwo" 
     android:layout_marginTop="5dp" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/button_EditedTask" 
      android:layout_width="0dp" 
      android:layout_height="27dp" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:background="@drawable/button_shape_two" 
      android:gravity="center" 
      android:paddingBottom="3sp" 
      android:paddingTop="3sp" 
      android:text="ASSIGN TASK" 
      android:focusable="false"  
      android:textColor="@android:color/white" 
      android:textSize="12sp" 
      android:textStyle="normal" /> 

     <Button 
      android:id="@+id/buttonViewTask" 
      android:layout_width="0dp" 
      android:layout_height="27dp" 
      android:layout_gravity="center_vertical|center_horizontal" 
      android:layout_marginLeft="3dp" 
      android:layout_weight="1" 
      android:background="@drawable/button_shape_one" 
      android:gravity="center" 
      android:paddingBottom="3sp" 
      android:paddingTop="3sp" 
      android:text="VIEW TASK" 
      android:focusable="false"  
      android:textColor="@android:color/white" 
      android:textSize="12sp" 
      android:textStyle="normal" /> 
    </LinearLayout> 

</RelativeLayout> 

List_Group plik xml -

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="8dp" 
    android:background="#000000"> 


    <TextView 
     android:id="@+id/lblListHeader" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft" 
     android:textSize="17dp" 
     android:textColor="#f9f93d" /> 

</LinearLayout> 

tutaj jest mój ExpandableListAdapter klasa -

public class ExpandableListAdapter extends BaseExpandableListAdapter { 

    private Context _context; 
    private List<String> _listDataHeader; // header titles 
    // child data in format of header title, child title 
    private HashMap<String, List<String>> _listDataChild; 
    Context context; 

    public ExpandableListAdapter(Context context, List<String> listDataHeader, 
      HashMap<String, List<String>> listChildData) { 
     this._context = context; 
     this._listDataHeader = listDataHeader; 
     this._listDataChild = listChildData; 
    } 

    public ExpandableListAdapter 
    (My_Project my_Project, List createGroupList, 
      int listGroup, String[] strings, int[] is, List createChildList, 
      int childItem, String[] strings2, int[] is2) 
    { 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public Object getChild(int groupPosition, int childPosition) 
    { 
     //this.get(groupPosition).getChildren().get(childPosition); 
     String strchildPosition = this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosition); 
     System.out.println("Child Position =" + strchildPosition); 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .get(childPosition); 
    } 

    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
     return childPosition; 
    } 

    @Override 
    public View getChildView(int groupPosition, final int childPosition, 
      boolean isLastChild, View convertView, ViewGroup parent) { 

     final String childText = (String) getChild(groupPosition, childPosition); 

     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.child_item, null); 
     } 

     TextView txtListChild = (TextView) convertView.findViewById(R.id.TextView_Projectdetails); 
     TextView txtOneListChild = (TextView)convertView.findViewById(R.id.textOne); 
     TextView txtTwoListChild = (TextView)convertView.findViewById(R.id.textTwo); 



     txtListChild.setText(childText); 
     txtOneListChild.setText(childText); 
     txtTwoListChild.setText(childText); 




     Button btnAssgnTask = (Button)convertView.findViewById(R.id.button_EditedTask); 
     btnAssgnTask.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       //Intent i=new Intent(context,Assign_Task.class); 
       //StartElementListener() 

      } 
     }); 


     Button btnViewTask = (Button)convertView.findViewById(R.id.buttonViewTask); 

     return convertView; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .size(); 
    } 

    @Override 
    public Object getGroup(int groupPosition) { 
     return this._listDataHeader.get(groupPosition); 
    } 

    @Override 
    public int getGroupCount() { 
     return this._listDataHeader.size(); 
    } 

    @Override 
    public long getGroupId(int groupPosition) { 
     return groupPosition; 
    } 

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
      View convertView, ViewGroup parent) { 
     String headerTitle = (String) getGroup(groupPosition); 
     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.list_group, null); 
     } 

     TextView lblListHeader = (TextView) convertView 
       .findViewById(R.id.lblListHeader); 
     lblListHeader.setTypeface(null, Typeface.BOLD); 
     lblListHeader.setText(headerTitle); 

     return convertView; 
    } 

    @Override 
    public boolean hasStableIds() { 
     return false; 
    } 

    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 

} 

Oto mój kod aktywny -

public class My_Project extends ExpandableListActivity 
{ 


    expListView = (ExpandableListView) findViewById(android.R.id.list); 

     expListView.setOnGroupClickListener(new OnGroupClickListener() 
     { 
      @Override 
       public boolean onGroupClick(ExpandableListView parent, View v, 
         int groupPosition, long id) 
       { 
        return false; 
       } 
      }); 


      expListView.setOnGroupExpandListener(new OnGroupExpandListener() { 

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


      expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() { 

       @Override 
       public void onGroupCollapse(int groupPosition) { 
        Toast.makeText(getApplicationContext(), 
          dtrProjectNAmeSize.length + " Collapsed", 
          Toast.LENGTH_SHORT).show(); 
       } 
      }); 


      expListView.setOnChildClickListener(new OnChildClickListener() { 

       @Override 
       public boolean onChildClick(ExpandableListView parent, View v, 
         int groupPosition, int childPosition, long id) { 
        // TODO Auto-generated method stub 

        int cp = (int) mAdapter.getChildId(groupPosition, childPosition); 
        if(cp == 3) 
        { 
         switch (cp) 
         { 
           case 3: 



           break; 

         } 
        } 


        return false; 

       } 
      }); 



      mAdapter = new SimpleExpandableListAdapter(
         this, 
         groupData, 
         R.layout.list_group, 
         new String[] { Project_Name }, 
         new int[] {R.id.lblListHeader }, 
         childData, 
         R.layout.child_item, 
         new String[] { Project_Date_Created , Project_End_Date , Project_Is_Active}, 
         new int[] { R.id.TextView_Projectdetails , R.id.textOne , R.id.textTwo } 
         ); 

       expListView.setAdapter(mAdapter); 

    } 



    } 

Odpowiedz

7

Można ustawić setOnChildClickListener chi d z rozwijanego listview, a następnie możesz przejść do innej aktywności zgodnie z wartością listy tablicowej, która jest używana do tworzenia rozszerzalnego widoku listy.

  menuExpandableListView 
        .setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 

         @Override 
         public boolean onChildClick(
           ExpandableListView parent, View v, 
           int groupPosition, int childPosition, 
           long id) { 
          GoCategory(mainMenusList.get(groupPosition) 
            .getPagesList().get(childPosition)); 
          return false; 
         } 
        }); 

Albo można to zrobić

@Override 
public View getChildView(final int groupPosition, final int childPosition, 
     boolean isLastChild, View convertView, ViewGroup parent) { 
    Page page =(Page) getChild(groupPosition, childPosition); 
    convertView = inflater.inflate(R.layout.child_list_layout, null); 
    Button button1=(Button)convertView.findViewById(R.id.button1); 
    button1.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

       Your code here.... 


     } 
    }); 

    return convertView; 
} 
+0

Co to jest GoCategory. – tazeenmulani

+0

To jest funkcja, gdzie chcesz przejść inną działalność .... –

+0

@Ad Abdul Gafur, Ale muszę dodać dwa przyciski w jednym wierszu na widoku listy dzieci obok siebie, tak jak wydarzenie click kliknij na tych przycisków. – tazeenmulani

-2

Co chcesz zrobić tutaj-

expListView.setOnChildClickListener(new OnChildClickListener() { 

       @Override 
       public boolean onChildClick(ExpandableListView parent, View v, 
         int groupPosition, int childPosition, long id) { 
        // TODO Auto-generated method stub 

        int cp = (int) mAdapter.getChildId(groupPosition, childPosition); 
        if(cp == 3) 
        { 
         switch (cp) 
         { 
           case 3: 



           break; 

         } 
        } 


        return false; 

       } 
      }); 

Ten słuchacz jest za pomocą kliknięcia na punkcie dziecka, zadzwoń startActivity(intent) w tej części kodu .