gpt4 book ai didi

java - 使用 RecyclerView 时的 getListView() 替换

转载 作者:行者123 更新时间:2023-11-30 01:58:00 27 4
gpt4 key购买 nike

我正在我的应用中实现 Material Design,并将 ListView 转换为 RecyclerView。我一直在关注使用方法 getListView() 的教程,但是当我使用 AppCompatActivity 扩展我的类时,这些教程不再可用。我可以使用替代方案吗?

最佳答案

getListView()ListActivityListFragment 的便捷方法。但是,不需要使用 ListActivityListFragment 来使用 ListView。如果您使用常规 Activity 或常规 Fragment,您将使用 findViewById() 从您的布局中检索您的 ListView >,就像您使用任何其他类型的小部件一样。

RecyclerView 也是如此。您将使用 findViewById() 从展开的布局中检索 RecyclerView

现在,欢迎您创建自己的 RecyclerViewActivityRecyclerViewFragment,如果您愿意,可以扩展它们。例如,您可以有一个像这样的 RecyclerViewActivity:

/***
Copyright (c) 2008-2015 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.

From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/

package com.commonsware.android.recyclerview.simplelist;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;

public class RecyclerViewActivity extends Activity {
private RecyclerView rv=null;

public void setAdapter(RecyclerView.Adapter adapter) {
getRecyclerView().setAdapter(adapter);
}

public RecyclerView.Adapter getAdapter() {
return(getRecyclerView().getAdapter());
}

public void setLayoutManager(RecyclerView.LayoutManager mgr) {
getRecyclerView().setLayoutManager(mgr);
}

public RecyclerView getRecyclerView() {
if (rv==null) {
rv=new RecyclerView(this);
rv.setHasFixedSize(true);
setContentView(rv);
}

return(rv);
}
}

在这里,你可以使用getRecyclerView()RecyclerViewActivity会为你创建RecyclerView实例,并将其设置为 Activity 。在我的例子中,RecyclerViewActivity 继承自 Activity,将其更改为继承自 AppCompatActivity 是向基类添加 9 个字符的问题(Activity --> AppCompatActivity).

关于java - 使用 RecyclerView 时的 getListView() 替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31920351/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com