implemented adapter class for app drawer screen

This commit is contained in:
enyason
2018-07-29 22:06:16 +01:00
parent a3a60c906f
commit 063c014d68
7 changed files with 188 additions and 15 deletions

2
.idea/misc.xml generated
View File

@ -24,7 +24,7 @@
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

View File

@ -25,6 +25,7 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation "com.android.support:recyclerview-v7:27.1.1"
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support:cardview-v7:27.1.1'
}

View File

@ -0,0 +1,14 @@
package com.nexdev.enyason.androidlauncherapplication;
import android.graphics.drawable.Drawable;
/**
* Created by enyason on 7/29/18.
*/
class AppInfo {
CharSequence label;
CharSequence packageName;
Drawable icon;
}

View File

@ -0,0 +1,120 @@
package com.nexdev.enyason.androidlauncherapplication;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.speech.tts.TextToSpeech;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
/**
* Created by Enyason on 3/5/2018.
*/
public class AppsDrawerAdapter extends RecyclerView.Adapter<AppsDrawerAdapter.ViewHolder> {
private static Context context;
private static List<AppInfo> appsList;
public AppsDrawerAdapter(Context c ) {
//This is where we build our list of app details, using the app
//object we created to store the label, package name and icon
context = c;
setUpApps();
}
public static void setUpApps(){
PackageManager pManager = context.getPackageManager();
appsList = new ArrayList<AppInfo>();
Intent i = new Intent(Intent.ACTION_MAIN, null);
i.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> allApps = pManager.queryIntentActivities(i, 0);
for (ResolveInfo ri : allApps) {
AppInfo app = new AppInfo();
app.label = ri.loadLabel(pManager);
app.packageName = ri.activityInfo.packageName;
Log.i(" Log package ",app.packageName.toString());
app.icon = ri.activityInfo.loadIcon(pManager);
appsList.add(app);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//This is what adds the code we've written in here to our target view
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row_view_list, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String appLabel = appsList.get(position).label.toString();
String appPackage = appsList.get(position).packageName.toString();
Drawable appIcon = appsList.get(position).icon;
TextView textView = holder.textView;
textView.setText(appLabel);
ImageView imageView = holder.img;
imageView.setImageDrawable(appIcon);
}
@Override
public int getItemCount() {
return appsList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextToSpeech textToSpeech;
public TextView textView;
public ImageView img;
public ViewHolder(View itemView) {
super(itemView);
//Finds the views from our row.xml
textView = (TextView) itemView.findViewById(R.id.tv_app_name);
img = (ImageView) itemView.findViewById(R.id.app_icon);
}
}
}

View File

@ -33,4 +33,11 @@ public class MainActivity extends AppCompatActivity {
return false;
}
@Override
public void onBackPressed() {
super.onBackPressed();
// Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fr)
}
}

View File

@ -14,22 +14,13 @@
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/appDrawer_recylerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textColor="#000"
android:textStyle="bold"
android:layout_centerInParent="true"
android:text="Apps Drawer"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v7.widget.RecyclerView>
</android.support.v7.widget.CardView>

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:clickable="false"
android:layout_height="wrap_content">
<LinearLayout
android:clickable="false"
android:layout_marginTop="16dp"
android:layout_width="match_parent"
android:layout_marginLeft="25dp"
android:orientation="horizontal"
android:layout_height="wrap_content">
<ImageView
android:src="@mipmap/ic_launcher"
android:id="@+id/app_icon"
android:clickable="false"
android:layout_width="48dp"
android:scaleType="centerCrop"
android:layout_height="48dp" />
<TextView
android:layout_marginLeft="16dp"
android:layout_width="wrap_content"
android:text="Chrome"
android:clickable="false"
android:lines="1"
android:textSize="12sp"
android:layout_gravity="center_vertical"
android:ellipsize="end"
android:fontFamily="sans-serif-medium"
android:layout_height="wrap_content"
android:id="@+id/tv_app_name"/>
</LinearLayout>
</RelativeLayout>