我在我的主要活動中實施Pakerfeldts' viewflow(特別是'DiffViewFlowExample'),我無法確定如何將點擊偵聽器添加到處於虛擬視圖中的ImageButton。如何將點擊監聽添加到充氣的XML按鈕?
那麼如何將點擊監聽器添加到位於dashboard_view中的圖像按鈕w/an @ id = regulatoryDocsBtn?
以下是代碼片段&我提前感謝您的任何幫助。
主要活動類別:
public class Home extends Activity {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* Setup the ViewFlow
* w/TitleFlow------------------------------------------
*/
ViewFlow viewFlow = (ViewFlow) findViewById(R.id.viewflow);
DiffAdapter vfAdapter = new DiffAdapter(this);
viewFlow.setAdapter(vfAdapter);
TitleFlowIndicator indicator = (TitleFlowIndicator) findViewById(R.id.viewflowindic);
indicator.setTitleProvider(vfAdapter);
viewFlow.setFlowIndicator(indicator);
}
DiffAdapter類:注意:膨脹的觀點@這個類的底部。
public class DiffAdapter extends BaseAdapter implements TitleProvider {
private static final int VIEW1 = 0;
private static final int VIEW2 = 1;
private static final int VIEW_MAX_COUNT = VIEW2 + 1;
private final String[] names = { "DashBoard", "ToolBox" };
private LayoutInflater mInflater;
public DiffAdapter(Context context) {
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override(S)...............
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int view = getItemViewType(position);
if (convertView == null) {
switch (view) {
case VIEW1:
convertView = mInflater.inflate(R.layout.dashboard_view, null);
break;
case VIEW2:
convertView = mInflater.inflate(R.layout.toolbox_view, null);
break;
}
}
return convertView;
}
A的視圖之一的片段:dashboard_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#10000000"
android:stretchColumns="*" >
<!-- Row 1 -->
<TableRow android:layout_weight="1" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageButton
android:id="@+id/regulatoryDocsBtn"
style="@style/dashBoardimageBtn"
android:src="@drawable/ic_launcher_regulatory" />
<TextView
style="@style/dashBoardTxt"
android:layout_below="@id/regulatoryDocsBtn"
android:text="Regulatory Docs" />
</RelativeLayout>
.....
</TableRow>
這就是我最初試過的想法,但它不工作 - 只是NPE。 thnx雖然。 – CelticParser 2011-12-14 23:20:47
什麼不工作?因爲它應該工作:)你應該在你的activity中調用findViewById(),或者像activity.findViewById(..)那樣做。 – 2011-12-14 23:35:33