Android GridView绘图布局

时间:2016-07-05 08:14:08

标签: android android-layout gridview getview

我想通过使用GridView实现以下图像设计,我尝试过使用这些代码行但是我在5到6,15和16之间没有得到空白,依此类推,因为它在设计中显示。

public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

final ViewHolder holder;
View view = convertView;
if (view == null) {

    view = mInflater.inflate(R.layout.table_selection_main_item_list,
            parent, false);

    holder = new ViewHolder();
    assert view != null;

    holder.tableSelectionBtn = (TextView) view
            .findViewById(R.id.item_table_selection_btn);
    holder.viewSideSpace = (View) view.findViewById(R.id.view_right_side);

    Log.e("","Table Status ID:::"+tableSelectionList.get(position)
            .getTable_selection_table_status_id()+"Count :: "+getCount());



    if(((position+1)%5==0)&&((position+1)%10==5)){

        gridView.setX(30);
        gridView.setHorizontalSpacing(30);
        holder.viewSideSpace.setVisibility(View.VISIBLE);
        holder.tableSelectionBtn.
        gridView.setColumnWidth(180);
        FrameLayout.LayoutParams layoutParams = new      FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout
                .LayoutParams.WRAP_CONTENT);
        layoutParams.leftMargin = 45;
        layoutParams.rightMargin = 45;
        layoutParams.topMargin = 45;
        layoutParams.bottomMargin = 45;


        int size = 150;
        GridLayout.LayoutParams layoutParams=new GridLayout.LayoutParams();
        layoutParams.setMargins(70,70,70,70);

        layoutParams.width=size;
        layoutParams.height=size;
        holder.tableSelectionBtn.sethor//set(0,0,60,0);
        holder.tableSelectionBtn.setLayoutParams(layoutParams);

        int pxWidth = (int) (100* Resources.getSystem().getDisplayMetrics().density);
        LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(pxWidth,pxWidth);
        llp.setMargins(0, 0, 40, 0); // llp.setMargins(left, top, right, bottom);

        holder.tableSelectionBtn.setLayoutParams(llp);


    }else{
        //holder.viewSideSpace.setVisibility(View.GONE);
        int pxWidth = (int) (30* Resources.getSystem().getDisplayMetrics().density);
        LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(pxWidth,pxWidth);
        llp.setMargins(0, 0, 0, 0);  llp.setMargins(left, top, right, bottom);
        holder.tableSelectionBtn.setLayoutParams(llp);
    }



} else {
    holder = (ViewHolder) view.getTag();
}

enter image description here

1 个答案:

答案 0 :(得分:0)

  1. 解决方案非常简单,您只需添加一个空白项目 在您的数据列表中获取5到6,15和5之间的空格 16等等。我在这里使用RecyclerView,但您也可以使用它 GridView中的代码。这是代码。

    public class MainActivity extends AppCompatActivity {
    
        RecyclerView viewList;
        private GridLayoutManager lLayout;
        int temp=5; 
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            viewList=(RecyclerView)findViewById(R.id.recyclerView);
    
            ArrayList<String> listInt=new ArrayList<>();
            for(int i=1;i<=80;i++){
                listInt.add(i+"");
                if(temp==i){// logic for adding blank items after 5 elements added
                    temp+=10;
                    listInt.add("");
                }
            }
    
            lLayout = new GridLayoutManager(MainActivity.this, 11);//no of rows are 11enter image description here
            viewList.setHasFixedSize(true);
            viewList.setLayoutManager(lLayout);
    
            CircleViewAdapter adapter=new CircleViewAdapter(listInt,this);
            viewList.setAdapter(adapter);
    
        }
    }
    
    Adapter code
    
    public class CircleViewAdapter extends RecyclerView.Adapter<CircleViewAdapter.CircleHolder> {
        private final Context context;
        private List<String> items;
    
        // Provide a reference to the views for each data item
        // Complex data items may need more than one view per item, and
        // you provide access to all the views for a data item in a view holder
        public class CircleHolder extends RecyclerView.ViewHolder {
            // each data item is just a string in this case
            public TextView txtView1;
    
            public CircleHolder(View v) {
                super(v);
                txtView1 = (TextView) v.findViewById(R.id.textView);
            }
        }
    
    
    
        public CircleViewAdapter(List<String> items, Context context) {
            this.items = items;
            this.context = context;
        }
    
        @Override
        public CircleViewAdapter.CircleHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item, parent, false);
            return new CircleHolder(v);
        }
    
        @Override
        public void onBindViewHolder(CircleHolder holder, int position) {
            String item = items.get(position);
            //TODO Fill in your logic for binding the view.
            holder.txtView1.setText(item);
    
    
        }
    
        @Override
        public int getItemCount() {
            if (items == null) {
                return 0;
            }
            return items.size();
        }
    }
     activiy_main.xml
    
       <?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.support.v7.widget.RecyclerView
        android:layout_width="match_parenter code hereent"
        android:layout_height="match_parent"
        android:id="@+id/recyclerView"/>
    
    </RelativeLayout>
    

    item.xml

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="1"
        android:id="@+id/textView"
        android:layout_margin="5dp"/>
    

    [![结果] [1] [1]

    [1]:http://i.stack.imgur.com/uwMp9.png