【发布时间】:2015-11-11 13:04:15
【问题描述】:
我正在尝试将拉动刷新功能添加到列表视图中。
在fragment_page.xml(这里定义了全局listview)
<LinearLayout
style="@style/AppTheme.BaseActivity"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="xx.view.fragment.PageFragment">
<TextView
android:id="@android:id/empty"
style="@style/AppTheme.WrapContent.NoResult"
android:text="@string/no_results"/>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="@drawable/list_selector"/>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
在活动中:
public class MessageActivity extends TabBaseActivity implements ManageView, OnRefreshListener {
private MessageController mMessageController;
private int mSelectedTab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
setUp(new String[]{"Unread", "Read", "Sent", "All"}, Constants.Adapter.MESSAGE_ADAPTER);
mMessageController.loadMessages();
//START Swipe to refresh
try {
final SwipeRefreshLayout swipeContainer = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);
if(swipeContainer == null) {
Logger.d("Null");
}
swipeContainer.setEnabled(false);
swipeContainer.setOnRefreshListener(this);
} catch (Exception e) {
Logger.e(e.getMessage());
}
//END Swipe to refresh
}
但 swipeContainer 仍然为空。
我尝试使用以下教程进行操作:
http://www.androidhive.info/2015/05/android-swipe-down-to-refresh-listview-tutorial/
和
http://www.survivingwithandroid.com/2014/05/android-swiperefreshlayout-tutorial.html
但没有运气。
请问如何解决?
非常感谢您的任何建议。
编辑:
视图的内容在 init 中设置:
private void init() {
mMessageController = MessageController.getInstance(this, getSupportFragmentManager());
mMessageController.setView(this);
mSelectedTab = 0;
}
【问题讨论】:
-
您是否设置了内容视图?除非您在
init()内执行此操作,否则它会丢失。 -
是的,请检查我更新的问题。
-
在尝试查找视图元素之前,您需要调用
setContentView() -
不能在 TabBaseActivity 中使用 setContentView()
-
如果你不能使用
setContentView,你也不能使用findViewById
标签: android listview swiperefreshlayout