简体   繁体   中英

How to add a “multiple choice” in list of installed android applications with icons?

I can not create multiple choice, because it's not exactly ListView. Please tell me how you can solve the problem. Thanks!

Example of work: enter image description here

I need this result: enter image description here

Oh...I tried to solve the problem in different ways, but I did not succeed. I hope that one of you will give me advice :) Thanks!

Activity: (MyExample.java)

package com.example.diplom.tester;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MyExample extends ListActivity {

    private PackageManager packageManager = null;
    private List<ApplicationInfo> applist = null;
    private AddAdapter listadapter = null;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_example);

        packageManager = getPackageManager();
        new LoadApplications().execute();
    }

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


    private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list){
        ArrayList<ApplicationInfo> appList = new ArrayList<ApplicationInfo>();
        for(ApplicationInfo info : list){
            try{
                if(packageManager.getLaunchIntentForPackage(info.packageName) != null){
                    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... voids) {

            applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
            listadapter = new AddAdapter(MyExample.this,R.layout.list_item,applist);
            return null;
        }
        @Override
        protected void onPostExecute(Void aVoid) {

            setListAdapter(listadapter);
            progress.dismiss();
            super.onPostExecute(aVoid);
        }
        @Override
        protected void onPreExecute() {
            progress = ProgressDialog.show(MyExample.this,null,"Loading...");
            super.onPreExecute();
        }
    }

}

AddAdapter.java

package com.example.diplom.tester;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import java.util.List;

/**
 * Created by 1 on 22.03.2017.
 */
public class AddAdapter extends ArrayAdapter<ApplicationInfo> {

    private List<ApplicationInfo> appList = null;
    private Context context;
    private PackageManager packageManager;

    public AddAdapter(Context context, int resource, List<ApplicationInfo> objects) {

        super(context, resource, objects);
        this.context = context;
        this.appList = objects;
        packageManager = context.getPackageManager();

    }

    @Override
    public int getCount() {
        return ((null!=appList)? appList.size() : 0);
    }

    @Override
    public ApplicationInfo getItem(int position) {
        return ((null!=appList)? appList.get(position) : null);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;

        if(null==view){
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(R.layout.list_item,null);
        }

        ApplicationInfo data = appList.get(position);

        if(null!=data){
            TextView appName = (TextView) view.findViewById(R.id.app_name);
            ImageView iconView = (ImageView) view.findViewById(R.id.app_icon);


            appName.setText(data.loadLabel(packageManager));
            iconView.setImageDrawable(data.loadIcon(packageManager));


        }
        return view;
    }
}

listItem.xml :

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

    <ImageView
        android:id="@+id/app_icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:padding="3dp"
        android:scaleType="centerCrop"
        android:contentDescription="@null"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:padding="5dp">

        <TextView
            android:id="@+id/app_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:textStyle="bold"/>
    </LinearLayout>

</LinearLayout>

activity_my_example.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.diplom.tester.MyExample">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@android:id/list"
        >

    </ListView>

</RelativeLayout>

I think you are almost there. Extend "listItem.xml" to have a checkbox at the end of it, then, extend your "AddAdapter" class by adding a listener to checkbox selection. You can propagate the event to the activity from the "AddAdapter" as well.

Hope this helps.

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