繁体   English   中英

Android-RecyclerView:是否需要为每个recyclerView使用适配器和ItemObject? 如何分别处理点击?

[英]Android - RecyclerView: Do I need to use an adapter and an ItemObject for each recyclerView ?. How do I handle the clicks separately?

我有一个带有多个布局的应用程序,每次单击时,都会显示一个新列表,其中包含更多选项可供选择。 主要活动具有一个RecyclerView,该活动非常流畅,但是当您单击其中一个选项时,大约需要2或3秒钟才能跳至下一个活动 (仅在程序首次启动时才执行,然后每次单击都不会延迟 ) 。 我为每个列表使用一个ItemObject,RecyclerViewAdapter和RecyclerViewHolder

最概括的问题是: 我可以使用单个适配器,所有列表的ItemObject并使用RecyclerViewHolder分别处理Clicks吗? 如何更改活动时避免延误? 我优化了内存的使用?

注意:每个RecyclerView旁边都会显示一个小图像。 每个列表管理5个以上的对象。

如果您能帮助我,我将不胜感激。 希望我的问题得到理解。 谢谢。

主要活动

其他活动是这样的

这是我的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();
    }

}

我的朋友,上面的代码块都ItemObject ,您省略了我们想看到的最重要的代码,即Adapter + ItemObject

我的信誉点不足以发表评论,但是一旦您删除了上面所有代码并发布了相关代码,我将编辑此答案并为您提供帮助。

我现在可以提供一些注意事项:

  • 是的,每个RecyclerView需要自己的Adapter ,否则它们都将显示相同的数据。
  • 通过将所有这些Activity替换为Fragment可以大大改善您的应用程序
  • 将数据列表存储(并更新)在Adapter ,而不是在Activity (或Fragment )中,使这两个列表仅与查看和管理 UI有关。

暂无
暂无

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

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