У меня есть приложение с несколькими раскладками, каждый раз, когда вы нажимаете, отображается новый список с дополнительными опциями для выбора.основное действие имеет RecyclerView, который работает очень плавно, но при нажатии на один из вариантов для перехода к следующему занятию требуется около 2 или 3 секунд (это происходит только при первом запуске программы, затемкаждый клик работает без задержек ). Я использую ItemObject, RecyclerViewAdapter и RecyclerViewHolder для каждого списка .
Наиболее краткий вопрос будет таким: могу ли я использовать один адаптер, ItemObject для всех списков иобрабатывать клики отдельно с RecyclerViewHolder?Как избежать задержек при смене видов деятельности?Я оптимизирую использование памяти? .
Примечание: каждый RecyclerView показывает маленькое изображение рядом с ним.каждый список управляет более чем 5 объектами.
Буду признателен, если вы мне поможете.Я надеюсь, что моя проблема понята.Спасибо.
MainActivity
Другие действия подобны этому
Вот мой файл Gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.nexttip.gridview"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
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'
compile 'com.android.support:appcompat-v7:27.1.1'
compile 'com.android.support:design:27.1.1'
compile 'me.itangqi.waveloadingview:library:0.3.5'
compile 'com.android.support:support-compat:27.1.1'
compile 'com.android.support:recyclerview-v7:27.1.1'
compile 'com.google.firebase:firebase-core:11.8.0'
compile 'com.google.firebase:firebase-messaging:11.8.0'
compile 'com.android.support:cardview-v7:27.1.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.google.android.gms:play-services-ads:11.8.0'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:multidex:1.0.0'
}
Мой манифест
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nexttip.gridview">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service android:name=".FirebaseNotificationsService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".mFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".SplashScreenActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".WorkoutActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".DonateMainLayout" />
<activity android:name=".VideoListActivity" />
<activity
android:name=".VideoActivity"
android:screenOrientation="landscape"
android:theme="@style/videoPlayer" />
<activity android:name=".RateActivity" />
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
<activity android:name=".BMI" />
<activity android:name=".Result_BMI" />
<activity
android:name=".Video_Stream"
android:screenOrientation="landscape"
android:theme="@style/videoStream" />
<activity
android:name=".Timer_CountDown"
android:theme="@style/AppTheme.Transparent" />
<activity
android:name=".ActivityList1"
android:label="@string/list1_label" />
<activity
android:name=".ActivityList2"
android:label="@string/list2_label" />
<activity
android:name=".ActivityList3"
android:label="@string/list3_label" />
<activity
android:name=".ActivityList4"
android:label="@string/list4_label" />
<activity
android:name=".ActivityList5"
android:label="@string/list5_label">
</activity>
<activity
android:name=".IntroActivity"
android:label="@string/intro_title">
</activity>
<activity
android:name=".TipsActivity"
android:label="@string/tips_title">
</activity>
<activity android:name=".DietPlanMain"
android:label="@string/diet_title">
</activity>
</application>
</manifest>
Моя основная деятельность
package com.nexttip.gridview;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.gms.ads.InterstitialAd;
import java.util.ArrayList;
import java.util.List;
public class WorkoutActivity extends AppCompatActivity{
private LinearLayoutManager lLayout;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
List<ItemObject> rowListItem = getAllItemList();
lLayout = new LinearLayoutManager(WorkoutActivity.this);
RecyclerView rView = (RecyclerView)findViewById(R.id.recycler_view);
rView.setLayoutManager(lLayout);
RecyclerViewAdapter rcAdapter = new RecyclerViewAdapter(WorkoutActivity.this, rowListItem);
rView.setAdapter(rcAdapter);
}
private List<ItemObject> getAllItemList(){
List<ItemObject> allItems = new ArrayList<ItemObject>();
allItems.add(new ItemObject("Legs Workout", R.drawable.legs));
allItems.add(new ItemObject("ABS Workout", R.drawable.abs));
allItems.add(new ItemObject("Arms Workout", R.drawable.arms));
allItems.add(new ItemObject("Butt Workout", R.drawable.butt));
allItems.add(new ItemObject("Total Body Workout", R.drawable.total));
return allItems;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
Другие действия такие:
package com.nexttip.gridview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
import com.nexttip.gridview.List1.ItemObject1;
import com.nexttip.gridview.List1.RecyclerViewAdapter1;
import java.util.ArrayList;
import java.util.List;
public class ActivityList1 extends AppCompatActivity {
private LinearLayoutManager lLayout;
private AdView mAdView;
private InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list1);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
List<ItemObject1> rowListItem1 = getAllItemList();
lLayout = new LinearLayoutManager(ActivityList1.this);
RecyclerView rView = (RecyclerView)findViewById(R.id.recycler_view1);
rView.setLayoutManager(lLayout);
RecyclerViewAdapter1 rcAdapter = new RecyclerViewAdapter1(ActivityList1.this, rowListItem1);
rView.setAdapter(rcAdapter);
prepareAd();
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
// Load the next interstitial.
mInterstitialAd.loadAd(new AdRequest.Builder().build());
finish();
}
});
}
private List<ItemObject1> getAllItemList(){
List<ItemObject1> allItems = new ArrayList<ItemObject1>();
allItems.add(new ItemObject1("Workout Video", R.drawable.videos));
allItems.add(new ItemObject1("Basic Squat", R.drawable.legs1));
allItems.add(new ItemObject1("Bulgarian Split Squat", R.drawable.legs2));
allItems.add(new ItemObject1("Donkey Kicks", R.drawable.legs3));
allItems.add(new ItemObject1("Front Back Lunge", R.drawable.legs4));
allItems.add(new ItemObject1("Heel Beats", R.drawable.legs5));
allItems.add(new ItemObject1("High Kick Cross Over", R.drawable.legs6));
allItems.add(new ItemObject1("Jumping Jacks", R.drawable.legs7));
allItems.add(new ItemObject1("Leg Lift", R.drawable.legs8));
allItems.add(new ItemObject1("Lunge Pulse", R.drawable.legs9));
allItems.add(new ItemObject1("Single Leg Wall Squat", R.drawable.legs10));
allItems.add(new ItemObject1("Skater", R.drawable.legs11));
allItems.add(new ItemObject1("Sumo Squats to Calf Raise", R.drawable.legs12));
return allItems;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case android.R.id.home:
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
else {
finish();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onBackPressed() {
{
//super.onBackPressed();
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
else {
finish();
}
}
}
public void prepareAd(){
MobileAds.initialize(this, getString(R.string.admob_app_id));
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.intersticial_ad_id));
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}