我正在我的应用程序中实现Material Design并将ListViews转换为RecyclerViews。我一直在关注使用方法getListView()的教程,但是当我使用AppCompatActivity扩展我的类时,这些教程不再可用。有没有我可以使用的替代方案?
答案 0 :(得分:1)
getListView()
是ListActivity
和ListFragment
上的便捷方法。但是,无需使用ListActivity
或ListFragment
来使用ListView
。如果您使用常规Activity
或常规Fragment
,则可以使用ListView
从布局中检索findViewById()
,就像使用任何其他类型的小部件一样。
RecyclerView
也是如此。您将使用RecyclerView
从充气版面中检索findViewById()
。
现在,如果您愿意,欢迎您创建自己的RecyclerViewActivity
和RecyclerViewFragment
。例如,您可以像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
个实例,并将其设置为活动的内容视图。在我的情况下,RecyclerViewActivity
继承自Activity
,将其更改为从AppCompatActivity
继承是向基类添加9个字符的问题(Activity
- > { {1}})。