2

這是我真正面臨的問題。但我需要求助。如果沒有可用的記錄,我正試圖顯示圖像。但圖像不可見。ImageView如果沒有任何記錄,則不顯示圖像

這裏是流程。

我有content_main.xml其中我有一個回收視圖,一個relativeLayout許可和一個ImageView爲圖像。

如果未授予權限我想顯示權限佈局(工作)。
如果授予權限,那麼我從數據庫獲取記錄並設置爲適配器,但如果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小時這個問題,但沒有幫助。請給我提示。

+3

嘗試在ImageView上設置android:background =「@ android:color/black」來查看視圖是否正確顯示。如果是這樣的話,問題出在你的形象上。 – k0bin

+0

嘗試添加另一個drawable(任何更大的圖像)到imageview以檢查它是否可見。 – MinnuKaAnae

+1

是的imageview顯示正確與黑色,但不是圖像。什麼可能是圖像的問題? –

回答

相關問題