我是一个正在创建的选项卡布局,其中包括项目中的三个片段(feedFragment,cameraFragment和profileFragment)。我已经完成了所有代码,但是,我正在努力将PagerViewAdapter包含在扩展BaseActivity的HomeActivity中。
我不确定如何在HomeActivity中正确实现PagerViewAdapter。请查看底部的完整代码。
以下是我已采取的步骤:
// Initialize adapter
pagerAdapter = PagerViewAdapter(supportFragmentManager)
viewPager.adapter = pagerAdapter
viewPager.offscreenPageLimit = 3
abstract fun PagerViewAdapter(supportFragmentManager: FragmentManager): PagerAdapter
一旦单击“实现成员”,HomeActivity中将出现以下代码:
override fun PagerViewAdapter(supportFragmentManager: FragmentManager): com.memory.pod.adapter.PagerAdapter {
TODO("Not yet implemented")
}
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)
}
}
}
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();
}
}
这是我的例子。我认为您可以从中得到提示。