在Listview中添加View

时间:2016-08-19 06:57:16

标签: android listview

Charge Tab

Hello Guys,

我用截图编辑了我的问题,你可以在充电标签上看到有列表视图,在另一个下面有几个块,在屏幕截图中你可以看到1,卡车号码和充电1并充电2,以下,有另一个块项目与块1具有相同的视图。

我需要做什么,收费(即低于LR编号)来自网络服务,可以收取任何数量的费用,如果卡车号码收取5项费用则应在拘留布局下方添加(即充电2)。(与充电2类似,在充电1下方添加)

BaseAdapter类:

public class ApprovalAdapter extends BaseAdapter {
    List<ApprovalPogo> list;
    Context context;
    LayoutInflater inflater;


    public ApprovalAdapter( Context context,List<ApprovalPogo> list) {

        this.list = list;
        this.context = context;

    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    class Viewholder{
        TextView truck_name;
        TextView lr_no;

        Button btn_approve;
        Button btn_reject;
        Button btn_approve_status_change;

        Button btn_approve2;
        Button btn_reject2;
        Button btn_chnge_status_two;

        TextView extra_height;
        TextView detention;




    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final Viewholder viewholder;

        if(convertView==null){
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = inflater.inflate(R.layout.single_row_approvals, parent, false);
            viewholder=new Viewholder();

            viewholder.truck_name = (TextView) convertView.findViewById(R.id.appr_txt_truck);
            viewholder.lr_no = (TextView) convertView.findViewById(R.id.appr_lrNo);

            viewholder.extra_height = (TextView) convertView.findViewById(R.id.extra_height);
            viewholder.detention = (TextView) convertView.findViewById(R.id.txt_detention);

            viewholder.btn_approve=(Button) convertView.findViewById(R.id.approve_btn);
            viewholder.btn_reject=(Button) convertView.findViewById(R.id.reject_btn);
            viewholder.btn_approve_status_change = (Button) convertView.findViewById(R.id.status_change_btn);


            viewholder.btn_approve2=(Button) convertView.findViewById(R.id.status_charge_two_approve_btn);
            viewholder.btn_reject2=(Button) convertView.findViewById(R.id.status_charge_two_reject_btn);
            viewholder.btn_chnge_status_two=(Button) convertView.findViewById(R.id.status_change_charge_two_btn);

            viewholder.btn_approve.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String time=getTime();

                    viewholder.btn_approve.setVisibility(View.GONE);
                    viewholder.btn_reject.setVisibility(View.GONE);



                    viewholder.btn_approve_status_change.setVisibility(View.VISIBLE);
                    viewholder.btn_approve_status_change.setText("Accepted on "+time);
                    viewholder.btn_approve_status_change.setBackgroundResource(R.drawable.approval_change_status_backg);

                }
            });
            viewholder.btn_reject.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String time=getTime();
                    viewholder.btn_reject.setVisibility(View.GONE);
                    viewholder.btn_approve.setVisibility(View.GONE);
                    viewholder.btn_approve_status_change.setVisibility(View.VISIBLE);
                    viewholder.btn_approve_status_change.setText("Rejected on "+time);
                    viewholder.btn_approve_status_change.setBackgroundResource(R.drawable.approval_reject_chng_status);
                }
            });

            viewholder.btn_approve2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String time=getTime();
                    viewholder.btn_approve2.setVisibility(View.GONE);
                    viewholder.btn_reject2.setVisibility(View.GONE);
                    viewholder.btn_chnge_status_two.setVisibility(View.VISIBLE);
                    viewholder.btn_chnge_status_two.setText("Accepted on "+time);
                    viewholder.btn_chnge_status_two.setBackgroundResource(R.drawable.approval_change_status_backg);
                }
            });
            viewholder.btn_reject2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String time=getTime();
                    viewholder.btn_reject2.setVisibility(View.GONE);
                    viewholder.btn_approve2.setVisibility(View.GONE);
                    viewholder.btn_chnge_status_two.setVisibility(View.VISIBLE);
                    viewholder.btn_chnge_status_two.setText("Rejected on "+time);
                    viewholder.btn_chnge_status_two.setBackgroundResource(R.drawable.approval_reject_chng_status);
                }
            });





            convertView.setTag(viewholder);

        }else{
            viewholder=(Viewholder) convertView.getTag();
        }
        viewholder.truck_name.setText(list.get(position).getTruckName()) ;
        viewholder.lr_no.setText(list.get(position).getLrNmbr()) ;
//        viewholder.btn_approve_status_change.setText(list.get(position));
//
//        for(int i=0;i<list.get(position).getApprovalSubListPogos().size();i++){
//            viewholder.btn_approve_status_change.setText(list.get(position).getApprovalSubListPogos().get(i).getStatusChange()) ;
//            viewholder.extra_height.setText(list.get(position).getApprovalSubListPogos().get(i).getHeight()) ;
//            viewholder.detention.setText(list.get(position).getApprovalSubListPogos().get(i).getHeight()) ;
//
//
//        }




        Config.colorFont(context, null, viewholder.truck_name, null);
        Config.colorFont(context, null, viewholder.lr_no, null);
        Config.colorFont(context, null, viewholder.extra_height, null);
        Config.colorFont(context, null, viewholder.detention, null);

        Config.colorFont(context, null, null ,viewholder.btn_approve);
        Config.colorFont(context, null, null, viewholder.btn_reject);
        Config.colorFont(context, null, null, viewholder.btn_approve_status_change);

        Config.colorFont(context, null, null, viewholder.btn_approve2);
        Config.colorFont(context, null,null, viewholder.btn_reject2);
        Config.colorFont(context, null,null, viewholder.btn_chnge_status_two);



        return convertView;
    }
    @Override
    public boolean areAllItemsEnabled()
    {
        return false;
    }

    @Override
    public boolean isEnabled(int position)
    {
        return false;
    }

    public String getTime(){
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"+5.5));
        Date currentLocalTime = cal.getTime();

        System.out.println(currentLocalTime);

        DateFormat date = new SimpleDateFormat("HH:mm a");

        String localTime = date.format(Calendar.getInstance().getTime());

//        date.setTimeZone(TimeZone.getTimeZone("GMT"));
//        String localTime = date.format(currentLocalTime);


        return localTime;
    }


    public void findDuplicate(){


        for(int i=0;i<list.size();i++){

            for(int j=i+1;j<list.size();j++){

                if(list.get(j).getTruckName().equalsIgnoreCase(list.get(i).getTruckName())){

                }

            }

        }

    }

}

