2013-09-22 83 views
3

我試圖將我的指示器設置爲textview旁邊,但我無法獲得正確的代碼來執行此操作。在ExpandableListView中更改指示器的位置導致問題

XML:

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

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_toLeftOf="@+id/lblListHeader" 
    android:src="@drawable/custom_arrow" /> 

這是代碼的唯一棧,我發現研究,但我無法得到它的工作:

//inside getGropView method 
View v; 
    if (convertView == null) { 
     v = newGroupView(isExpanded, parent); 
    } else { 
     v = convertView; 
    } 
    bindView(v, mGroupData.get(groupPosition), mGroupFrom, mGroupTo); 
    ((ImageView) v.findViewById(R.id.videos_group_indicator)) 
    .setImageResource(isExpanded?R.drawable.videos_chevron_expanded:R.drawable.videos_chevron_collapsed); 
    return v; 

的主要問題是,它「下劃線「newGroupView方法等,因爲我沒有這樣的方法,並沒有提到如何在我看到的example中創建它。

另外,一旦我得到解決方案,有人可以嘗試向我解釋這段代碼嗎?我已經閱讀了很多時間,並且我無法理解它,我是一個初學者。

回答

7

這裏有一個自定義擴展列表視圖的例子:

你想看看你在新的項目中使用這個代碼是什麼:

enter image description here

創建此代碼到活動

public class ExpActivity extends Activity 
{ 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // Находим наш list 
     ExpandableListView listView = (ExpandableListView)findViewById(R.id.exListView); 

     //Создаем набор данных для адаптера   
     ArrayList<ArrayList<String>> groups = new ArrayList<ArrayList<String>>(); 
     ArrayList<String> children1 = new ArrayList<String>(); 
     ArrayList<String> children2 = new ArrayList<String>(); 
     children1.add("Child_1"); 
     children1.add("Child_2"); 
     groups.add(children1); 
     children2.add("Child_1"); 
     children2.add("Child_2"); 
     children2.add("Child_3"); 
     groups.add(children2);  
     //Создаем адаптер и передаем context и список с данными 
     ExpListAdapter adapter = new ExpListAdapter(getApplicationContext(), groups); 
     listView.setAdapter(adapter); 
    } 
} 

將expandableListView添加到main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<ExpandableListView 
     android:id="@+id/exListView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:indicatorLeft="250dp" 
     android:indicatorRight="300dp" 
    /> 
</LinearLayout> 

創建一個適配器類的方法和參數

public class ExpListAdapter extends BaseExpandableListAdapter { 

    private ArrayList<ArrayList<String>> mGroups; 
    private Context mContext; 

    public ExpListAdapter (Context context,ArrayList<ArrayList<String>> groups){ 
     mContext = context; 
     mGroups = groups; 
    } 

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

    @Override 
    public int getChildrenCount(int groupPosition) { 
     return mGroups.get(groupPosition).size(); 
    } 

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

    @Override 
    public Object getChild(int groupPosition, int childPosition) { 
     return mGroups.get(groupPosition).get(childPosition); 
    } 

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

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

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

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, 
          ViewGroup parent) { 

     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.group_view, null); 
     } 

     if (isExpanded){ 
      //Изменяем что-нибудь, если текущая Group раскрыта 
     } 
     else{ 
      //Изменяем что-нибудь, если текущая Group скрыта 
     } 

     TextView textGroup = (TextView) convertView.findViewById(R.id.textGroup); 
     textGroup.setText("Group " + Integer.toString(groupPosition)); 

     return convertView; 

    } 

    @Override 
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 
          View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.child_view, null); 
     } 

     TextView textChild = (TextView) convertView.findViewById(R.id.textChild); 
     textChild.setText(mGroups.get(groupPosition).get(childPosition)); 

     Button button = (Button)convertView.findViewById(R.id.buttonChild); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Toast.makeText(mContext,"button is pressed",5000).show(); 
      } 
     }); 

     return convertView; 
    } 

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

的名字是相當有價值的信息。方法getGroupView和getChildView相應地返回View的p parentschildren。使用方法getGroupView中的參數isExpanded,例如,我們可以在不同狀態下更改組的背面。使用LayoutInflater我們爲我們的列表使用自定義佈局。

group_view.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"> 
    <TextView 
      android:id="@+id/textGroup" 
      android:layout_width="wrap_content" 
      android:layout_height="50dp" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="20dp" 
      android:textColor="@android:color/white" 
      android:textStyle="bold" 
      /> 
</LinearLayout> 

child_view.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"> 
    <TextView 
    android:id="@+id/textChild" 
    android:layout_width="wrap_content" 
    android:layout_height="40dp" 
    android:layout_marginLeft="20dp" 
    android:layout_marginTop="20dp" 
    android:textColor="@android:color/white" 
    /> 
    <Button 
    android:id="@+id/buttonChild" 
    android:layout_width="100dp" 
    android:layout_height="40dp" 
    android:layout_marginLeft="150dp" 
    android:layout_marginTop="10dp" 
    android:text="Button" 
    android:focusable="false" 
    /> 
</LinearLayout> 

在我們增加了一個按鈕,在適配器方法getChildView控制其按子視圖。以類似的方式,我們可以在group_view.xml中添加按鈕和其他元素。

另外,我們可以將聽衆鎖定在我們的列表中。

•OnChildClickListener - 按元素 •OnGroupCollapseListener上 - 崩潰組 •OnGroupExpandListener - 擴大集團 •OnGroupClickListener - 對組

現在讓我們來看看groupIndicater - 組狀態指示燈。它的位置指向main.xml,參數indicatorLeft,indicatorRigh - 對應於左右邊界。默認情況下,指標放在左側,什麼不是很酷。

我們也可以添加自定義圖片,爲此,我們需要在可用此代碼繪製的文件夾中添加indicator.xml。

接下來我們需要在main.xml中添加一行在我們的名單的參數

android:groupIndicator="@drawable/indicator" 
+0

感謝您的答覆時間

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_expanded="true" android:drawable="@drawable/imageOpen"> </item> <item android:state_empty="true" android:drawable="@drawable/imageClose"> </item> </selector> 

其中imageOpen是擴展羣 imageClose是摺疊組,我改變了XML,但問題仍然在java代碼中。它仍然強調「newGroupView」,因爲我的代碼中沒有這樣的方法。 – Guy

相關問題