我正在创建一个Android应用,它在一个活动中包含一个带有多个片段的TabLayout。如果我的服务器中有可用的内容,则这些片段显示RecyclerView;如果没有内容,则显示EmptyView。
但是,如果我在活动标签中没有内容,如果我在标签之间横向滚动,则应用会崩溃。如果我在选项卡之间单击此导航,则不会发生此问题。我该如何解决?
我的代码是:
MessagesActivity.java:
.slick-slide
{
height: 300px;
}
MessagesInboxTab.java :(目前所有标签都包含此类)
public class MessagesActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private Toolbar toolbar;
private NavigationView navigationView;
private TabLayout tabLayout;
private ViewPager pager;
private FloatingActionButton newMessageButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.messagesactivity);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
pager = (ViewPager) findViewById(R.id.messagesActivity_viewPager);
setupViewPager(pager);
tabLayout = (TabLayout) findViewById(R.id.messagesActivity_tabs);
tabLayout.setupWithViewPager(pager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
pager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
private void setupViewPager(ViewPager viewPager) {
MessagesTabAdapter adapter = new MessagesTabAdapter(getSupportFragmentManager());
adapter.addFragment(new MessagesInboxTab(), getResources().getString(R.string.Inbox));
adapter.addFragment(new MessagesSentTab(), getResources().getString(R.string.Sent));
adapter.addFragment(new MessagesTrashTab(), getResources().getString(R.string.Trash));
viewPager.setAdapter(adapter);
}
class MessagesTabAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitleList = new ArrayList<>();
public MessagesTabAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return fragmentTitleList.get(position);
}
public void addFragment(Fragment fragment, String title) {
fragmentList.add(fragment);
fragmentTitleList.add(title);
}
}
}
片段的Xml:
public class MessagesInboxTab extends Fragment {
private RecyclerView recyclerView;
private ScrollView emptyView;
private ProgressBar progressView;
private ArrayList<MessageListItem> inboxList = new ArrayList<MessageListItem>();
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list_tab, container, false);
progressView = (ProgressBar) view.findViewById(R.id.progressBar_list);
emptyView = (ScrollView) view.findViewById(R.id.emptyView_list);
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView_list);
progressView.setVisibility(View.VISIBLE);
getInbox();
return view;
}
public void getInbox() {
JsonObjectRequest jsonRequest = new JsonObjectRequest
(Request.Method.GET, "http://my_domain.com/service/inbox", (String) null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//Check if there are messages
if (result!=null) { //There are messages
//Handle messages and store info in InboxList
final MessagesListItemAdapter adapter = new MessagesListItemAdapter(inboxList);
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
recyclerView.setItemAnimator(new DefaultItemAnimator());
adapter.notifyDataSetChanged();
progressView.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
} else { //No messages
progressView.setVisibility(View.GONE);
emptyView.setVisibility(View.VISIBLE);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
Volley.newRequestQueue(getActivity()).add(jsonRequest);
}
}
最后,错误代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ProgressBar
android:id="@+id/progressBar_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_centerInParent="true"
style="?android:attr/progressBarStyleLarge" />
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ScrollView
android:id="@+id/emptyView_list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="93.3dp"
android:paddingBottom="93.3dp"
android:background="#000000" >
</RelativeLayout>
</ScrollView>
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:1)
将此添加到onCreateView()
的{{1}}
Fragment
我认为这是因为当没有内容时你没有设置任何LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
,所以最好设置一个