我面临的问题真的很奇怪。但我需要求帮助。如果没有可用的记录,我试图显示图像。但图像不可见。
这是流程。
我有content_main.xml
我有一个回收视图,一个relativeLayout
用于权限,一个ImageView
用于Image。
如果未授予许可,我想显示权限布局(工作)
如果授予了权限,那么我从数据库获取记录并设置为适配器但是如果recordList大小为0则我需要显示图像(不工作)。
相反,我只能看到fab按钮和白色背景。
这是我的content_main.xml(我使用导航抽屉,因此content_main.xml
中包含app_bar_main.xml
)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.sandy.callrecorder.MainActivity"
tools:showIn="@layout/app_bar_main">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/no_records"
android:visibility="gone"
android:id="@+id/backgroundImage"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/record_recycle_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:visibility="gone" />
<RelativeLayout
android:id="@+id/permissionLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingEnd="36dip"
android:paddingLeft="36dip"
android:paddingRight="36dip"
android:paddingStart="36dip"
>
<Button
android:id="@+id/btnAllowPermission"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="@string/allow_permissions" />
</RelativeLayout>
这是我的MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById();
managePermissions();
}
private void findViewById() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
backgroundImage = (ImageView) findViewById(R.id.backgroundImage);
permissionLayout = (RelativeLayout) findViewById(R.id.permissionLayout);
mBtnAllowPermission = (Button)findViewById(R.id.btnAllowPermission);
recyclerView = (RecyclerView) findViewById(R.id.record_recycle_list);
mRelativeLayout = (RelativeLayout) findViewById(R.id.content_main);
fab = (FloatingActionButton) findViewById(R.id.fab);
mBtnAllowPermission.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ActivityCompat.requestPermissions(MainActivity.this, permission, MY_PERMISSIONS_REQUES);
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
private void managePermissions() {
if (!hasPermissions(permission)) {
permissionLayout.setVisibility(View.VISIBLE);
backgroundImage.setVisibility(View.GONE);
} else {
initializeRecycleView();
startService();
}
}
private void initializeRecycleView() {
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLayoutManager);
DBHelper mDBHelper = new DBHelper(this);
mDBHelper.open();
ArrayList<CallRecord> listOfRecord = mDBHelper.getRecordList();
mDBHelper.close();
listOfRecords = new ArrayList<>();
for (CallRecord objCallRecord : listOfRecord) {
if (objCallRecord.getIsArchive() == 0) {
listOfRecords.add(objCallRecord);
}
}
setListToRecycle(listOfRecords);
}
private void setListToRecycle(ArrayList<CallRecord> listOfRecords) {
// define an adapter
if (listOfRecords.size() <= 0) {
fab.setVisibility(View.VISIBLE);
permissionLayout.setVisibility(View.GONE);
backgroundImage.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
} else {
fab.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
backgroundImage.setVisibility(View.GONE);
permissionLayout.setVisibility(View.GONE);
mAdapter = new RecordListAdapter(listOfRecords, this);
recyclerView.setAdapter(mAdapter);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MY_PERMISSIONS_REQUES: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (!isMyServiceRunning(TService.class)) {
Intent intent = new Intent(MainActivity.this, TService.class);
startService(intent);
}
initializeRecycleView();
} else {
if (isMyServiceRunning(TService.class)) {
Intent intent = new Intent(MainActivity.this, TService.class);
stopService(intent);
}
}
}
}
}
}
我在过去24小时内遇到了这个问题,但没有任何帮助。请给我提示。