我需要使用ListView重新创建一个选项菜单。我创建了2个布局,一个用于"部分的标题"和一个项目本身。
标题
<TextView
android:id="@+id/headerTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:textColor="@android:color/holo_green_light" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/holo_green_light" />
设置活动
<ListView
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="@+id/optionList" />
我需要设置只有标题不会有OnClickListener。
活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.context = this;
setContentView(R.layout.settings_layout);
setupActionBar();
this.optionsList = (ListView)findViewById(R.id.optionList);
this.optionsList.setHeaderDividersEnabled(false);
this.options = new ArrayList<>();
this.options.add(new Option("Generali", "", OptionTypes.HEADER));
this.options.add(new Option("Cartella download", parseDirectory(getSharedPreferences(MainActivity.SHARED_PREFS_NAME, MODE_PRIVATE)
.getString("DOWNLOADDIR",
Environment.getExternalStorageDirectory()
+ "/ItaSAMobileSubs")), OptionTypes.NORMAL));
this.optionsList.getChildAt(0).setOnClickListener(null);
OptionMenuAdapter optionMenuAdapter = new OptionMenuAdapter(this, this.options);
this.optionsList.setAdapter(optionMenuAdapter);
this.optionsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
Toast.makeText(getApplicationContext(), "Titolo", Toast.LENGTH_LONG).show();
break;
case 1:
mDirectoryTextView = (TextView)view.findViewById(R.id.optionDescription);
Intent directoryIntent = new Intent(context, DirectoryChooserActivity.class);
directoryIntent.putExtra(DirectoryChooserActivity.EXTRA_NEW_DIR_NAME,
"ItaSAMobileSubs");
startActivityForResult(directoryIntent, REQUEST_DIRECTORY);
break;
}
}
});
/*mDirectoryTextView = (TextView) findViewById(R.id.settingDescr);
mDirectoryTextView.setText(parseDirectory(getSharedPreferences(MainActivity.SHARED_PREFS_NAME, MODE_PRIVATE).getString("DOWNLOADDIR",
Environment.getExternalStorageDirectory()
+ "/ItaSAMobileSubs")));
Button directoryButton = (Button) findViewById(R.id.buttonDirectory);
directoryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});*/
}
private void setupActionBar() {
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setBackgroundDrawable(
getResources().getDrawable(R.drawable.banner));
getActionBar().setDisplayShowTitleEnabled(false);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_DIRECTORY) {
Log.i("Prova", String.format("Return from DirChooser with result %d",
resultCode));
if (resultCode == DirectoryChooserActivity.RESULT_CODE_DIR_SELECTED) {
mDirectoryTextView
.setText(parseDirectory(data
.getStringExtra(DirectoryChooserActivity.RESULT_SELECTED_DIR)));
SharedPreferences.Editor editor = getSharedPreferences(MainActivity.SHARED_PREFS_NAME, MODE_PRIVATE).edit();
editor.putBoolean("CHANGED", true);
editor.putString("DOWNLOADDIR", data.getStringExtra(DirectoryChooserActivity.RESULT_SELECTED_DIR));
editor.commit();
Log.i("Cambiata", "" + getSharedPreferences(MainActivity.SHARED_PREFS_NAME, MODE_PRIVATE).getBoolean("CHANGED", false));
Log.i("Directory", getSharedPreferences(MainActivity.SHARED_PREFS_NAME, MODE_PRIVATE).getString("DOWNLOADDIR", "-"));
}
}
}
private String parseDirectory(String path) {
try {
return path.replace(Environment.getExternalStorageDirectory().getCanonicalPath(), "");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
我该怎么做?
答案 0 :(得分:2)
对于将来的用户,覆盖适配器的isEnabled()方法可以解决问题。
@Override
public boolean isEnabled(int position) {
if(position == 0){
return false; // disable click on first item
}
return true;
}
答案 1 :(得分:0)
在第二个布局上设置OnClickListener
(使用getView()
方法)并从OnItemClickListener
移除ListView
。