我有一个列表视图,我已成功将列表本身设为可点击。现在在列表中,我也有一个按钮。我尝试了很多方法,但我无法点击按钮。这是我的列表的样子
========================================
content 1 [button remove]
========================================
content 2 [button remove]
========================================
content 3 [button remove]
========================================
我可以将内容(例如:内容1,内容2)点击;单击内容本身时,我已成功打开一个对话框。现在我想让[button remove]也可以点击,这应该会触发一个toast。然而,无论我尝试什么,似乎我无法让我的应用程序检测到已经点击了按钮删除。
这是我的代码的一部分: mylist.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dip">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:id="@+id/tvDescription" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:id="@+id/tvRating" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remove"
android:focusable="false"
android:clickable="true"
android:id="@+id/btnRemoveExpenses" />
</LinearLayout>
daily.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="50dip"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:id="@+id/ll_expensesDaily"
android:baselineAligned="false">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
MyOwnList.java
private ListView lvExpenses;
public class MyOwnList extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
final View rootView = inflater.inflate(R.layout.daily, container, false);
lvExpenses = (ListView) rootView.findViewById(android.R.id.list);
lvExpenses.setItemsCanFocus(true);
lvExpenses.setOnItemClickListener(expensesClickHandler);
return rootView;
}
private AdapterView.OnItemClickListener expensesClickHandler = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id)
{
Toast.makeText(getActivity(), " i: " + position + " long: " + id, Toast.LENGTH_SHORT).show();
showEditExpensesDialog(expensesID.get(position));
}
};
}
答案 0 :(得分:1)
也许您应该使用SwipeToDimissUndoList而不是那些按钮.. 看这里: https://github.com/timroes/SwipeToDismissUndoList
这是一个样本 -
SwipeDismissList.OnDismissCallback callback = new SwipeDismissList.OnDismissCallback() {
// Gets called whenever the user deletes an item.
public SwipeDismissList.Undoable onDismiss(AbsListView listView, final int position) {
// Get your item from the adapter (mAdapter being an adapter for MyItem objects)
final MyItem deletedItem = mAdapter.getItem(position);
// Delete item from adapter
mAdapter.remove(deletedItem);
// Return an Undoable implementing every method
return new SwipeDismissList.Undoable() {
// Method is called when user undoes this deletion
public void undo() {
// Reinsert item to list
mAdapter.insert(deletedItem, position)
}
// Return an undo message for that item
public String getTitle() {
return deletedItem.toString() + " deleted";
}
// Called when user cannot undo the action anymore
public void discard() {
// Use this place to e.g. delete the item from database
finallyDeleteFromSomeStorage(deletedItem);
}
};
}
};
答案 1 :(得分:0)
在适配器的按钮上添加一个on click侦听器。
答案 2 :(得分:0)
我会忽略on item click侦听器并在xml中执行。
添加到按钮:android:onClick =“removeBtnClicked”
添加到活动代码:
public void removeBtnClicked(View v){
//Do something in here to remove row / set the row visibility to gone
}