我正在创建一个Android应用程序。
在我的应用程序中,我有一个listView和一个gridView,当我设置这些视图的addapter时,我得到了The application may be doing too much work on its main thread
我试图将这些setadapter放在AsyncTask上,但没有任何改变......我不明白。
我的活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showToolbarTitle(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
toolbarTitle.setText("PARALLAX + FADING IMAGEVIEW");
toolbarTitle.setVisibility(View.INVISIBLE);
scrollview.setScrollViewCallbacks(this);
listView = (ListView) findViewById(R.id.listview);
gridView = (GridView) findViewById(R.id.gridView);
new LongOperation().execute(); //I call the AsyncTask
gridView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
return true;
}
return false;
}
});
}
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
CustomList adapter = new
CustomList(ParallaxLikeIoshedActivity.this, test, imageId, false);
// listView.setAdapter(adapter); // If I do this I will get the error
CustomList adapterGrid = new
CustomList(ParallaxLikeIoshedActivity.this, test, imageId, true);
// gridView.setAdapter(adapterGrid); // If I do this I will get the error
return "Executed";
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}
这是活动的布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<com.github.ksoichiro.android.observablescrollview.ObservableScrollView
android:id="@+id/scrollview"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/image_container"
android:layout_width="match_parent"
android:layout_height="250dp">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="#000000"/>
<!--<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/moviethumbnail"
tools:ignore="ContentDescription" />-->
</FrameLayout>
<LinearLayout
android:id="@+id/header"
android:layout_below="@id/image_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:background="#222"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:gravity="center_vertical|center_horizontal"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/linearLayout">
<TextView
android:textAppearance="?android:textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MHeader 1 !!!"
android:textSize="40sp"
android:textColor="@color/white"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/header"
android:background="#ff7f7f7f"
android:showDividers="middle">
<com.example.edouard.NonScrollListView
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:paddingTop="20dp"
android:divider="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listview"
android:visibility="visible"/>
<GridView
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:paddingTop="20dp"
android:divider="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/gridView"
android:visibility="invisible"/>
</FrameLayout>
</RelativeLayout>
</com.github.ksoichiro.android.observablescrollview.ObservableScrollView>
</FrameLayout>