如何创建像附加截图一样的Recycler-view卡视图网格布局

时间:2018-05-08 16:22:08

标签: android android-layout

我正在创建一个Android应用程序,我需要在回收器视图/卡片视图网格中显示博客文章,如附加任何示例或建议?

enter image description here

或喜欢这个

enter image description here

还是喜欢这个?

enter image description here

1 个答案:

答案 0 :(得分:0)

Grid View Example

我希望我的应用的截图有点接近您想要的,如第3张截图所示。我会告诉你我的源代码,只是为了让你有个主意。

LAYOUT.XML文件这是布局文件

<LinearLayout
    android:id="@+id/layer"
    android:gravity="right"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

<Button
    android:id="@+id/addBttn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="+"/>

    <Button
        android:id="@+id/doneBttn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Done"/>
</LinearLayout>

<GridView
    android:layout_below="@id/layer"
    android:id="@+id/gridView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:gravity="center"
    android:numColumns="auto_fit"
    android:focusable="true"
    android:clickable="true"/>**

这是用于向GridView填写数据的ADAPTER类

然后,您可以通过任何活动将创建的适配器设置为网格视图

public class gridViewAdapter extends BaseAdapter{
private Context context;
private View view;
private LayoutInflater layoutInflater;
private final ArrayList<Uri> Images;

public gridViewAdapter(@NonNull Context context,ArrayList<Uri>images) {
    this.context = context;
    Images = images;
}


@Override
public int getCount() {
    return Images.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(convertView == null) {
        view = new View(context);
        view = layoutInflater.inflate(R.layout.grid_item_layout,null);
        ImageView imageView =     (ImageView)view.findViewById(R.id.imageView1);
        imageView.setImageURI(Images.get(position));

    }
    return view;
}

}

gridView =(GridView) this.findViewById(R.id.gridView);
    gridViewAdapter gridAdapter = new gridViewAdapter(this,imgUriList);
    gridView.setAdapter(gridAdapter);