Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 是否可以从Fragment中获取“RecyclerView”?_Android_Android Recyclerview_Kotlin_Android Framelayout - Fatal编程技术网

Android 是否可以从Fragment中获取“RecyclerView”?

Android 是否可以从Fragment中获取“RecyclerView”?,android,android-recyclerview,kotlin,android-framelayout,Android,Android Recyclerview,Kotlin,Android Framelayout,正在尝试从此布局获取RecyclerView: <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="

正在尝试从此布局获取
RecyclerView

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".listFragment">

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recyclee">
</android.support.v7.widget.RecyclerView>
在onCreate方法中:

        mBlogList = recyclee
但同样的错误仍然存在

编辑2 listFragment代码:

class listFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.fragment_list, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    recyclee != null
}
companion object {
    fun newInstance(): listFragment = listFragment()
}
}
编辑3 整个活动代码:

//this app supposed to read from FirebaseDatabase
//into Recycler view
//the RecyclerView is into Fragment layout
//i use Fragments into FrameLayout in the activity_main.xml
// the RecyclerView should be shown when navigatoinBar is clicked
//or on start of MainActivity
class MainActivity : AppCompatActivity() {
    private var mDatabase:DatabaseReference? = null
    private lateinit var mBlogList : RecyclerView
    private var query:Query?=null
    private var options:FirebaseRecyclerOptions<Blog>?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //start listFragment , RecyclerView is there
        val mFragment = listFragment.newInstance()
        //openFragment method is below
        openFragment(mFragment)
        //navigation bottom onclicklistener


navBar.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
        //get data from database
        mDatabase=FirebaseDatabase.getInstance().getReference().child("mall")
        mDatabase?.keepSynced(true)
        //here i should have recyclee but it is null i don't know why
        mBlogList = recyclee
        mBlogList.setHasFixedSize(true)
        mBlogList.layoutManager = LinearLayoutManager(this)
        //query of database
        query = mDatabase?.orderByKey()

    }
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
   //there are 4 icons in the navigation_bottom_bar 
    //now we are talking about listNav icon only because it is realted 
    // with listFragment
    when (item.itemId) {
        R.id.listNav -> {
            val mFragment = listFragment.newInstance()
            openFragment(mFragment)
            return@OnNavigationItemSelectedListener true
        }
        R.id.cartNav -> {
            val mFragment = cartFragment.newInstance()
            openFragment(mFragment)
            return@OnNavigationItemSelectedListener true
        }
        R.id.supportNav -> {
            val mFragment = supportFragment.newInstance()
            openFragment(mFragment)
            return@OnNavigationItemSelectedListener true
        }
        R.id.accountNav -> {
            val mFragment = accountFragment.newInstance()
            openFragment(mFragment)
            return@OnNavigationItemSelectedListener true
        }
    }
    false
}
private fun openFragment(fragment: Fragment) {
    //open Fragment into FrameLayout in the main_activity.xml
    val transaction = supportFragmentManager.beginTransaction()
    transaction.replace(R.id.mainFrame, fragment)
    transaction.addToBackStack(null)
    transaction.commit()
}

