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

时间:2017-10-08 17:37:27

标签: java android listview onitemclicklistener listactivity

我正在创建一个应用程序,它在列表视图中显示手机上的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>

2 个答案:

答案 0 :(得分:0)

如果你想做什么都不做。不要覆盖onListItemClick方法(删除它)以及所有方法。

答案 1 :(得分:0)

在适配器bean中定义一个标志,通过适配器中的标志设置高亮显示,在单击项目时更改标志和adapter.notifyDataSetChanged()。