简体   繁体   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?

I have an application with several Layouts, every time you click, a new list is displayed with more options to select. 我有一个带有多个布局的应用程序,每次单击时,都会显示一个新列表,其中包含更多选项可供选择。 the main activity has a RecyclerView that works very fluid but when you click on one of the options it takes about 2 or 3 seconds to jump to the next activity (only does it the first time the program starts, then every click works without delays ). 主要活动具有一个RecyclerView,该活动非常流畅,但是当您单击其中一个选项时,大约需要2或3秒钟才能跳至下一个活动 (仅在程序首次启动时才执行,然后每次单击都不会延迟 ) 。 I am using an ItemObject, RecyclerViewAdapter and a RecyclerViewHolder for each list . 我为每个列表使用一个ItemObject,RecyclerViewAdapter和RecyclerViewHolder

The most summarized question would be: can I use a single adapter, an ItemObject for all the lists and handle the Clicks separately with a RecyclerViewHolder? 最概括的问题是: 我可以使用单个适配器,所有列表的ItemObject并使用RecyclerViewHolder分别处理Clicks吗? How do I avoid delays when changing activities? 如何更改活动时避免延误? I optimize the use of memory ? 我优化了内存的使用? .

note: each RecyclerView shows a small image next to it. 注意:每个RecyclerView旁边都会显示一个小图像。 each list manages more than 5 objects. 每个列表管理5个以上的对象。

I would appreciate if you help me. 如果您能帮助我,我将不胜感激。 I hope my problem is understood. 希望我的问题得到理解。 Thank you. 谢谢。

MainActivity 主要活动

The other activities are like this 其他活动是这样的

Here is my Gradle File: 这是我的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'
}

My Manifest 我的清单

    <?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>

My Main Activity 我的主要活动

    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();
    }
}

The other activities are like this: 其他活动如下:

    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();
    }

}

My friend, none of your code blocks above are helping, you left out the most important code we want to see, your Adapter s + ItemObject s . 我的朋友,上面的代码块都ItemObject ,您省略了我们想看到的最重要的代码,即Adapter + ItemObject

I have insufficient reputation points to comment, but I will edit this answer and help you once you remove all that code above and post the relevant code. 我的信誉点不足以发表评论,但是一旦您删除了上面所有代码并发布了相关代码,我将编辑此答案并为您提供帮助。

A few notes I can give now: 我现在可以提供一些注意事项:

  • Yes, every RecyclerView needs its own Adapter , otherwise they'll all be showing the same data. 是的,每个RecyclerView需要自己的Adapter ,否则它们都将显示相同的数据。
  • You can improve your app so much by replacing all these Activity s with Fragment s 通过将所有这些Activity替换为Fragment可以大大改善您的应用程序
  • Store your data lists (and update them) inside your Adapter s, not in the Activity s (or Fragment s), make these two concerned only about viewing and managing the UI 将数据列表存储(并更新)在Adapter ,而不是在Activity (或Fragment )中,使这两个列表仅与查看和管理 UI有关。

暂无
暂无

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

相关问题 如何将ListView适配器转换为RecyclerView适配器? - How do I convert my ListView adapter into RecyclerView adapter? 我是否需要在 recyclerView 或 recyclerView 中使用列表视图? - Do I need to use list view inside recyclerView or recyclerView inside recyclerView? 如何从sqlite数据库附加RecyclerView适配器 - How do I attach RecyclerView adapter from sqlite database 如何在 recyclerview 适配器中为 Activity 扩展上下文 - How do I extablish context for an Activity within a recyclerview adapter 我如何在 Android Studio 上的 RecyclerView 上 setOnClickListener - How do i setOnClickListener on RecyclerView on Android Studio 如何修复RecyclerView:未连接适配器; 跳过布局? - How do i fix RecyclerView: No adapter attached; skipping layout? 如何使用 AsyncTask、RecyclerView 和 RecyclerView.Adapter 将手机中的图像获取到 RecyclerView? - How can I use AsyncTask, RecyclerView, and a RecyclerView.Adapter to get images from my phone into my RecyclerView? 在recyclerview中,如何让每个事件开启新的活动? - In recyclerview, how do I get each event to open new activities? Android:使用RecyclerView和CardView进行商品复制-如何停止复制? - Android: Item Duplication with RecyclerView & CardView - How do I stop the duplication? Android 如何在 ThreadPoolExecutor 完成后调用 RecyclerVIew notifyDataSetChanged()? - Android How Do I Call RecyclerVIew notifyDataSetChanged() after ThreadPoolExecutor finishes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM