在条件中跳过项目Android网格视图 [英] Skip Item Android Grid View in condition

查看:86
本文介绍了在条件中跳过项目Android网格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to build an android application,I have a 3 columns gridview that show a list of points,I want to jump to next line in a condition even when some of the columns are empty

示例我想要做这样的事情:

Example I want to do something like this:

 ARX ARH
 BRH BRX BRY
 CRH
 DRH DRY


推荐答案


尝试这一项

您的列表项目行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 for List会像这样

And CustomAdapter for List Would be like this



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;
    }
}

这篇关于在条件中跳过项目Android网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