2016-02-05 143 views
0

我創建了自定義ExpandableListView,但點擊它時不會展開。自定義ExpandableListView無法正常工作

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<ExpandableListView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/exp_lv" 
    ></ExpandableListView> 

group_items.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" > 


<TextView 
    android:layout_width="270sp" 
    android:layout_height="30sp" 
    android:layout_marginLeft="30sp" 
    android:id="@+id/title" 
    android:textIsSelectable="true" 
    /> 

<CheckBox 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/chk" 
    /> 

child_items.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 


<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/child" 
    android:textIsSelectable="true" 
    /> 
<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    > 
    <TextView 
    android:layout_width="120sp" 
    android:layout_height="wrap_content" 
    android:textIsSelectable="true" 
    android:id="@+id/dd" 
    android:textStyle="bold" 
    /> 

    <TextView 
     android:layout_width="200sp" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/dd" 
     android:textIsSelectable="true" 
     android:id="@+id/date" 
     /> 
</RelativeLayout> 

group.java

package com.example.expandablelistview; 

import java.util.ArrayList; 

public class group { 

private String Name; 
private boolean isSelected; 
private ArrayList<child> Items; 

public String getName() { 
    return Name; 
} 

public void setName(String name) { 
    Name = name; 
} 

public boolean isSelected() { 
    return isSelected; 
} 

public void setSelected(boolean isSelected) { 
    this.isSelected = isSelected; 
} 

public ArrayList<child> getItems() { 
    return Items; 
} 

public void setItems(ArrayList<child> items) { 
    Items = items; 
} 
} 

child.java

package com.example.expandablelistview; 

public class child { 

String child_title; 
String dd; 
String date; 
public String getChild_title() { 
    return child_title; 
} 
public void setChild_title(String child_title) { 
    this.child_title = child_title; 
} 
public String getDd() { 
    return dd; 
} 
public void setDd(String dd) { 
    this.dd = dd; 
} 
public String getDate() { 
    return date; 
} 
public void setDate(String date) { 
    this.date = date; 
} 
} 

MyBaseAdapter.java

package com.example.expandablelistview; 

import java.util.ArrayList; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseExpandableListAdapter; 
import android.widget.CheckBox; 
import android.widget.TextView; 

public class MyBaseAdapter extends BaseExpandableListAdapter{ 
Context context; 
ArrayList<group> group_al; 

public MyBaseAdapter(Context context,ArrayList<group> group_al) { 
this.context=context; 
this.group_al=group_al; 
} 
@Override 
public Object getChild(int groupPosition, int childPosition) { 
    ArrayList<child> chList = group_al.get(groupPosition).getItems(); 

    return chList.get(childPosition); 
} 

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

    return childPosition; 
} 

@Override 
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, 
     ViewGroup parent) { 
    child ch = (child) getChild(groupPosition, childPosition); 
     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) context 
        .getSystemService(context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.child_items, null); 
     } 
     TextView child = (TextView) convertView.findViewById(R.id.child); 
     TextView dd = (TextView) convertView.findViewById(R.id.dd); 
     TextView date= (TextView) convertView.findViewById(R.id.date); 

     child.setText(ch.getChild_title().toString()); 
     dd.setText(ch.getChild_title().toString()); 
     date.setText(ch.getChild_title().toString()); 

     return convertView; 
} 

@Override 
public int getChildrenCount(int groupPosition) { 
    ArrayList<child> chList = group_al.get(groupPosition).getItems(); 

    return chList.size(); 
} 

@Override 
public Object getGroup(int groupPosition) { 

    return group_al.get(groupPosition); 
} 

@Override 
public int getGroupCount() { 

    return group_al.size(); 
} 

@Override 
public long getGroupId(int groupPosition) { 

    return groupPosition; 
} 

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
    group gr = (group) getGroup(groupPosition); 
    long group_id = getGroupId(groupPosition); 
    if (convertView == null) { 
     LayoutInflater inf = (LayoutInflater) context 
       .getSystemService(context.LAYOUT_INFLATER_SERVICE); 
     convertView = inf.inflate(R.layout.group_items, null); 
    } 

    TextView title = (TextView) convertView.findViewById(R.id.title); 
    title.setText(gr.getName()); 
    CheckBox chk=(CheckBox) convertView.findViewById(R.id.chk); 
    chk.setFocusable(false); 

    return convertView; 
} 

