当从外部服务器或API加载数据时,如何消除活动之间出现的黑屏?

问题描述 投票:0回答:1

我有两个活动,第一个活动是启动屏幕,该屏幕在4秒钟内消失,在显示第二个活动之前,黑屏出现几秒钟。我正在使用https://newsapi.org/将数据加载到第二个活动中。由于加载数据需要时间,因此直到该请求从服务器返回之前,黑屏一直保留在前面,一旦我有了数据,第二个活动便会填充UI。

我正在使用AsyncTask在后台执行此数据加载。但是仍然面临黑屏问题,在活动之间切换时,我会出现[。我试图在与片段关联的活动的onCreate和片段中都使用进度条,但是仍然出现黑屏。

Splash Screen appears for 4s

Black screen between two activities

Second Activity after data is loaded

飞溅活动代码

public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView imageView = findViewById(R.id.imageView); imageView.animate().rotationBy(720).setDuration(3000); final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { Intent i = new Intent(MainActivity.this, homepage.class ); startActivity(i); finish(); } },4000); }
}

片段

public class FragmentRecentNews extends Fragment { View view; private List<RecentNews> recentNewsList; NewsRepo newsRepo; public FragmentRecentNews() { } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { //recentNews newsRepo= new NewsRepo(); recentNewsList = newsRepo.GetTopHeadlines("https://newsapi.org/v2/top-headlines?country=au&apiKey=b4fcfc3bfc9c4a8380a83a02b3d0cfc7"); view = inflater.inflate(R.layout.recentnews_fragment,container,false); RecyclerView recyclerView = view.findViewById(R.id.recyclerViewid); RecycleViewAdapter adapter = new RecycleViewAdapter(getActivity(),recentNewsList); recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 1)); recyclerView.setAdapter(adapter); return view; }
}

第二活动

public class homepage extends AppCompatActivity { private TabLayout tabLayout; private ViewPager viewPager; private DrawerLayout mDrawerLayout; private ActionBarDrawerToggle mToggle; private List<RecentNews> recentNewsList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_homepage); //settanlayout tabLayout = findViewById(R.id.tablayoutid); viewPager = findViewById(R.id.viewpagerid); ViewPageAdapter viewPageAdapter = new ViewPageAdapter(getSupportFragmentManager()); viewPageAdapter.AddFragment(new FragmentRecentNews(),"Recent News"); viewPageAdapter.AddFragment(new FragmentNewsSources(),"Sources"); viewPageAdapter.AddFragment(new FragmentCountries(),"Countries"); viewPager.setAdapter(viewPageAdapter); tabLayout.setupWithViewPager(viewPager); //Drawer naviagtion mDrawerLayout = findViewById(R.id.drwaerlayoutid); mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close); mDrawerLayout.addDrawerListener(mToggle); mToggle.syncState(); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { if (mToggle.onOptionsItemSelected(item)){ return true; } return super.onOptionsItemSelected(item); }
}

RECYCLERVIEWADAPTER

public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.MyViewHolder>
{

private FragmentActivity mContext; private List<RecentNews> mData; private Context context; public RecycleViewAdapter(FragmentActivity mContext, List<RecentNews> mData) { this.mContext = mContext; this.mData = mData; }

}

@NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view; LayoutInflater mInflater = LayoutInflater.from(mContext); view= mInflater.inflate(R.layout.card_recentnews,parent,false); return new MyViewHolder(view); } @Override public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) { holder.title.setText(mData.get(position).getTitle()); holder.source.setText(mData.get(position).getSourceName()); // holder.author.setText(mData.get(position).getAuthor()); holder.date.setText(mData.get(position).getPublishedAt()); //holder.image.setImageResource(mData.get(position).getUrlToImage()); Picasso.with(mContext).load(mData.get(position).getUrlToImage()).into(holder.image); holder.image.setClipToOutline(true); holder.button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager fragmentManager = mContext.getSupportFragmentManager(); Intent intent = new Intent(mContext, newsdetails.class); mContext.startActivity(intent); } }); } @Override public int getItemCount() { return mData.size(); } public static class MyViewHolder extends RecyclerView.ViewHolder{ TextView title; TextView source; // TextView author; TextView date; ImageView image; Button button; public MyViewHolder(@NonNull View itemView) { super(itemView); title = (TextView) itemView.findViewById(R.id.titletextview); source = itemView.findViewById(R.id.sourcetextview); // author = itemView.findViewById(R.id.authortextview); date = itemView.findViewById(R.id.datetextview); image = itemView.findViewById(R.id.newsimageview); button = itemView.findViewById(R.id.detailsbtn); } }

}

首页布局

<androidx.drawerlayout.widget.DrawerLayout android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/drwaerlayoutid" > <LinearLayout 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:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".homepage"> <RelativeLayout android:layout_width="match_parent" android:layout_height="200dp"> <ImageView android:layout_width="match_parent" android:layout_height="200dp" android:scaleType="centerCrop" android:src="@drawable/bg" android:background="@color/colorPrimary"> </ImageView> <ImageView android:id="@+id/icon" android:layout_width="92dp" android:layout_height="73dp" android:layout_alignParentTop="true" android:layout_centerInParent="true" android:src="@drawable/logon"></ImageView> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="@string/appdesc" android:textColor="#ffff" android:layout_below="@+id/heading" android:paddingTop="0dp" android:textSize="18dp" /> <TextView android:layout_below="@+id/icon" android:id="@+id/heading" android:layout_width="match_parent" android:layout_height="wrap_content" android:fontFamily="sans-serif-black" android:gravity="center" android:text="Awais World News" android:textColor="#ffffff" android:textSize="30dp" android:textStyle="bold"> </TextView> </RelativeLayout> <com.google.android.material.tabs.TabLayout android:layout_width="match_parent" android:layout_height="60dp" app:tabGravity="fill" app:tabMode="fixed" android:id="@+id/tablayoutid" android:background="@color/colorPrimaryDark" app:tabTextColor="#fff" app:tabIndicatorColor="#FFB300" app:tabIndicatorHeight="4dp" /> <androidx.viewpager.widget.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/viewpagerid"/> </LinearLayout> <com.google.android.material.navigation.NavigationView android:layout_width="wrap_content" android:layout_height="match_parent" app:menu="@menu/menu" android:layout_gravity="start"> </com.google.android.material.navigation.NavigationView> </androidx.drawerlayout.widget.DrawerLayout>

Styles.XML

<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> </resources>
android api android-fragments android-activity splash-screen
1个回答
0
投票
好像您的网络呼叫是在FragmentRecentNews中创建视图之前进行的。请尝试执行以下步骤:1. onCreateView必须仅包含ui创建步骤,例如

view = inflater.inflate(R.layout.recentnews_fragment,container,false); return view

    创建片段的重写onViewCreated方法,并在其中粘贴您的api调用代码
  • 您看到黑屏,因为onCreateView做很多工作,并且无法绘制片段。
  • © www.soinside.com 2019 - 2024. All rights reserved.