我有一个可点击的列表视图以及一个也可点击的按钮。问题是我不能让按钮做任何事情(它显示点击/突出显示但我的动作没有执行)。
这是我的Photo.class:
public class Photos extends MyBaseMenuOptions implements OnItemClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photos);
LayoutInflater inflater = Photos.this.getLayoutInflater();
final View buttonView = inflater.inflate(R.layout.list_item, null);
Button button = (Button) buttonView.findViewById(R.id.trash);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)",
Toast.LENGTH_LONG).show();
}
});
...
listView = (ListView) findViewById(R.id.list);
CustomListViewAdapter adapter = new CustomListViewAdapter(this,
R.layout.list_item, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
我在课程中也有一个@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
,但是当您点击列表视图时
这是我的list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:contentDescription="@string/image"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/icon"
android:paddingBottom="10dp"
android:textColor="#CC0033"
android:textSize="16dp" />
<TextView
android:id="@+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_toRightOf="@+id/icon"
android:paddingLeft="10dp"
android:textColor="#3399FF"
android:textSize="14dp" />
<Button
android:id="@+id/trash"
android:layout_width="70dp"
android:layout_height="70dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_alignParentRight="true"
android:focusable="false"
android:descendantFocusability="blocksDescendants"
/>
</RelativeLayout>
答案 0 :(得分:1)
所以我遇到了同样的问题 XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/beam_contact_entry_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/sv_standard_padding"
android:paddingRight="25dp"
android:background="?entry_background" >
<TextView
android:id="@+id/beam_contact_fragment_top_letter"
style="?primary_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?entry_background"
android:visibility="gone"
android:text="A"
android:textColor="@color/black" />
<include
android:id="@+id/beam_contact_fragment_top_line_fit"
android:layout_width="fill_parent"
android:layout_height="@dimen/sv_stripe_height_for_header"
android:layout_below="@+id/beam_contact_fragment_top_letter"
android:visibility="gone"
layout="@layout/svyaznoy_stripe" />
<FrameLayout
android:id="@+id/beam_contact_fragment_top_line_slim"
android:layout_width="match_parent"
android:layout_height="0.25dp"
android:layout_below="@+id/beam_contact_fragment_top_line_fit"
android:visibility="gone"
android:background="?divider_background" >
</FrameLayout>
<RelativeLayout
android:id="@+id/beam_contact_entry_contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/sv_standard_padding"
android:paddingBottom="@dimen/sv_standard_padding"
android:layout_alignParentLeft="true"
android:layout_below="@+id/beam_contact_fragment_top_line_slim"
android:layout_toLeftOf="@+id/beam_contact_entry_invite"
android:background="?entry_background" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:orientation="vertical"
android:layout_toRightOf="@+id/contact_info_avatar" >
<TextView
android:id="@+id/beam_contact_fragment_entry_text"
style="?primary_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alexander Great"
android:textSize="18sp" />
<TextView
android:id="@+id/beam_contact_fragment_entry_text_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-4dp"
android:text="mobile"
android:visibility="gone"
style="?secondary_text"
android:textSize="15sp" />
</LinearLayout>
<ru.fon.svyaznoy.ui.UserpicImageView
android:id="@+id/contact_info_avatar"
android:layout_width="@dimen/avatar_picture_side"
android:layout_height="@dimen/avatar_picture_side"
android:layout_alignParentLeft="true"
android:src="@drawable/sv_no_userpic" />
</RelativeLayout>
<LinearLayout
android:id="@+id/beam_contact_entry_invite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/beam_contact_entry_contact"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/beam_contact_entry_contact"
android:background="?entry_background"
android:orientation="horizontal" >
<ImageView
android:id="@+id/beam_contact_fragment_entry_right_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="?entry_background"
android:duplicateParentState="true"
android:src="@drawable/sv_svyaznoy_contact" />
</LinearLayout>
</RelativeLayout>
在方法getView
中的适配器中public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
View childview1 = view.findViewById(R.id.beam_contact_entry_contact)
childview1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// your text
}
});
View childview2 = view.findViewById(R.id.beam_contact_entry_invite);
childview2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// your text 2
}
});
return view;
}
答案 1 :(得分:0)
我认为你应该在适配器的按钮上设置onClickListener。
类似的东西:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//填充你的手机
Button button = (Button) buttonView.findViewById(R.id.trash);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)",
Toast.LENGTH_LONG).show();
}
});
}
答案 2 :(得分:-1)
您必须在适配器中为按钮编写click事件。 在适配器的get view方法中编写click事件的按钮,只有你才能得到按钮实例。 祝你好运
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflator inflater = LayoutInflator.from(context);
if(convertview == null {
convertview = inflater .inflate(R.layout.listitem, parent, false);
}
Button button = (Button) convertView.findViewById(R.id.trash);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)",
Toast.LENGTH_LONG).show();
}
});
return convertview;
这可行 祝你好运