简体   繁体   English

Android-如何使用按钮显示空白网格视图

[英]Android-How to display blank gridview with button

i want to display blank dynamic grids like this 我想显示这样的空白动态网格

空白网格 i tried to populate grid like this but for this i need to send drawable int array to baseadapter.I know its not right way to do this 我试图像这样填充网格,但为此我需要将drawable int数组发送到baseadapter。我知道它不是正确的方法来做到这一点

Please Consider this scenario: 请考虑以下情况:

1) User Will get this kind of screen with blank grid with "+" button to add images to grid and "-" button if image is exist on grid 1)用户将获得这种带有空白网格的屏幕,其中“+”按钮将图像添加到网格,如果图像存在于网格上,则显示“ - ”按钮

2) Increase Grid Dynamically as soon as user filled second last blank grid of GridView. 2)一旦用户填充GridView的第二个空白网格,就立即动态增加网格。

Consider this question too Alternate Question 考虑这个问题太替代问题

I have created a dummy approach for the issue (To add modify the Gridview dynamically): 我已经为这个问题创建了一个虚拟方法(要动态添加修改Gridview):

Create an activity Main3Activity 创建活动Main3Activity

  public class Main3Activity extends AppCompatActivity implements ViewClickCallBack {
    private RecyclerView recyclerView;
    private GridAdapter gridAdapter;
    private List<Model> models = new ArrayList<>();
    private final int SIZE_NEXT_ITEM = 5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        recyclerView = (RecyclerView) findViewById(R.id.grid_recycle);
        gridAdapter = new GridAdapter(this);
        getNextModel();
        gridAdapter.setModels(models);
        RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 3);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setAdapter(gridAdapter);
    }

    @Override
    public void viewClicked(int position) {
        models.get(position - 1).setUploaded(true);// Set the upload flag as true for the clicked item
        int gridItemCount = gridAdapter.getItemCount();// Get the total count of items in gridview
        if ((gridItemCount - position) == 1) { // check if the clicked item is second last, if yes then difference would be 1

            getNextModel();
            gridAdapter.setModels(models);

        } else {
            Toast.makeText(this, "Popup Image picker", Toast.LENGTH_SHORT).show();
        }
        gridAdapter.notifyDataSetChanged();
    }

    /**
     * Function to get the set (or next set) of objects that
     * we want to show in GRID view.
     *
     * These objects will be added to a list.
     * This list will act as data source for adapter
     **/
    private void getNextModel() {
        for (int i = 0; i < SIZE_NEXT_ITEM; i++) {
            Model model = new Model();
            model.setUploaded(false);
            models.add(model);
        }

    }


}

XML as activity_main3 XML作为activity_main3

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="work.sof.ghost.myapplication.Main3Activity">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/grid_recycle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</RelativeLayout>

An adapter say GridAdapter 一个适配器说GridAdapter

public class GridAdapter extends RecyclerView.Adapter<GridAdapter.GridViewHolder> {
private ViewClickCallBack viewClickCallBack;


private List<Model> models;

public GridAdapter(ViewClickCallBack viewClickCallBack) {
    this.viewClickCallBack = viewClickCallBack;
}

    class GridViewHolder extends RecyclerView.ViewHolder {
        public TextView textView;


        public GridViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.text_some);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (viewClickCallBack != null) {
                        Log.e("Element Index", "" + getAdapterPosition());
                        /**
                         * Increment the position by 1, as getAdapterPosition will
                         * return the index (count starts from 0) of the element.
                         * Hence, to simplify, we will increment the index by one,
                         * so that when we calculate the second last element, we will
                         * check the difference for 1.
                         * */
                        viewClickCallBack.viewClicked(getAdapterPosition() + 1);
                    }
                }
            });
        }
    }

    @Override
    public GridViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.grid_view, parent, false);

        return new GridViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(GridViewHolder holder, int position) {
        Model model = getModel(position);
        if (model.isUploaded()) {
            holder.textView.setText("-");
        } else {
            holder.textView.setText("+");
        }

    }

    @Override
    public int getItemCount() {
        if (models != null) {
            return models.size();
        }
        return 0;
    }

    private Model getModel(int position) {
        if (models != null) {
            return models.get(position);
        }
        return null;
    }

    public void setModels(List<Model> models) {
        this.models = models;
    }
}

An model class Model 模型类模型

public class Model {
    private String imagePath;
    private boolean isUploaded;

    public String getImagePath() {
        return imagePath;
    }

    public void setImagePath(String imagePath) {
        this.imagePath = imagePath;
    }

    public boolean isUploaded() {
        return isUploaded;
    }

    public void setUploaded(boolean uploaded) {
        isUploaded = uploaded;
    }
}

A layout for grid view (i know its not same as shown in question :( ) 网格视图的布局(我知道它与问题所示的不一样:()

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_marginRight="5dp"
    android:background="@drawable/rect_drawable"
    android:orientation="vertical">
    <ImageView
        android:src="@drawable/ic_launcher"
        android:id="@+id/image_holder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_gravity="right"
        android:id="@+id/text_some"
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:text="+" />
</LinearLayout>

An drawable file rect_drawable 一个可绘制的文件rect_drawable

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

    <size
        android:width="6dp"
        android:height="6dp" />
    <solid android:color="@color/colorPrimary" />
</shape>

For the alternate question 对于替代问题

" How to send multiple images to server via AsyncTask as soon as user press Save or Done button " : 用户按”保存“或”完成“按钮后,如何通过AsyncTask将多个图像发送到服务器 ”:

Use 使用

a) executeOnExecutor(java.util.concurrent.Executor, Object[]) with THREAD_POOL_EXECUTOR . a)使用THREAD_POOL_EXECUTOR的 executeOnExecutor(java.util.concurrent.Executor,Object []) To send 1 image per async task in parallel, More info at https://developer.android.com/reference/android/os/AsyncTask.html 要同时发送每个异步任务1个图像,请访问https://developer.android.com/reference/android/os/AsyncTask.html

or 要么

b) You can follow https://stackoverflow.com/a/7130806/1920735 b)您可以关注https://stackoverflow.com/a/7130806/1920735

This will show UI like this 这将显示这样的UI 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM