条件跳过项目Android网格视图

时间:2017-06-07 14:32:22

标签: android android-layout gridview

我正在尝试构建一个Android应用程序,我有一个3列gridview显示一个点列表,我想在一个条件中跳转到下一行,即使某些列是空的

示例我想做这样的事情:

 ARX ARH
 BRH BRX BRY
 CRH
 DRH DRY

1 个答案:

答案 0 :(得分:0)

  

试试这一个   你的List项Row XML就是这样的   的 Abc.xml

<?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:orientation="vertical"
    android:background="@android:color/darker_gray">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="5dp">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Name1"
            android:textSize="18sp"
            android:background="@android:color/white"
            android:layout_margin="5dp"/>

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Name2"
            android:textSize="18sp"
            android:layout_margin="5dp"
            android:background="@android:color/white"/>

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Name3"
            android:textSize="18sp"
            android:layout_margin="5dp"
            android:background="@android:color/white"/>


    </LinearLayout>


</LinearLayout>
  

列表的CustomAdapter就像这样

public class Custom_Adapter_Products extends BaseAdapter {
    Context context;
    String[] list = {"AAA","AAA","AAA","AAA","AAA","","AAA", "AAA","AAA"};

    public Custom_Adapter_Products(Context context){
        this.context = context;
    }

    @Override
    public int getCount() {
        return (list.length/3);
    }

    @Override
    public Object getItem(int position) {

        return null;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int pos, View convertView, ViewGroup parent) {
        try{
            int position = (pos*3);
            ViewHolder holder;
            LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (convertView == null)
            {
                convertView = vi.inflate(R.layout.abc, null);
                holder = createViewHolder(convertView);
                convertView.setTag(holder);
            }
            else
            {
                holder = (ViewHolder) convertView.getTag();
            }


            if(!list[position].equals("")){
                holder.textView1.setText(list[position]);
                holder.textView1.setVisibility(View.VISIBLE);
            }
            else{
                holder.textView1.setVisibility(View.GONE);
            }

            if(!list[position+1].equals("")){
                holder.textView2.setText(list[position]);
                holder.textView2.setVisibility(View.VISIBLE);
            }
            else{
                holder.textView2.setVisibility(View.GONE);
            }

            if(!list[position+2].equals("")){
                holder.textView3.setText(list[position]);
                holder.textView3.setVisibility(View.VISIBLE);
            }
            else{
                holder.textView3.setVisibility(View.GONE);
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return convertView;
    }


    private static class ViewHolder
    {
        public TextView textView1;
        public TextView textView2;
        public TextView textView3;
    }

    private ViewHolder createViewHolder(View v) {
        ViewHolder holder = new ViewHolder();
        try{
            holder.textView1 = (TextView) v.findViewById(R.id.textView1);
            holder.textView2 = (TextView) v.findViewById(R.id.textView2);
            holder.textView3 = (TextView) v.findViewById(R.id.textView3);
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return holder;
    }
}