single_row_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:background="@drawable/truck_rounded_view"

    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:background="@drawable/approval_header"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="5dp"

                android:orientation="vertical">
        <TextView
            android:id="@+id/appr_txt_truck"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Truck  MH01BQ4111"
            android:layout_marginTop="@dimen/approval_margin"
            android:layout_marginLeft="@dimen/approval_margin"
            android:textSize="@dimen/approval_header_size"
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:textColor="@color/darkgray"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/appr_lrNo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="LR 788691"
            android:layout_marginTop="@dimen/approval_margin"
            android:layout_marginLeft="@dimen/approval_margin"
            android:textSize="@dimen/approval_header_size"
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:layout_marginBottom="@dimen/approval_rl_top"
            android:textColor="@color/darkgray"
            android:textStyle="bold" />
            </LinearLayout>
            </LinearLayout>
        <!--<View
            android:layout_marginTop="@dimen/approval_margin"
            android:layout_marginLeft="@dimen/approval_margin"
            android:layout_marginRight="@dimen/approval_margin"
            android:layout_width="match_parent"
            android:layout_height="@dimen/approval_divider_height"
            android:background="@color/white" />-->

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_marginTop="@dimen/approval_rl_top"
            android:layout_marginLeft="@dimen/approval_margin"
            android:layout_marginRight="@dimen/approval_margin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/appr_charge_one"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Charge 1"
                android:layout_centerInParent="true"
                android:layout_alignParentLeft="true"
                android:textSize="@dimen/txt_approval_txt"
                android:textColor="@color/white"
                android:textStyle="bold" />

            <Button

                android:id="@+id/approve_btn"
                android:layout_width="@dimen/approval_btn_width"
                android:layout_height="@dimen/approval_btn_height"
                android:background="@drawable/aprroval_aprrove_backg"
                android:textColor="@color/white"
                android:layout_gravity="center"
                android:gravity="center"
                android:paddingLeft="@dimen/approval_padding_left"
                android:clickable="true"
                android:layout_alignParentRight="true"

                android:textSize="@dimen/approval_text_size"
                android:textStyle="bold"
                android:text="APPROVE" />


            <Button
           android:visibility="gone"
                android:id="@+id/status_change_btn"
                android:layout_width="@dimen/approval_status_chnege_width"
                android:layout_alignParentRight="true"
                android:maxLines="2"
                android:layout_height="@dimen/approval_btn_height"
                android:background="@drawable/approval_change_status_backg"
                android:gravity="center"
                android:padding="@dimen/approval_margin"


                android:textSize="@dimen/approval_text_size"
                android:textStyle="bold"
                android:textColor="@color/white"
                android:text="Accepted on 3:00 PM" />




        </RelativeLayout>

        <RelativeLayout
            android:layout_marginTop="@dimen/approval_rl_top"
            android:layout_marginLeft="@dimen/approval_margin"
          android:layout_marginRight="@dimen/approval_margin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <TextView
            android:id="@+id/extra_height"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Extra Height - Rs 200"

            android:layout_centerInParent="true"
            android:layout_alignParentLeft="true"
            android:textSize="@dimen/txt_approval_txt"
            android:layout_gravity="center"
            android:gravity="center"
            android:textColor="@color/white"
            android:textStyle="bold" />

            <Button
                android:id="@+id/reject_btn"
                android:layout_width="@dimen/approval_btn_width"
                android:layout_height="@dimen/approval_btn_height"
                android:text="Reject"
                android:textSize="@dimen/approval_text_size"
                android:paddingLeft="@dimen/approval_padding_left"
                android:gravity="center"
                android:textColor="@color/white"
                android:layout_alignParentRight="true"
                android:background="@drawable/approval_reject_backg"
                android:textStyle="bold"/>




        </RelativeLayout>
        <View
            android:layout_marginTop="@dimen/approval_rl_top"
            android:layout_marginLeft="@dimen/approval_margin"
            android:layout_marginRight="@dimen/approval_margin"
            android:layout_width="match_parent"
            android:layout_height="@dimen/approval_divider_height"
            android:background="@color/white" />

        <RelativeLayout
            android:layout_marginTop="@dimen/approval_rl_top"
            android:layout_marginLeft="@dimen/approval_margin"
            android:layout_marginRight="@dimen/approval_margin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/charge_two"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Charge 2 "
                android:layout_gravity="center"
                android:layout_centerInParent="true"
                android:layout_alignParentLeft="true"
                android:textSize="@dimen/txt_approval_txt"
                android:textColor="@color/white"
                android:textStyle="bold" />

            <Button

                android:id="@+id/status_charge_two_approve_btn"
                android:layout_width="@dimen/approval_btn_width"
                android:layout_height="@dimen/approval_btn_height"
                android:background="@drawable/aprroval_aprrove_backg"
                android:layout_gravity="center"
                android:gravity="center"
                android:textColor="@color/white"
                android:layout_alignParentRight="true"
                android:paddingLeft="@dimen/approval_padding_left"

                android:textSize="@dimen/approval_text_size"
                android:textStyle="bold"
                android:text="APPROVE" />


            <Button
                android:visibility="gone"
                android:id="@+id/status_change_charge_two_btn"
                android:layout_width="@dimen/approval_status_chnege_width"
                android:layout_alignParentRight="true"
                android:maxLines="2"
                android:layout_height="@dimen/approval_btn_height"
                android:background="@drawable/approval_change_status_backg"
                android:textStyle="bold"
                android:textColor="@color/white"
                android:gravity="center"
                android:paddingLeft="@dimen/approval_padding_left"


                android:textSize="@dimen/approval_text_size"

                android:text="Rejected on 3:00 PM" />



        </RelativeLayout>

        <RelativeLayout
            android:layout_marginTop="@dimen/approval_rl_top"
            android:layout_marginLeft="@dimen/approval_margin"
            android:layout_marginRight="@dimen/approval_margin"

            android:layout_marginBottom="@dimen/approval_margin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <TextView
            android:id="@+id/txt_detention"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Detention - Rs 200"
            android:layout_gravity="center"
            android:layout_centerInParent="true"
            android:layout_alignParentLeft="true"
            android:textSize="@dimen/txt_approval_txt"


            android:textColor="@color/white"
            android:textStyle="bold" />

            <Button

                android:id="@+id/status_charge_two_reject_btn"
                android:layout_width="@dimen/approval_btn_width"
                android:layout_height="@dimen/approval_btn_height"
                android:gravity="center"
                android:layout_alignParentRight="true"
                android:text="REJECT"
                android:textColor="@color/white"
                android:background="@drawable/approval_reject_backg"
                android:textStyle="bold"
                android:paddingLeft="@dimen/approval_padding_left"
                />

         </RelativeLayout>
        </LinearLayout>
    </LinearLayout>




</LinearLayout>

2 个答案:

答案 0 :(得分:2)

请使用Expandable ListView以便更好地使用。

for more information and example about Expandable listview

答案 1 :(得分:0)

你不应该把可滚动的东西放到可滚动的东西上,而你通常不应该首先需要。这大部分时间都不会起作用。