简体   繁体   English

如何在带有图标的已安装Android应用程序列表中添加“多项选择”?

[英]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. 我不能创建多个选择,因为它不完全是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) 活动:(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 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 : 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 : 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. 扩展“ listItem.xml”以在其末尾具有一个复选框,然后通过将侦听器添加到复选框选择来扩展“ AddAdapter”类。 You can propagate the event to the activity from the "AddAdapter" as well. 您也可以从“ AddAdapter”将事件传播到活动。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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