1

私はフラグメントをロードするベースのナビゲーション・ドロワ・アクティビティを持っています。その断片。フラグメントのレイアウトにリサイクルビューがあります。リサイクラービューには、Glideを使用してインターネットから画像を読み込むアダプターがあります。イメージを読み込むことができません。 (マニフェストでインターネット許可を追加しました)。 マイベース活性がある:recyclerviewはフラグメント内にアダプタ値をロードしていません

import android.net.Uri; 
import android.os.Bundle; 
import android.support.design.widget.NavigationView; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.RelativeLayout; 
import android.widget.Toast; 

public class BaseActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener, Item1Fragment.OnFragmentInteractionListener { 

    /** 
    * Frame layout: Which is going to be used as parent layout for child activity layout. 
    * This layout is protected so that child activity can access this 
    */ 
    protected RelativeLayout relativeLayout; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     relativeLayout = (RelativeLayout) findViewById(R.id.content_frame); 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_base); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     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); 


     //Add the Very First i.e Squad Fragment to the Container 
     Fragment item1Fragment = new Item1Fragment(); 
     FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
     fragmentTransaction.add(R.id.content_frame, item1Fragment, null); 
     fragmentTransaction.commit(); 
    } 

    @Override 
    public void onFragmentInteraction(Uri uri) { 

    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.base, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 
     FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction(); 
     if (id == R.id.nav_camera) { 
      Fragment item1Fragment = new Item1Fragment(); 
      fragmentTransaction.replace(R.id.content_frame, item1Fragment); 
      fragmentTransaction.commit(); 
     } else if (id == R.id.nav_gallery) { 
      Fragment item2Fragment = new Item2Fragment(); 
      fragmentTransaction.replace(R.id.content_frame, item2Fragment); 
      fragmentTransaction.commit(); 
     } else if (id == R.id.nav_slideshow) { 
      Toast.makeText(getApplicationContext(), "333", Toast.LENGTH_SHORT).show(); 
     } else if (id == R.id.nav_manage) { 
      Toast.makeText(getApplicationContext(), "444", Toast.LENGTH_SHORT).show(); 
     } else if (id == R.id.nav_share) { 
      Toast.makeText(getApplicationContext(), "555", Toast.LENGTH_SHORT).show(); 
     } else if (id == R.id.nav_send) { 
      Toast.makeText(getApplicationContext(), "666", Toast.LENGTH_SHORT).show(); 
     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 

    // Container Activity must implement this interface 
    public interface OnHeadlineSelectedListener { 
     public void onArticleSelected(int position); 
    } 
} 

アクティビティレイアウトは次のとおり

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 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/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" android:background="@color/colorPrimary" 
    tools:openDrawer="start"> 

    <RelativeLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <include 
     layout="@layout/app_bar_base" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_base" 
     app:menu="@menu/activity_base_drawer" /> 
</android.support.v4.widget.DrawerLayout> 

フラグメント:

import android.content.Context; 
import android.content.res.Configuration; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v7.widget.GridLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

import java.util.ArrayList; 

public class Item1Fragment extends Fragment { 

    private OnFragmentInteractionListener mListener; 
    private ArrayList<ImageModel> data = new ArrayList<>(); 
    private GalleryAdapter mAdapter; 
    private RecyclerView mRecyclerView; 

    public static String IMGS[] = { 
      "https://images.unsplash.com/photo-1444090542259-0af8fa96557e?q=80&fm=jpg&w=1080&fit=max&s=4b703b77b42e067f949d14581f35019b", 
      "https://images.unsplash.com/photo-1439546743462-802cabef8e97?dpr=2&fit=crop&fm=jpg&h=725&q=50&w=1300", 
      "https://images.unsplash.com/photo-1441155472722-d17942a2b76a?q=80&fm=jpg&w=1080&fit=max&s=80cb5dbcf01265bb81c5e8380e4f5cc1", 
      "https://images.unsplash.com/photo-1437651025703-2858c944e3eb?dpr=2&fit=crop&fm=jpg&h=725&q=50&w=1300", 
      "https://images.unsplash.com/photo-1431538510849-b719825bf08b?dpr=2&fit=crop&fm=jpg&h=725&q=50&w=1300", 
      "https://images.unsplash.com/photo-1434873740857-1bc5653afda8?dpr=2&fit=crop&fm=jpg&h=725&q=50&w=1300", 
      "https://images.unsplash.com/photo-1439396087961-98bc12c21176?dpr=2&fit=crop&fm=jpg&h=725&q=50&w=1300", 
      "https://images.unsplash.com/photo-1433616174899-f847df236857?dpr=2&fit=crop&fm=jpg&h=725&q=50&w=1300", 
      "https://images.unsplash.com/photo-1438480478735-3234e63615bb?dpr=2&fit=crop&fm=jpg&h=725&q=50&w=1300", 
      "https://images.unsplash.com/photo-1438027316524-6078d503224b?dpr=2&fit=crop&fm=jpg&h=725&q=50&w=1300" 
    }; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     for (int i = 0; i < IMGS.length; i++) { 

      ImageModel imageModel = new ImageModel(); 
      imageModel.setName("Image " + i); 
      imageModel.setUrl(IMGS[i]); 
      data.add(imageModel); 
     } 

     View view = inflater.inflate(R.layout.fragment_item1,container,false); 
     mRecyclerView = (RecyclerView)view.findViewById(R.id.list); 
     if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 
      mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3)); 
     }else{ 
      mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2)); 
     } 

     mRecyclerView.setHasFixedSize(true); 
     mAdapter = new GalleryAdapter(getActivity().getApplicationContext(), data); 
     mRecyclerView.setAdapter(mAdapter); 

     return view; 
    } 

    // 
    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     if (context instanceof OnFragmentInteractionListener) { 
      mListener = (OnFragmentInteractionListener) context; 
     } else { 
      throw new RuntimeException(context.toString() 
        + " must implement OnFragmentInteractionListener"); 
     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mListener = null; 
    } 

    public interface OnFragmentInteractionListener { 
     // TODO: Update argument type and name 
     void onFragmentInteraction(Uri uri); 
    } 
} 

