Когда я создаю свое приложение, загрузка изображений очень запаздывает.Кажется, это происходит в сочетании с добавлением BottomNavigationView или панели инструментов, когда они удаляются, приложение работает без сбоев.Установка - это MainActivity и фрагменты загружаются в нее через нижнюю панель навигации.
public class FragmentActivity extends AppCompatActivity {
Fragment currentFragment;
Fragment selectedFragment;
public static final String TAG = FragmentActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_activity);
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
SearchFragment searchFragment = new SearchFragment();
currentFragment = searchFragment;
// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, currentFragment).commit();
setBottomNavBar();
}
}
public void setBottomNavBar() {
Log.d(TAG, "setting up bar");
LinearLayout bottomBar = findViewById(R.id.toolbar);
ImageButton search = bottomBar.findViewById(R.id.search);
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedFragment = new SearchFragment();
getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.fragment_container, selectedFragment).commit();
}
});
ImageButton user = bottomBar.findViewById(R.id.user);
user.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedFragment = new MyUserFragment();
getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.fragment_container, selectedFragment).commit();
}
});
ImageButton trending = bottomBar.findViewById(R.id.trending);
trending.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), TrendingMapTest.class); //creating new intent, passing in the UserFeedActivity calling it
startActivity(intent);
}
});
ImageButton upload = bottomBar.findViewById(R.id.upload);
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), CameraAndGalleryActivity.class); //creating new intent, passing in the UserFeedActivity calling it
startActivity(intent);
}
});
ImageButton rewards = bottomBar.findViewById(R.id.rewards);
rewards.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedFragment = new RewardsFragment();
getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.fragment_container, selectedFragment).commit();
}
});
}}
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@color/white"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:gravity="bottom">
<ImageButton
android:id="@+id/search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="false"
android:background="@android:color/white"
android:clickable="true"
android:padding="12dp"
android:scaleType="fitCenter"
android:src="@drawable/search_icon" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
private void generatePostsGrid(String jsonData) {
try {
JSONObject posts = new JSONObject(jsonData);
final JSONArray posts_arr = posts.getJSONArray("posts");
amountLoaded = amountLoaded + posts_arr.length();
Log.d(TAG, "generating POSTGRIDD");
Log.v(TAG, "tag loading in posts " + Integer.toString(posts_arr.length()));
if (posts_arr != null) {
for (int i = 0; i < posts_arr.length(); i++) {
try {
String path =
posts_arr.getJSONObject(i).getString("image_path");
if (path.contains("http")) { //tells us if its an
external image url WILL NOT BE NEEDED LATER
image_list.add(path);
all_images_list.add(path);
} else {
//adding image paths strings to image list code
}
image_list_id.add(posts_arr.getJSONObject(i).getString("_id"));
} catch (JSONException e) {
e.printStackTrace();
}
}
Log.d(TAG, image_list.toString());
}
GridView gridview = getView().findViewById(R.id.postsGridTest);
Log.v(TAG, "running");
//declare adapter
if (adapter != null) {
adapter.setImages(image_list);
//adapter.notifyDataSetChanged();
waitingPosts = false; //reset this var now that we have loaded
in more posts
image_list = new ArrayList<String>();
return;
}
adapter = new PostAdapter(getContext(), image_list);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
gridview.setAdapter(adapter);
}
});
waitingPosts = false; //reset this var now that we have loaded in
more posts
image_list = new ArrayList<String>();
if (!instantiatedScroll) {
Log.d(TAG, "instatiating scroll setting listener");
gridview.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
Log.d(TAG, "scroll listener called");
if (firstVisibleItem + visibleItemCount >=
totalItemCount) {
int pos = firstVisibleItem + visibleItemCount;
// End has been reached load more posts in
Log.v(TAG, "Wanting to");
Log.v(TAG, waitingPosts.toString());
if (pos >= totalItemCount && pos != scrollPos) {
// End has been reached load more posts in
Log.v(TAG, "reached end");
if (waitingPosts == false) {
Log.v(TAG, "getting posts");
getPosts();
scrollPos = firstVisibleItem + visibleItemCount;
}
}
}
}
@Override
public void onScrollStateChanged(AbsListView view, int
scrollState) {
}
});
}
gridview.setOnItemClickListener(new
AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position,
long id) {
// Log.v(TAG, "listsizeonclick: " + image_list_id.size());
// Log.v(TAG, "listsizeonclick2: " + a
all_images_list.size());
SinglePostFragment singlePostFragment = new
SinglePostFragment();
Bundle args = new Bundle();
args.putString("post_id", image_list_id.get(position));
args.putString("post_image_url",
all_images_list.get(position));
singlePostFragment.setArguments(args);
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, singlePostFragment).commit();
}
});
} catch (
JSONException e)
{
e.printStackTrace();
}
}
^ пример того, как я загружаю изображения из API в представление сетки в одном из фрагментов
Этот код использует простой макет lin с кнопками, но использование BottomNavigationView дает ту же производительность, что иэто