我有一個自定義列表視圖。但問題是,可點擊的列表視圖的唯一部分是文本(無法在右側/空白處)。我還添加了一些我的java代碼。爲什麼我的listview只在文本被點擊時纔會響應?
這是xml文件我一個ArrayAdapter使用:
?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
android:maxLines="1"
android:ellipsize="end"
android:textSize="50sp">
</TextView>
MainActivity.java
public class MainActivity extends AppCompatActivity {
static ArrayList<String> note;
ListView listView;
static ArrayList<String> notes = new ArrayList<>();
static ArrayAdapter arrayAdapter;
static Set<String> set;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
setTitle("Notely");
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
notes.add("");
SharedPreferences sharedPreferences = getSharedPreferences("mypackage", Context.MODE_PRIVATE);
if (set == null) {
set = new HashSet<String>();
} else {
set.clear();
}
set.addAll(notes);
arrayAdapter.notifyDataSetChanged();
sharedPreferences.edit().remove("notes").apply();
sharedPreferences.edit().putStringSet("notes", set).apply();
Intent i = new Intent(getApplicationContext(), EditNote.class);
i.putExtra("noteId", notes.size() - 1);
startActivity(i);
}
});
ListView listView = (ListView) findViewById(R.id.listView);
SharedPreferences sharedPreferences = this.getSharedPreferences("mypackage", Context.MODE_PRIVATE);
set = sharedPreferences.getStringSet("notes", null);
notes.clear();
if (set != null) {
notes.addAll(set);
} else {
notes.add("Example note");
set = new HashSet<String>();
set.addAll(notes);
sharedPreferences.edit().putStringSet("notes", set).apply();
}
arrayAdapter = new ArrayAdapter(this, R.liststyle, notes);
listView.setAdapter(arrayAdapter);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getApplicationContext(), EditNote.class);
i.putExtra("noteId", position);
startActivity(i);
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
new AlertDialog.Builder(MainActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Are you sure?")
.setMessage("Do you want to delete this note?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
notes.remove(position);
SharedPreferences sharedPreferences = MainActivity.this.getSharedPreferences("mypackage", Context.MODE_PRIVATE);
if (set == null) {
set = new HashSet<String>();
} else {
set.clear();
}
set.addAll(notes);
sharedPreferences.edit().remove("notes").apply();
sharedPreferences.edit().putStringSet("notes", set).apply();
arrayAdapter.notifyDataSetChanged();
}
})
.setNegativeButton("No", null)
.show();
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
return super.onOptionsItemSelected(item);
}
}
你曾經使用過的點擊監聽器? listviewItemclick? –
你是什麼意思的自定義'ListView'?你的適配器是自定義的還是你正在擴展'ListView'本身?你如何收聽列表項目上的點擊事件?請分享一些更相關的代碼 – Mithun
ArrayAdapter arrayAdapter = new ArrayAdapter (this,android.R.layout.simple_list_item_1,notes); –