断片レイアウト:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    tools:context="com.akl.nav2.Item1Fragment"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/list" android:background="#FFBB00" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 
</RelativeLayout> 

アダプタ:

import android.content.Context; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 

import com.bumptech.glide.Glide; 
import com.bumptech.glide.load.engine.DiskCacheStrategy; 

import java.util.ArrayList; 
import java.util.List; 

/** 
* Created by akl on 3/12/2016. 
*/ 
public class GalleryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 

    Context context; 
    List<ImageModel> data = new ArrayList<>(); 

    public GalleryAdapter(Context context, List<ImageModel> data) { 
     this.context = context; 
     this.data = data; 
    } 

    @Override 
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     RecyclerView.ViewHolder viewHolder; 
     View v; 
     v = LayoutInflater.from(parent.getContext()).inflate(
       R.layout.list_item, parent, false); 
     viewHolder = new MyItemHolder(v); 

     return viewHolder; 
    } 

    @Override 
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 

     Glide.with(context).load(data.get(position).getUrl()) 
       .thumbnail(0.5f) 
       .override(200, 200) 
       .crossFade() 
       .diskCacheStrategy(DiskCacheStrategy.ALL) 
       .into(((MyItemHolder) holder).mImg); 
    } 

    @Override 
    public int getItemCount() { 
     return data.size(); 
    } 

    public static class MyItemHolder extends RecyclerView.ViewHolder { 
     ImageView mImg; 

     public MyItemHolder(View itemView) { 
      super(itemView); 

      mImg = (ImageView) itemView.findViewById(R.id.item_img); 
     } 
    } 
} 

マニフェスト:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.akl.nav2"> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name=".BaseActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category 
        android:name="android.intent.category.LAUNCHER" 
        android:theme="@style/AppTheme.NoActionBar" /> 
      </intent-filter> 
     </activity> 

    </application> 
    <uses-permission android:name="android.permission.INTERNET" /> 
</manifest> 
+0

何らかのエラーまたは例外が発生していますか? –

+0

いいえ、エラーはありません。例外はありません。実際、フラグメントのonCreateView()では、ビューが返される直前に、ブレークポイントを設定し、アダプタ値が使用可能であることを確認します。グライドが画像をレンダリングしていないという問題がある場合、私はそれを得られません。 – Adi

+1

あなたのアダプタonBindViewHolderにブレークポイントを入れて、コントロールが来ているかどうか一度チェックしてください –

答えて

0

コードは大丈夫でした。 @HoangNguyenによって提案されたクリーンなビルドは、コードを実行するのに十分でした。

関連する問題