PagerViewAdapter在扩展BaseActivity的活动中

问题描述 投票:0回答:1

我是一个正在创建的选项卡布局,其中包括项目中的三个片段(feedFragment,cameraFragment和profileFragment)。我已经完成了所有代码,但是,我正在努力将PagerViewAdapter包含在扩展BaseActivity的HomeActivity中。

我不确定如何在HomeActivity中正确实现PagerViewAdapter。请查看底部的完整代码。

以下是我已采取的步骤:

  1. 像这样简单地在setupView(savedInstanceState:Bundle?)下包括PagerViewAdapter:

// Initialize adapter
pagerAdapter = PagerViewAdapter(supportFragmentManager)
viewPager.adapter = pagerAdapter
viewPager.offscreenPageLimit = 3
  1. 但是,这提供了以下错误:“未解析的参考PagerViewAdapter”它建议我“创建一个抽象函数'PagerViewAdapter'”。一旦我单击“创建抽象函数'PagerViewAdapter'”。我将以下代码输入到@LayoutRes下的BaseActvity中。

abstract fun PagerViewAdapter(supportFragmentManager: FragmentManager): PagerAdapter
  1. 然后,在HomeActivity中出现一个错误,指出以下内容:“类'HomeActivity'不是抽象的,并且没有实现抽象基类成员公共抽象乐趣 PagerViewAdapter(supportFragmentManger:FragmentManager):在com.memory中定义的PagerAdapter .pod.ui.base.BaseActivity”。它建议“实施成员。

一旦单击“实现成员”,HomeActivity中将出现以下代码:

override fun PagerViewAdapter(supportFragmentManager: FragmentManager): com.memory.pod.adapter.PagerAdapter {
  TODO("Not yet implemented")
}
  1. 此刻不再出现错误,我启动了该应用程序,但是由于以下错误,该应用程序失败:

E / AndroidRuntime: FATAL EXCEPTION: main
Process: com.memory.pod.debug, PID: 16562
kotlin.NotImplementedError: An operation is not implemented: Not yet implemented
at com.memory.pod.camerax.ui.home.HomeActivity.PagerViewAdapter(HomeActivity.kt: 71)
at com.memory.pod.camerax.ui.home.HomeActivity.setupView(HomeActivity.kt: 43)
at com.memory.pod.camerax.ui.base.BaseActivity.onCreate(BaseActivity.kt: 14)
at android.app.Activity.performCreate(Activity.java: 7825)
at android.app.Activity.performCreate(Activity.java: 7814)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1306)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java: 83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java: 135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java: 95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 2016)
at android.os.Handler.dispatchMessage(Handler.java: 107)
at android.os.Looper.loop(Looper.java: 214)
at android.app.ActivityThread.main(ActivityThread.java: 7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java: 492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 930)

我现在不确定该怎么办,我不确定如何实现PagerViewAdapter。

这里是所有代码:

BaseActivity:

package com.memory.pod.camerax.ui.base

import android.os.Bundle
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentManager
import com.memory.pod.adapter.PagerAdapter

abstract class BaseActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        provideLayout()?.let { setContentView(it) }
        setupView(savedInstanceState)
    }

    @LayoutRes
    open fun provideLayout(): Int? = null
    abstract fun setupView(savedInstanceState: Bundle?)
    abstract fun PagerViewAdapter(supportFragmentManager: FragmentManager): PagerAdapter
}

HomeActivity:

package com.memory.pod.camerax.ui.home

import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.KeyEvent
import android.widget.ImageButton
import androidx.fragment.app.FragmentManager
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.memory.pod.camerax.util.commons.Constants
import com.memory.pod.R
import com.memory.pod.camerax.ui.base.BaseActivity

/**
 * Main entry point into our app. This app follows the single-activity pattern, and all
 * functionality is implemented in the form of fragments.
 */

class HomeActivity : BaseActivity() {

    private lateinit var viewPager: ViewPager
    private lateinit var homeBtn: ImageButton
    private lateinit var cameraBtn: ImageButton
    private lateinit var profileBtn: ImageButton
    private lateinit var pagerAdapter: PagerAdapter

    override fun provideLayout(): Int = R.layout.activity_home

    override fun setupView(savedInstanceState: Bundle?) {

        // Initialize views
        viewPager = findViewById(R.id.viewPager)

        // Initialize buttons
        homeBtn = findViewById(R.id.home_button)
        cameraBtn = findViewById(R.id.camera_button)
        profileBtn = findViewById(R.id.profile_button)

        // Initialize adapter
        pagerAdapter = PagerViewAdapter(supportFragmentManager)
        viewPager.adapter = pagerAdapter
        viewPager.offscreenPageLimit = 3

        // Add page change listener
        viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {

            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                // Change the images when tab is changed
                changingTabs(position)
            }

        })

        // Set default fragment that will be displayed on application cold start
        viewPager.currentItem = 0
        homeBtn.setImageResource(R.drawable.ic_bottom_navigation_feed)
    }

    override fun PagerViewAdapter(supportFragmentManager: FragmentManager): com.memory.pod.adapter.PagerAdapter {
        TODO("Not yet implemented")
    }

    private fun changingTabs(position: Int) {

        if(position == 0){

            homeBtn.setImageResource(R.drawable.ic_bottom_navigation_feed)
            cameraBtn.setImageResource(R.drawable.ic_back)
            profileBtn.setImageResource(R.drawable.ic_back)

        }

        if(position == 1){

            homeBtn.setImageResource(R.drawable.ic_back)
            cameraBtn.setImageResource(R.drawable.ic_camera_action)
            profileBtn.setImageResource(R.drawable.ic_back)

        }

        if(position == 2){

            homeBtn.setImageResource(R.drawable.ic_back)
            cameraBtn.setImageResource(R.drawable.ic_back)
            profileBtn.setImageResource(R.drawable.ic_bottom_navigation_profile)

        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return when (keyCode) {
            KeyEvent.KEYCODE_VOLUME_DOWN -> {
                val intent = Intent(Constants.EXTRAS.KEY_EVENT_ACTION).apply {
                    putExtra(Constants.EXTRAS.KEY_EVENT_EXTRA, keyCode)
                }
                LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
                true
            }
            else -> super.onKeyDown(keyCode, event)
        }
    }
}
android android-fragments kotlin android-pageradapter android-adapterview
1个回答
0
投票

1 BaseActivity

public class BaseActivity extends AppCompatActivity {
    public String s = "okay";

    public String TestLog(){
        Log.e("test", s);

        return s;
    }
}

2。 MainActivity

public class MainActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView text = (TextView)findViewById(R.id.text);

        text.setText(s);
        TestLog();

    }
}

这是我的例子。我认为您可以从中得到提示。

© www.soinside.com 2019 - 2024. All rights reserved.