简体   繁体   中英

Make a ListView item non-clickable

I need to recreate an option menu with a ListView. I've created 2 layouts, one for the "title of the sections" and one for the item itself.

Title:

<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" />

Settings Activity

<ListView
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:id="@+id/optionList" />

I need to set that only the titles will not have the OnClickListener.

Activity

@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;
}

How can I do that?

For future users, Overriding isEnabled() method of the adapter solve the problem.

@Override
public boolean isEnabled(int position) {

    if(position == 0){
       return false; // disable click on first item
    }

    return true;
}

在第二个布局上设置OnClickListener (在getView()方法中), OnItemClickListenerListView删除OnItemClickListener

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM