我正在制作一个简单的应用程序,用户可以在列表视图中查看笔记列表(只是说明),当他点击某个笔记时,它会在新活动中打开整个笔记,在那里他可以编辑它等等。当我决定为每个列表项添加删除按钮时发生。在此之后,该行变得无法点击,而按钮是可点击的。 我读了几个类似的主题,但没有他们帮助我。我的问题是如何使按钮和列表视图“行”可点击? 我不太确定效率等。如果你认为这不是一个好方法,请告诉我,并可能给我更好的解决方案,否则我会坚持这个。
user_notes.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
list_note.xml:
<TextView
android:id="@+id/list_note_description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:paddingLeft="6dip"
android:paddingTop="6dip"
android:textSize="17dip"
android:textStyle="bold" />
<ImageButton
android:id="@+id/ibtn_delete_note"
android:onClick="test"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:layout_weight="0.2"
android:focusable="false"
android:scaleType="fitXY"
android:src="@drawable/delete_note_icon" />
public class UserNotes extends ListActivity{
private ProgressDialog progressDialog;
private ImageButton ibtn_delete_note;
private UserData userData;
private DBFacade dbFacade;
private JSONArray jsonArray;
private ArrayList<HashMap<String, String>> userNotes;
private static final String URL_LOAD_NOTES = "";
private static final String URL_UPDATE_NOTE = "";
private static final String TAG = "UserNotes";
//Drawer attributes
String[] drawer_menu_titles;
private ListView drawer_list;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.user_notes);
drawer_menu_titles = getResources().getStringArray(R.array.drawer_menu_array);
drawer_list = (ListView)findViewById(R.id.left_drawer);
drawer_list.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, drawer_menu_titles));
drawer_list.setOnItemClickListener(new DrawerItemClickListener());
userData = UserData.getInstance();
dbFacade = DBFacade.getInstance();
userNotes = new ArrayList<HashMap<String,String>>();
new loadUserNotes().execute();
ListView listView = getListView();
listView.setItemsCanFocus(true);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//int noteId = Integer.valueOf(((TextView) view.findViewById(R.id.list_note_id)).getText().toString());
Intent i = new Intent(UserNotes.this, DisplayNote.class);
i.putExtra("index", position);
Log.d(TAG, "Index: " + String.valueOf(position));
startActivityForResult(i, 100);
}
});
}
public void test(View v){
Log.d(TAG, "Clicked");
}
class loadUserNotes extends AsyncTask<String, String, String>{
private JSONObject jsonObject;
@Override
protected void onPreExecute(){
super.onPreExecute();
progressDialog = new ProgressDialog(UserNotes.this);
progressDialog.setMessage("Loading notes...");
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(true);
progressDialog.show();
}
@Override
protected String doInBackground(String... arg0) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("logged_in_user_id", String.valueOf(userData.getUserId())));
jsonObject = dbFacade.makeHttpRequest(URL_LOAD_NOTES, "GET", params);
Log.d(TAG, jsonObject.toString());
return null;
}
protected void onPostExecute(String file_url){
progressDialog.dismiss();
try{
if(jsonObject.getInt("success") == 1){
jsonArray = jsonObject.getJSONArray("notes");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject o = jsonArray.getJSONObject(i);
Note tempNote = new Note(o.getInt("note_id"), o.getString("description"), o.getString("data"));
userData.addNote(tempNote);
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("id", o.getString("note_id"));
hashMap.put("description", o.getString("description"));
userNotes.add(hashMap);
//Log.d(TAG, userNotes.get(0).toString());
}
}
Log.d(TAG, "Test: " + userNotes.get(0).toString());
Toast.makeText(UserNotes.this, jsonObject.getString("message"), Toast.LENGTH_LONG).show();
}
catch (JSONException e){
e.printStackTrace();
}
runOnUiThread(new Runnable(){
@Override
public void run() {
UserNotesAdapter listAdapter = new UserNotesAdapter(UserNotes.this,
userNotes,
R.layout.list_note,
new String[] {"description"},
new int[] {R.id.list_note_description});
/*
ListAdapter listAdapter = new SimpleAdapter(UserNotes.this,
userNotes,
R.layout.list_note,
new String[] {"description"},
new int[] {R.id.list_note_description});
*/
setListAdapter(listAdapter);
}
});
}
}
public class UserNotesAdapter extends SimpleAdapter{
public UserNotesAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
view.setClickable(true);
view.setFocusable(true);
view.setBackgroundResource(android.R.drawable.menuitem_background);
return view;
}
}
提前致谢!
答案 0 :(得分:0)
在列表视图项(行)布局中添加android:descendantFocusability="blocksDescendants"
。这将启用行项控件单击以及列表项单击。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" android:descendantFocusability="blocksDescendants">
<TextView
android:id="@+id/list_note_description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:paddingLeft="6dip"
android:paddingTop="6dip"
android:textSize="17dip"
android:textStyle="bold" />
<ImageButton
android:id="@+id/ibtn_delete_note"
android:onClick="test"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:layout_weight="0.2"
android:focusable="false"
android:scaleType="fitXY"
android:src="@drawable/delete_note_icon" />
</LinearLayout>
一旦尝试这个......