java - 选定的行项目在 ListActivity 中保持突出显示

标签 java android listview onitemclicklistener listactivity

我正在创建一个应用程序,它以 ListView 的形式显示手机上的 apk 文件。我在这个应用程序中使用 ListActivity。当我在 ListView 中选择一行时,它会突出显示,并且与 apk 文件对应的应用程序打开。我不希望该应用程序打开,我希望该行在选择它后保持突出显示状态,并且没有其他进一步的操作。我尝试使用
1. setOnItemLongClickListener (但在这里不起作用)。
2. android:choiceMode="singleChoice" (不再工作)
3. getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); (工作,但单击后打开应用程序)
我不希望该行在一段时间后突出显示,我希望它在单击时突出显示并保持突出显示,并且我不希望应用程序打开,我希望该行在选择它后保持突出显示并且没有其他进一步的操作。我怎样才能实现这一点?

AllAppsActivity.java:

public class AllAppsActivity extends ListActivity {
    private PackageManager packageManager = null;
    private List<ApplicationInfo> applist = null;
    private ApplicationAdapter listadaptor = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apk);

        packageManager = getPackageManager();

        new LoadApplications().execute();

        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        getListView().setSelector(android.R.color.darker_gray);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        ApplicationInfo app = applist.get(position);
        try {
            Intent intent = packageManager
                    .getLaunchIntentForPackage(app.packageName);

            if (null != intent) {
                startActivity(intent);
            }
        } catch (ActivityNotFoundException e) {
            Toast.makeText(AllAppsActivity.this, e.getMessage(),
                    Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(AllAppsActivity.this, e.getMessage(),
                    Toast.LENGTH_LONG).show();
        }
    }

    private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
        ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>();
        for (ApplicationInfo info : list) {
            try {
                if (null != packageManager.getLaunchIntentForPackage(info.packageName)) {
                    applist.add(info);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return applist;
    }

    private class LoadApplications extends AsyncTask<Void, Void, Void> {
        private ProgressDialog progress = null;

        @Override
        protected Void doInBackground(Void... params) {
            applist = checkForLaunchIntent(packageManager.getInstalledApplications
                    (PackageManager.GET_META_DATA));
            listadaptor = new ApplicationAdapter(AllAppsActivity.this,
                    R.layout.snippet_list_row, applist);

            return null;
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }

        @Override
        protected void onPostExecute(Void result) {
            setListAdapter(listadaptor);
            progress.dismiss();
            super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {
            progress = ProgressDialog.show(AllAppsActivity.this, null,
                    "Loading application info...");
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }
    }
}

activity_apk.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/btn_playlist" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:divider="#242424"
        android:dividerHeight="1dp"
        />


</LinearLayout>

最佳答案

如果你想什么都不做,什么也不做。不要重写 onListItemClick 方法(将其删除),仅此而已。

关于java - 选定的行项目在 ListActivity 中保持突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46634065/

相关文章:

java - 已使用不兼容格式定义的属性

jquery - 如何在用户单击链接后突出显示列表项

c# - ListView 按列排序

java - 重新进入应用程序后将最后选择的图像加载到 ImageView 中

android - 如何在本地高效保存 Drawables?

android - 在 Android 的 ListView 中滚动时出现黑屏

java - 如何相对于标记翻转 ByteBuffer?

java - 为什么我的代码无法识别 addActionListener(ActionListener e) 方法?

java - charset=unicode 是 UTF-8、UTF-16 还是其他?

Java 多线程多请求方法