0

我在导航栏上有 5 个项目,每个项目都有片段。我想要的是,我希望我的图像在每次点击我存储在可绘制文件夹中的导航栏项目时显示。我可以显示文本,但每当我尝试使用 gridview 显示图像时,应用程序就会出错。谁能帮助我。我试图将 gridview 放在 acitvity_main.xml 中,并将以下代码放在 MainAcitvity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GridView gridView = (GridView) findViewById(R.id.gridView);

    gridView.setAdapter(new ImageAdapter(this));
}

单击导航栏项后,我的图像将显示在我要显示的主页中。注意:导航栏中的所有5个项目都是为了显示不同类别的图像。简而言之,我在导航栏中有 5 个项目的 10 个图像,即可绘制文件夹中的 50 个图像。单击第一项时,我希望显示 10 张图像,并且在单击导航栏上的第二项时与 10 张图像相同,依此类推。

4

1 回答 1

0

如果您使用的是 NavigationDrawer,那么最好为每个片段编写 recyclerview。

因此,在每个片段中,您可以添加如下代码:[请考虑所有内容都已定义和初始化,是的,我正在使用Universal Image Loader加载图像

// Create global configuration and initialize ImageLoader with this config
    ImageLoaderConfiguration config = new 
    ImageLoaderConfiguration.Builder(this).build();
    ImageLoader.getInstance().init(config);

    rv_photo_grid = (RecyclerView) findViewById(R.id.rv_photo_grid);
    GridLayoutManager layoutManager;
    layoutManager = new GridLayoutManager(this, 3);
    rv_photo_grid.setLayoutManager(layoutManager);

    photoUrls.add("image1");
    photoUrls.add("image2");
    photoUrls.add("image3");
    photoUrls.add("image4");
    photoUrls.add("image5");

    photoGridAdapter = new PhotoGridAdapter(this, photoUrls);

    rv_photo_grid.setAdapter(photoGridAdapter);

您的适配器可以是这样的:

public class PhotoGridAdapter extends 
RecyclerView.Adapter<PhotoGridAdapter.ItemHolder>  {
private List<String> arraylist;
private Activity mContext;
private boolean isGrid;
ImageLoader imageLoader;

public PhotoGridAdapter(Activity context, List<String> arraylist) {
    this.arraylist = arraylist;
    this.mContext = context;
    imageLoader = ImageLoader.getInstance();
}

@Override
public ItemHolder onCreateViewHolder(ViewGroup viewGroup, int i)              
View v =    LayoutInflater.from(viewGroup.getContext())
                          .inflate(R.layout.item_photo_grid, null);
ItemHolder ml = new ItemHolder(v);
return ml;
}

@Override
public void onBindViewHolder(final ItemHolder itemHolder, int i) {
    imageLoader.displayImage(arraylist.get(i),itemHolder.iv_photo);
}

@Override
public int getItemCount() {
    return (5);
}

public void updateDataSet(List<String> arraylist) {
    this.arraylist = arraylist;
}

public class ItemHolder extends RecyclerView.ViewHolder implements 
View.OnClickListener {
    protected TextView tv_more_images_count, artist;
    protected ImageView iv_photo;
    protected View footer;

    public ItemHolder(View view) {
        super(view);
        this.iv_photo = (ImageView) view.findViewById(R.id.iv_photo);
        tv_more_images_count = (TextView) 
             view.findViewById(R.id.tv_more_images_count);
        view.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
    }
 }   

}

每个片段中的 Recyclerview 可以这样:

 <RelativeLayout
        android:id="@+id/relativelayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_photo_grid"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


    </RelativeLayout>

而 item_photo_grid.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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:gravity="center"
android:orientation="vertical">

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/iv_photo"
    android:layout_width="fill_parent"
    android:layout_height="150dp"
    android:scaleType="fitXY"
    app:srcCompat="@drawable/default_placeholder" />

希望这有帮助!

快乐编码^_^

于 2017-07-20T20:28:03.063 回答