正如您猜想的那样,一切正常,没有触发点击事件。在我开始之前,我已经阅读了很多关于此的主题,但是我一生都无法弄清楚。这是代码。
activity_main.xml(调用列表的主布局)
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
list_mainsegments_row.xml(自定义行)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/iconView"
style="@style/generalPagesLogoStyle"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="@dimen/tendpPadding"
android:scaleType="fitCenter"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/titleMainSegments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_weight="0.5"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="@dimen/generalTextSize"
/>
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:scaleType="fitCenter"
android:src="@drawable/arrow" />
</LinearLayout>
我的适配器.java
public class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int resource, int textViewResourceId,
String[] strings) {
super(context, resource, textViewResourceId, strings);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
// This is a single row
View row = inflater.inflate(R.layout.list_mainsegments_row, parent,
false);
String[] items = getContext().getResources().getStringArray(
R.array.segments_main);
ImageView iv = (ImageView) row.findViewById(R.id.iconView);
TextView tv = (TextView) row.findViewById(R.id.titleMainSegments);
tv.setText(items[position]);
organiseLayout(position, items, iv);
return row;
}
private void organiseLayout(int position, String[] items, ImageView iv) {
if (items[position].equals(getContext().getResources().getString(
R.string.segment_one))) {
iv.setImageResource(R.drawable.img1);
}
if (items[position].equals(getContext().getResources().getString(
R.string.segment_two))) {
iv.setImageResource(R.drawable.img2);
}
}
}
MainActivity.java
public class MainActivity extends ListActivity implements OnItemClickListener {
private MyAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new MyAdapter(this, android.R.layout.simple_list_item_1,
R.id.titleMainSegments, getResources().getStringArray(
R.array.segments_main));
setListAdapter(mAdapter);
getListView().setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
CharSequence text = "Item clicked";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this, text, duration);
toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL,
duration, duration);
toast.show();
} }
没有错误,所以 logCat 日志不会有用。一切顺利,但点击 ListView 时没有显示 Toast。
编辑:
样式.xml:
<!-- Global styles -->
<style name="horizLayoutHolder">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_gravity">center_horizontal</item>
</style>
<!-- This is for the text based pages -->
<style name="general_pages">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:orientation">vertical</item>
<item name="android:paddingBottom">@dimen/generalPadding</item>
<item name="android:paddingLeft">@dimen/generalPadding</item>
<item name="android:paddingRight">@dimen/generalPadding</item>
<item name="android:paddingTop">@dimen/tendpPadding</item>
</style>
<style name="generalPagesTitleStyle">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginBottom">@dimen/tendpPadding</item>
<item name="android:textColor">#000</item>
<item name="android:textSize">@dimen/generalHeadingTextSize</item>
<item name="android:textStyle">bold</item>
</style>
<style name="generalPageScrollView">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="generalPageMainText">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#000</item>
<item name="android:textSize">@dimen/generalSmallFontSize</item>
</style>
<style name="generalPagesLogoStyle">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginBottom">@dimen/tendpPadding</item>
" <item name="android:src">@drawable/mainlogo</item>
</style>
<style name="generalActionButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">40dp</item>
<item name="android:layout_gravity">center_horizontal</item>
<item name="android:textSize">@dimen/generalVerySmallFontSize</item>
<item name="android:paddingRight">@dimen/generalPadding</item>
<item name="android:paddingLeft">@dimen/generalPadding</item>
</style>
<style name="generalTextBoxes">
<item name="android:layout_width">250dp</item>
<item name="android:layout_height">50dp</item>
<item name="android:layout_gravity">center_horizontal</item>
<item name="android:ems">10</item>
<item name="android:textSize">@dimen/generalSmallFontSize</item>
</style>
<!-- End of General / Full text based activities -->
<!-- I have used this in theme.xml -->
<style name="windowTitleBackgroundStyle">
<item name="android:background">#CCCCCC</item>
</style>
<style name="windowTitleStyle">
<item name="android:textColor">#000000</item>
<item name="android:padding">12dip</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">16sp</item>
</style>
<!-- End of Theme.xml stuff -->
主题.xml
<style name="Theme.myTheme.TitleBar" parent="android:Theme">
<item name="android:windowTitleBackgroundStyle">@style/windowTitleBackgroundStyle</item>
<item name="android:windowTitleStyle">@style/windowTitleStyle</item>
<item name="android:windowTitleSize">50dip</item>
<item name="android:background">#F8C845</item>
<item name="android:textColor">#555</item>
</style>