Android cwac无休止的片段演示问题-在我自己的片段格式中使用
我有一个使用ADT向导生成的基本工作应用程序。因此,我使用了一种基本的片段格式(1) 我想以演示代码(2)为例,如果我以片段为例,应用程序崩溃,而logcat将片段显示为故障,则无论如何都要使用它 我试图从(1)和(2)中获取代码,以创建(3)多个错误的发生方式。看了这么多之后,我认为问题出在“DemoAdapter=null”上;因为无论出于什么原因,“类DemoAdapter扩展EndlesAdapter实现IItemsReadyListener”都无法调用 T (1) 我的基本片段-哪个有效Android cwac无休止的片段演示问题-在我自己的片段格式中使用,android,list,fragment,android-listfragment,cwac-endless,Android,List,Fragment,Android Listfragment,Cwac Endless,我有一个使用ADT向导生成的基本工作应用程序。因此,我使用了一种基本的片段格式(1) 我想以演示代码(2)为例,如果我以片段为例,应用程序崩溃,而logcat将片段显示为故障,则无论如何都要使用它 我试图从(1)和(2)中获取代码,以创建(3)多个错误的发生方式。看了这么多之后,我认为问题出在“DemoAdapter=null”上;因为无论出于什么原因,“类DemoAdapter扩展EndlesAdapter实现IItemsReadyListener”都无法调用 T (1) 我的基本片段-哪个有
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_dod_events, container, false);
// Do Stuff Here
return root;
}
@Override public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Save some state!
}
}
(2) 无休止的演示-如果我按原样使用,没有错误,但会使我的应用程序崩溃
package com.commonsware.cwac.endless.demo;
import android.app.ListFragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ArrayAdapter;
import java.util.ArrayList;
import com.commonsware.cwac.endless.EndlessAdapter;
/**
* This example makes use of EndlessAdapter's
* setRunInBackground feature.
*
* Calling setRunInBackground(false) allows you to launch
* your own AsyncTask with a listener callback, rather than
* using the built in cacheInBackground functionality.
*
* This is useful if you have existing AsyncTask(s) written
* to fetch data in a background thread, and don't want
* EndlessAdapter launching that in yet another background
* thread.
*/
public class EndlessAdapterCustomTaskFragment extends ListFragment {
DemoAdapter adapter=null;
ArrayList<Integer> items=null;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
if (adapter == null) {
items=new ArrayList<Integer>();
for (int i=0; i < 25; i++) {
items.add(i);
}
adapter=new DemoAdapter(items);
adapter.setRunInBackground(false); // Tell the adapter
// we will handle
// starting the
// background task
}
setListAdapter(adapter);
}
class DemoAdapter extends EndlessAdapter implements
IItemsReadyListener {
private RotateAnimation rotate=null;
DemoAdapter(ArrayList<Integer> list) {
super(new ArrayAdapter<Integer>(getActivity(), R.layout.row,
android.R.id.text1, list));
rotate=
new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(600);
rotate.setRepeatMode(Animation.RESTART);
rotate.setRepeatCount(Animation.INFINITE);
}
@Override
protected View getPendingView(ViewGroup parent) {
View row=
getActivity().getLayoutInflater().inflate(R.layout.row, null);
View child=row.findViewById(android.R.id.text1);
child.setVisibility(View.GONE);
child=row.findViewById(R.id.throbber);
child.setVisibility(View.VISIBLE);
child.startAnimation(rotate);
return(row);
}
@Override
protected boolean cacheInBackground() throws Exception {
new FetchDataTask(this, items.size()).execute();
return(items.size()<75);
}
@Override
public void onItemsReady(ArrayList<Integer> data) {
items.addAll(data);
adapter.onDataReady(); // Tell the EndlessAdapter to
// remove it's pending
// view and call
// notifyDataSetChanged()
}
@Override
protected void appendCachedData() {
}
}
interface IItemsReadyListener {
public void onItemsReady(ArrayList<Integer> data);
}
class FetchDataTask extends AsyncTask<Void, Void, ArrayList<Integer>> {
IItemsReadyListener listener;
/*
* The point from where to start counting. In a real
* life scenario this could be a pagination number
*/
int startPoint;
protected FetchDataTask(IItemsReadyListener listener, int startPoint) {
this.listener=listener;
this.startPoint=startPoint;
}
@Override
protected ArrayList<Integer> doInBackground(Void... params) {
ArrayList<Integer> result=new ArrayList<Integer>();
SystemClock.sleep(3000); // pretend to do work
for (int i=startPoint; i < startPoint + 25; i++) {
result.add(i);
}
return(result);
}
@Override
protected void onPostExecute(ArrayList<Integer> result) {
listener.onItemsReady(result);
}
}
}
package com.commonware.cwac.uncentral.demo;
导入android.app.ListFragment;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.os.SystemClock;
导入android.view.view;
导入android.view.ViewGroup;
导入android.view.animation.animation;
导入android.view.animation.RotateAnimation;
导入android.widget.ArrayAdapter;
导入java.util.ArrayList;
导入com.commonware.cwac.uncil.EndlessAdapter;
/**
*此示例使用EndlessAdapter的
*设置运行背景功能。
*
*调用setRunInBackground(false)可以启动
*您自己的异步任务具有侦听器回调,而不是
*使用内置的cacheInBackground功能。
*
*如果您已经编写了现有的异步任务,这将非常有用
*在后台线程中获取数据,但不希望
*EndlessAdapter在另一个背景下发布
*线。
*/
公共类EndlesAdapterCustomTaskFragment扩展了ListFragment{
DemoAdapter=null;
arraylistitems=null;
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
setRetainInstance(真);
if(适配器==null){
items=newarraylist();
对于(int i=0;i<25;i++){
项目.添加(i);
}
适配器=新的DemoAdapter(项目);
setRunInBackground(false);//告诉适配器
//我们会处理
//开始
//后台任务
}
setListAdapter(适配器);
}
类DemoAdapter扩展EndlesAdapter实现
Iitemsradylistener{
私有RotateAimation rotate=null;
DemoAdapter(ArrayList列表){
超级(新阵列适配器(getActivity(),R.layout.row,
android.R.id.text1,list));
轮换=
新的旋转动画(0f,360f,动画。相对于自身,
0.5f,动画。相对于自身,0.5f);
轮换。设定持续时间(600);
rotate.setRepeatMode(动画.重新启动);
rotate.setRepeatCount(Animation.INFINITE);
}
@凌驾
受保护的视图getPendingView(视图组父视图){
查看行=
getActivity().GetLayoutFlater()充气(R.layout.row,null);
View child=row.findviewbyd(android.R.id.text1);
setVisibility(View.GONE);
child=row.findViewById(R.id.throbber);
setVisibility(View.VISIBLE);
child.startAnimation(旋转);
返回(行);
}
@凌驾
受保护的布尔cacheInBackground()引发异常{
新建FetchDataTask(this,items.size()).execute();
返回(items.size()
import java.util.ArrayList;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ArrayAdapter;
import com.commonsware.cwac.endless.EndlessAdapter;
public class Fragment2 extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_dod_events, container, false);
DemoAdapter adapter=null;
ArrayList<Integer> items=null;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
if (adapter == null) {
items=new ArrayList<Integer>();
for (int i=0; i < 25; i++) {
items.add(i);
}
adapter=new DemoAdapter(items);
adapter.setRunInBackground(false); // Tell the adapter
// we will handle
// starting the
// background task
}
setListAdapter(adapter);
}
class DemoAdapter extends EndlessAdapter implements
IItemsReadyListener {
private RotateAnimation rotate=null;
DemoAdapter(ArrayList<Integer> list) {
super(new ArrayAdapter<Integer>(getActivity(), R.layout.row,
android.R.id.text1, list));
rotate=
new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(600);
rotate.setRepeatMode(Animation.RESTART);
rotate.setRepeatCount(Animation.INFINITE);
}
@Override
protected View getPendingView(ViewGroup parent) {
View row=
getActivity().getLayoutInflater().inflate(R.layout.row, null);
View child=row.findViewById(android.R.id.text1);
child.setVisibility(View.GONE);
child=row.findViewById(R.id.throbber);
child.setVisibility(View.VISIBLE);
child.startAnimation(rotate);
return(row);
}
@Override
protected boolean cacheInBackground() throws Exception {
new FetchDataTask(this, items.size()).execute();
return(items.size()<75);
}
@Override
public void onItemsReady(ArrayList<Integer> data) {
items.addAll(data);
adapter.onDataReady(); // Tell the EndlessAdapter to
// remove it's pending
// view and call
// notifyDataSetChanged()
}
@Override
protected void appendCachedData() {
}
}
interface IItemsReadyListener {
public void onItemsReady(ArrayList<Integer> data);
}
class FetchDataTask extends AsyncTask<Void, Void, ArrayList<Integer>> {
IItemsReadyListener listener;
/*
* The point from where to start counting. In a real
* life scenario this could be a pagination number
*/
int startPoint;
protected FetchDataTask(IItemsReadyListener listener, int startPoint) {
this.listener=listener;
this.startPoint=startPoint;
}
@Override
protected ArrayList<Integer> doInBackground(Void... params) {
ArrayList<Integer> result=new ArrayList<Integer>();
SystemClock.sleep(3000); // pretend to do work
for (int i=startPoint; i < startPoint + 25; i++) {
result.add(i);
}
return(result);
}
@Override
protected void onPostExecute(ArrayList<Integer> result) {
listener.onItemsReady(result);
}
}
// Do Stuff Here
return root;
}
@Override public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Save some state!
}