@Override 
public boolean hasStableIds() { 

    return true; 
} 

@Override 
public boolean isChildSelectable(int arg0, int arg1) { 

    return false; 
} 

} 

MainActivity.java

package com.example.expandablelistview; 

import java.util.ArrayList; 
import android.os.Bundle; 
import android.app.Activity; 
import android.widget.ExpandableListView; 

public class MainActivity extends Activity { 
private MyBaseAdapter ExpAdapter; 
private ArrayList<group> ExpListItems; 
private ExpandableListView ExpandList; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    ExpandList = (ExpandableListView) findViewById(R.id.exp_lv); 
    ExpListItems = SetStandardGroups(); 
    ExpAdapter = new MyBaseAdapter(MainActivity.this, ExpListItems); 
    ExpandList.setAdapter(ExpAdapter); 
} 

private ArrayList<group> SetStandardGroups() { 

    ArrayList<group> list = new ArrayList<group>(); 
    group gru= new group(); 
    gru.setName("Tax 1"); 

    ArrayList<child> ch_list= new ArrayList<child>(); 
    child ch = new child(); 
    ch.setChild_title("Information of tax 1"); 
    ch.setDd("Due Date:"); 
    ch.setDate("22/02/2016"); 

    ch_list.add(ch); 

gru.setItems(ch_list); 
list.add(gru); 

    return list; 
} 
} 

當我運行應用程序,我可以看到ExpandableListView即Tax1的第一行與CheckBox旁邊,但是當我在該行則點擊它不擴大。

請大家幫幫我。 在此先感謝。

回答

0

請加:

android:focusable="false" 
android:focusableInTouchMode="false" 

您複選框

也從TextView中刪除:

android:textIsSelectable="true" 
+0

只是一個分鐘我會檢查 –

+0

我試過了,但也ExpandableListView不擴展 –

+0

你必須將它設置到你的複選框 –

0

**

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" > 
<TextView 
    android:layout_width="270sp" 
    android:layout_height="30sp" 
    android:layout_marginLeft="30sp" 
    android:id="@+id/title" 
    ****android:textIsSelectable="true"**// remove this line in group layout** 
    /> 
<CheckBox 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/chk" 
    /> 

**

+0

雅@Zeeshan汗我做了更改,但我面臨新的問題我在三個不同的TextView中的子項目我想設置以下參數:'ch.setChild_title(「稅務信息1」);''ch.setDd( 「Due Date:」);''ch.setDate(「22/02/2016」);'_But我可以看到_ **所有三個textviews中的稅收信息1 ** _ –

+0

您在所有子項目textview中獲得相同的值改變child.setText(ch.getChild_title()。toString()); dd.setText(ch.getDd()。toString()); date.setText(ch.getDate()。toString()); –

+0

好的老兄我得到的解決方案非常感謝 –

0
In MybaseAdapter please change method like this 

@Override 
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, 
          ViewGroup parent) { 
     child ch = (child) getChild(groupPosition, childPosition); 
     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) context 
        .getSystemService(context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.child_items, null); 
     } 
     TextView child = (TextView) convertView.findViewById(R.id.child); 
     TextView dd = (TextView) convertView.findViewById(R.id.dd); 
     TextView date= (TextView) convertView.findViewById(R.id.date); 

     child.setText(ch.getChild_title().toString()); 
     dd.setText(ch.getDd().toString()); 
     date.setText(ch.getDate().toString()); 

     return convertView; 
    } 
+0

非常感謝你我的解決方案 –

+0

我想存儲CheckBox的值(只有group_title和值的Date TextView)到數據庫,所以,我在checkbox上添加clickListener –

0

當我添加

android:focusable="false" 
android:focusableInTouchMode="false" 

到您的複選框,並

android:textIsSelectable="true" 

到TextView的那麼它的偉大工程 ,但現在我想存儲選中的複選框值(僅group_title和Date TextView的值)添加到數據庫,所以,我在複選框上添加clickListener

相關問題