我在有兩個組的Activity中有ExpandableListView。每個組都有一個自定義的View。我可以點擊每個組中的按鈕,它可以工作。如果我點擊一個組的自定義視圖中的文本字段,則會顯示軟鍵盤。但是一旦我解僱它,任何一組都不會註冊點擊。鍵盤解散後,ExpanableListView失去焦點。一旦對話被解散,我怎樣才能將焦點重新放在列表視圖上?如果我摺疊並擴展組,它將被重置。我嘗試了各種聽衆無濟於事。ExpandableListView中的自定義視圖不會獲得點擊事件
我的課程和佈局稍微複雜一點,但我把它剝離到最低限度,這是我的代碼。
package com.test;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
public class TestOnClickActivity extends Activity
{
private static final String cLogTag = "TestOnClick";
private ExpadableAdapter iExpandableListAdapter;
private ExpandableListView iExpandableList;
private View[] iSearchViews;
private class ExpadableAdapter extends BaseExpandableListAdapter
{
private String[] iGroups =
{
"Search By Device",
"Search By Date",
};
@Override
public Object getChild(int theGroupPosition, int theChildPosition)
{
return "NA";
}
@Override
public long getChildId(int theGroupPosition, int theChildPosition)
{
return theGroupPosition;
}
@Override
public int getChildrenCount(int theGroupPosition)
{
return 1;
}
@Override
public View getChildView(int theGroupPosition,
int theChildPosition,
boolean isLastChild,
View theConvertView,
ViewGroup theParent)
{
return iSearchViews[theGroupPosition];
}
@Override
public Object getGroup(int theGroupPosition)
{
return iGroups[theGroupPosition];
}
@Override
public int getGroupCount()
{
return iGroups.length;
}
@Override
public long getGroupId(int theGroupPosition)
{
return theGroupPosition;
}
@Override
public View getGroupView(int theGroupPosition,
boolean theIsExpanded,
View theConvertView,
ViewGroup theParent)
{
if (theConvertView == null) {
Context theContext = TestOnClickActivity.this;
TextView theTV = new TextView(theContext,
null,
android.R.attr.textAppearanceMedium);
theTV.setText(iGroups[theGroupPosition]);
return theTV;
} else {
return theConvertView;
}
}
@Override
public boolean hasStableIds()
{
return true;
}
@Override
public boolean isChildSelectable(int theGroupPosition,
int theChildPosition)
{
return true;
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle theSavedState)
{
super.onCreate(theSavedState);
setContentView(R.layout.main);
iExpandableListAdapter = new ExpadableAdapter();
iExpandableList =
(ExpandableListView) findViewById(R.id.searchOptionsListView);
iExpandableList.setAdapter(iExpandableListAdapter);
iExpandableList.setGroupIndicator(null);
createSearchViews();
}
private void createSearchViews()
{
iSearchViews = new View[2];
LinearLayout theRowView;
// Create the Search By Device View
LayoutInflater theInflator = (LayoutInflater) getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
theRowView = new LinearLayout(this);
theInflator.inflate(R.layout.search1, theRowView, true);
iSearchViews[0] = theRowView;
// Create the Search By Date View
theRowView = new LinearLayout(this);
theInflator.inflate(R.layout.search2, theRowView, true);
iSearchViews[1] = theRowView;
}
}
佈局文件main.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" >
<ExpandableListView
android:id="@+id/searchOptionsListView"
android:divider="@android:color/transparent"
android:childDivider="#00000000"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ExpandableListView>
</LinearLayout>
search1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/searchByDeviceIdLinearLayout"
android:paddingLeft="36dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:text="@string/deviceId"/>
<EditText
android:id="@+id/deviceIdEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number" >
</EditText>
<ImageButton
android:id="@+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/search"
android:src="@drawable/ic_btn_search" />
</LinearLayout>
和search2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/searchByDateLinearLayout"
android:paddingLeft="36dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp">
<ImageButton
android:id="@+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/search"
android:src="@drawable/ic_btn_search" />
</LinearLayout>
我的團隊的名字每次點擊它都不會改變。你的代碼和我的代碼之間的區別似乎是我在onCreate中創建了一次行視圖,而你的代碼每次getGroupView和getChildView都被調用時,它們都會設置它們。我會嘗試一下,看看它是否有所作爲。 – Milind