override fun onStart() {
    super.onStart()
    //set options for FirebaseRecyclerAdapter
    options = FirebaseRecyclerOptions.Builder<Blog>()
            .setQuery(query!!, Blog::class.java)
            .build()
    //set custom adapter
    val mAdapter = object : FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
            options!!) {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BlogViewHolder {
            val view = LayoutInflater.from(parent.context)
                    .inflate(R.layout.cardview, parent, false)
            return BlogViewHolder(view)}

        override fun onBindViewHolder(holder: BlogViewHolder, position: Int, model: Blog) {
            holder.setTitle(model.title)
            holder.setDes(model.des)
            holder.setImage(applicationContext, model.image)
        }
    }
    mBlogList.adapter = mAdapter
}
inner class BlogViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    var mView:View= itemView
    //set title, des amd image with data we got from database
    fun setTitle(title:String){
        var postTitle = mView.findViewById<TextView>(R.id.post_title)
        postTitle?.text = title
    }
    fun setDes(des:String){
        var postDes = mView.findViewById<TextView>(R.id.post_des)
        postDes?.text = des
    }
    fun setImage(image:String){
        var postImage = mView.findViewById<ImageView>(R.id.post_title)
        Picasso.get().load(image).into(postImage)
    }
}
//此应用程序应该从FirebaseDatabase读取
//进入回收者视图
//RecyclerView进入片段布局
//我在activity_main.xml中的FrameLayout中使用片段
//单击导航栏时应显示RecyclerView
//或在活动开始时
类MainActivity:AppCompatActivity(){
私有变量mDatabase:数据库引用?=null
私有lateinit var mBlogList:RecyclerView
私有变量查询:查询?=null
私有变量选项:FirebaseRecyclerOptions?=null
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//启动listFragment,回收视图在那里
val MFFragment=listFragment.newInstance()
//openFragment方法如下所示
openFragment(MFFragment)
//导航底部侦听器
navBar.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
//从数据库获取数据
mDatabase=FirebaseDatabase.getInstance().getReference().child(“mall”)
mDatabase?.keepSynced(true)
//在这里,我应该有回收,但它是空的,我不知道为什么
mBlogList=回收者
mBlogList.setHasFixedSize(true)
mBlogList.layoutManager=LinearLayoutManager(此)
//数据库查询
query=mDatabase?.orderByKey()
}
private val mOnNavigationItemSelectedListener=BottomNavigationView.OnNavigationItemSelectedListener{item->
//导航栏底部有4个图标
//现在我们谈论listNav图标只是因为它是真实的
//用listFragment
时间(item.itemId){
R.id.listNav->{
val MFFragment=listFragment.newInstance()
openFragment(MFFragment)
return@OnNavigationItemSelectedListener真的
}
R.id.cartNav->{
val MFFragment=cartFragment.newInstance()
openFragment(MFFragment)
return@OnNavigationItemSelectedListener真的
}
R.id.supportNav->{
val mffragment=supportFragment.newInstance()
openFragment(MFFragment)
return@OnNavigationItemSelectedListener真的
}
R.id.accountNav->{
val mffragment=accountFragment.newInstance()
openFragment(MFFragment)
return@OnNavigationItemSelectedListener真的
}
}
假的
}
私有趣味openFragment(片段:片段){
//在main_activity.xml中将片段打开到FrameLayout中
val事务=supportFragmentManager.beginTransaction()
事务.替换(R.id.mainFrame,片段)
transaction.addToBackStack(空)
commit()事务
}
覆盖有趣的onStart(){
super.onStart()
//设置FirebaseRecyclerAdapter的选项
选项=FirebaseRecyclerOptions.Builder()
.setQuery(query!!,Blog::class.java)
.build()
//设置自定义适配器
val mAdapter=对象:FirebaseRecyclerAdapter(
选项!!){
重写FunonCreateViewHolder(父级:ViewGroup,viewType:Int):BlogViewHolder{
val view=LayoutInflater.from(parent.context)
.充气(R.layout.cardview,父级,假)
返回BlogViewHolder(视图)}
覆盖onBindViewHolder(holder:BlogViewHolder,位置:Int,型号:Blog){
持有者设置标题(型号标题)
支架设置des(型号des)
holder.setImage(applicationContext,model.image)
}
}
mBlogList.adapter=mAdapter
}
内部类BlogViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
var mView:View=itemView
//设置标题,des amd图像和我们从数据库获得的数据
乐趣集标题(标题:字符串){
var postTitle=mView.findViewById(R.id.post\u title)
postTitle?.text=标题
}
乐趣集(des:String){
var postDes=mView.findviewbyd(R.id.post\u des)
postDes?.text=des
}
乐趣集图像(图像:字符串){
var postImage=mView.findviewbyd(R.id.post\u标题)
毕加索.get().load(图像).into(后期图像)
}
}

}

尝试在onCreate或onCreateView(如果在片段中)上使用
findViewById
。这是因为在使用这种方法之前,您的布局还没有膨胀,所以Android无法找到您的RecyclerView

class MainActivity : AppCompatActivity() {

     private lateinit var mBlogList : RecyclerView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mBlogList = findViewById<RecyclerView>(R.id.recyclee)
    }
}
class MainActivity:AppCompatActivity(){
私有lateinit var mBlogList:RecyclerView
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mBlogList=findviewbyd(R.id.recycleee)
}
}
我建议您阅读以下内容:
Kotlin有一种更好的方法来获取视图的参考,您不需要使用
findViewById
=)

您不能从全局范围调用
findViewById
。但是,您可以将其声明为:

private val mBlogList by lazy{findViewById(R.id.recycleee)}

…然后像往常一样使用变量。

如果应用
kotlin android extensions
插件,则不需要再使用
findViewById
,因为您可以访问布局中的视图,就好像它们是属性一样(按名称)。因此,您的
onCreate
可以如下所示:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    assert recyclee != null
}
编辑

因为您正试图在
片段中执行此操作,并且您的布局文件被调用
fragment\u List.xml
然后在
fragment
中,必须首先对布局进行充气,然后才能访问
RecyclerView
,如上述
活动的示例所示:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.fragment_List, container, false)
} 

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    assert recyclee != null
}
如果您试图访问
中的
回收视图
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    assert recyclee != null
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.fragment_List, container, false)
} 

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    assert recyclee != null
}