2015-11-27 2 views
2

Я работаю над приложением, в котором я использую пользовательское listview, и для своего списка я определил макет строки для настройки.Удалите один макет из списка динамически

В моей макете строк у меня есть два Относительных макета.

Я хочу динамически удалить этот Относительный макет из своего списка, а затем добавить его снова.

Пожалуйста, помогите мне, как я могу это сделать. Я добавил снимок экрана моего макета вы можете увидеть здесь Ниже мое Ее расположение:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:id="@+id/r2_imageslayout"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/r2_persontext" 
    android:text="Per Person" 
    android:textSize="15sp" 
    android:textColor="@color/toolbar_color" 
    android:layout_marginLeft="150sp" 
    android:layout_marginTop="@dimen/size_8" 
    /> 
<TextView 
    android:layout_width="100sp" 
    android:layout_height="wrap_content" 
    android:id="@+id/result2_price" 
    android:text="$568" 
    android:textSize="20sp" 
    android:textStyle="bold" 
    android:background="@color/yellow" 
android:layout_alignParentRight="true" 
    android:layout_marginRight="@dimen/size_10" 
    android:layout_marginTop="@dimen/size_5" 
    android:paddingLeft="@dimen/size_15" 

    /> 
<ImageView 
    android:layout_width="30sp" 
    android:layout_height="20sp" 
    android:contentDescription="@null" 
    android:layout_marginLeft="@dimen/size_10" 
    android:layout_marginTop="@dimen/size_30" 
    android:id="@+id/r2_departairlineimage" 
    android:src="@drawable/dl" 
    /> 
<TextView 
android:layout_width="40sp" 
android:layout_height="20sp" 
android:id="@+id/result2_flightcode" 
android:text="SG250" 
android:textSize="12sp" 
android:layout_marginLeft="10sp" 
android:layout_below="@+id/r2_departairlineimage" 
/> 
<ImageView 
android:layout_width="30sp" 
android:layout_height="20sp" 
    android:id="@+id/r2_depart_image" 
android:layout_toRightOf="@+id/r2_departairlineimage" 
android:layout_marginLeft="@dimen/size_15" 
android:layout_marginTop="35sp" 
android:src="@drawable/depart" 
/> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/r2_departtime" 
    android:text="11:30" 
    android:textColor="@color/toolbar_color" 
    android:textSize="@dimen/size_15" 
    android:layout_toRightOf="@+id/r2_depart_image" 
    android:layout_marginLeft="@dimen/size_5" 
    android:layout_marginTop="36sp" 
    /> 
    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/r2_dep_citycode" 
    android:text="Bos" 
    android:textSize="12sp" 
    android:layout_toRightOf="@+id/r2_departtime" 
    android:layout_marginTop="38sp" 
    android:layout_marginLeft="@dimen/size_3" 
    /> 
<ImageView 
    android:layout_width="80sp" 
    android:layout_height="wrap_content" 
    android:id="@+id/r2_image" 
    android:contentDescription="@null" 
    android:src="@drawable/onestop_line" 
    android:layout_toRightOf="@+id/r2_dep_citycode" 
    android:layout_toEndOf="@+id/r2_dep_citycode" 
    android:layout_marginTop="42sp" 
    android:layout_marginLeft="@dimen/size_10" 
    /> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/r2_1stoptext" 
    android:text="1 stop" 
    android:textSize="12sp" 
    android:layout_toRightOf="@+id/r2_dep_citycode" 
    android:layout_toEndOf="@+id/r2_dep_citycode" 
    android:layout_marginTop="32sp" 
    android:layout_marginLeft="@dimen/size_30" 
    /> 
    <ImageView 
    android:layout_width="10sp " 
    android:layout_height="10sp" 
    android:id="@+id/r2_departclock" 
    android:layout_toRightOf="@+id/r2_dep_citycode" 
    android:layout_toEndOf="@+id/r2_dep_citycode" 
    android:layout_below="@+id/r2_1stoptext" 
    android:layout_marginTop="5sp" 
    android:src="@drawable/time_image" 
    android:layout_marginLeft="@dimen/size_30" 
    /> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="15sp" 
    android:id="@+id/r2_dep_flightduration" 
    android:text="5h 60m" 
    android:textSize="12sp" 
    android:layout_below="@+id/r2_1stoptext" 
    android:layout_marginTop="@dimen/size_1" 
    android:layout_toRightOf="@+id/r2_departclock" 
    /> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/r2_arrivetime" 
    android:text="11:30" 
    android:textColor="@color/toolbar_color" 
    android:textSize="@dimen/size_15" 
    android:layout_toRightOf="@+id/r2_image" 
    android:layout_marginLeft="@dimen/size_5" 
    android:layout_marginTop="36sp" 
    /> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/r2_arrive_citycode" 
    android:hint="Was" 
    android:textSize="12sp" 
    android:layout_toRightOf="@+id/r2_arrivetime" 
    android:layout_marginTop="38sp" 
    android:layout_marginLeft="@dimen/size_3" 
    /> 
<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/result2_returnlayout" > 
<ImageView 
    android:layout_width="30sp" 
    android:layout_height="20sp" 
    android:contentDescription="@null" 
    android:layout_marginLeft="@dimen/size_10" 
    android:layout_marginTop="@dimen/size_80" 
    android:id="@+id/r2_return_airlineimage" 
    android:src="@drawable/dl" 
    /> 
<TextView 
    android:layout_width="40sp" 
    android:layout_height="20sp" 
    android:id="@+id/result2_returnflightcode" 
    android:text="SG250" 
    android:textSize="12sp" 
    android:layout_marginLeft="10sp" 
    android:layout_below="@+id/r2_return_airlineimage" /> 
<ImageView 
    android:layout_width="30sp" 
    android:layout_height="20sp" 
    android:id="@+id/r2_return_image" 
    android:layout_toRightOf="@+id/r2_return_airlineimage" 
    android:layout_marginLeft="@dimen/size_15" 
    android:layout_marginTop="80sp" 
    android:src="@drawable/return_image" /> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/r2_return_deptime" 
    android:text="11:30" 
    android:textColor="@color/toolbar_color" 
    android:textSize="@dimen/size_15" 
    android:layout_toRightOf="@+id/r2_return_image" 
    android:layout_marginLeft="@dimen/size_5" 
    android:layout_marginTop="80sp"/> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/r2_return_depcitycode" 
    android:text="Bos" 
    android:textSize="12sp" 
    android:layout_toRightOf="@+id/r2_return_deptime" 
    android:layout_marginTop="82sp" 
    android:layout_marginLeft="@dimen/size_3"/> 
<ImageView 
    android:layout_width="80sp" 
    android:layout_height="10sp" 
    android:id="@+id/r2_ret_image" 
    android:contentDescription="@null" 
    android:src="@drawable/nonstop" 
    android:layout_toRightOf="@+id/r2_return_depcitycode" 
    android:layout_toEndOf="@+id/r2_return_depcitycode" 
    android:layout_marginTop="85sp" 
    android:layout_marginLeft="@dimen/size_10"/> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/r2_return_nonstoptext" 
    android:text="Non stop" 
    android:textSize="12sp" 
    android:layout_toRightOf="@+id/r2_return_depcitycode" 
    android:layout_toEndOf="@+id/r2_return_depcitycode" 
    android:layout_marginTop="75sp" 
    android:layout_marginLeft="@dimen/size_25" /> 
<ImageView 
    android:layout_width="10sp " 
    android:layout_height="10sp" 
    android:id="@+id/r2_ret_clock" 
    android:layout_toRightOf="@+id/r2_return_depcitycode" 
    android:layout_toEndOf="@+id/r2_return_depcitycode" 
    android:layout_below="@+id/r2_return_nonstoptext" 
    android:layout_marginTop="5sp" 
    android:src="@drawable/time_image" 
    android:layout_marginLeft="@dimen/size_30"/> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="15sp" 
    android:id="@+id/r2_ret_flightduration" 
    android:text="5h 60m" 
    android:textSize="12sp" 
    android:layout_marginTop="@dimen/size_1" 
    android:layout_below="@+id/r2_return_nonstoptext" 
    android:layout_toRightOf="@+id/r2_ret_clock"/> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/r2_ret_arrivetime" 
    android:text="11:30" 
    android:textColor="@color/toolbar_color" 
    android:textSize="@dimen/size_15" 
    android:layout_toRightOf="@+id/r2_ret_image" 
    android:layout_marginLeft="@dimen/size_5" 
    android:layout_marginTop="80sp"/> 
    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/r2_ret_arrivecitycode" 
    android:text="Was" 
    android:textSize="12sp" 
    android:layout_toRightOf="@+id/r2_ret_arrivetime" 
    android:layout_marginTop="82sp" 
    android:layout_marginLeft="@dimen/size_3"/> 
</RelativeLayout> 
</RelativeLayout> 

Это мой адаптер код: Result2Adapter (контекст Context, Список list_row) { // супер (контекст, resourceID, list_row); this.context = context; this.list_row = list_row;

} 
@Override 
public int getCount() { 
    return list_row.size(); 
} 

@Override 
public Custom_Result getItem(int position) { 
    return list_row.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 
private class ViewHolder 
{ 
    ImageView DepAirline_logo, Ret_Airlinelogo; 
    TextView Dep_Airline_code,dep_time,dep_arr_time,dep_stops,dep_duration,Dep_dep_citycode,Dep_arrivecitycode,Ret_dep_citycode,Ret_arrivecitycode,Ret_Airline_code,Ret_time,Ret_arr_time, 
      Ret_stops,Ret_duration,price; 


} 

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

    ViewHolder holder=null; 
    LayoutInflater inflater=(LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
    if(convertView==null){ 

     convertView=inflater.inflate(R.layout.result2_rowlayout,null); 

     holder=new ViewHolder(); 
     holder. DepAirline_logo=(ImageView) convertView.findViewById(R.id.r2_departairlineimage); 
     holder.Dep_Airline_code=(TextView) convertView.findViewById(R.id.result2_flightcode); 
     holder.dep_time=(TextView) convertView.findViewById(R.id.r2_departtime); 
     holder.dep_arr_time=(TextView) convertView.findViewById(R.id.r2_arrivetime); 
     holder.Dep_arrivecitycode=(TextView) convertView.findViewById(R.id.r2_arrive_citycode); 
     holder.dep_stops=(TextView) convertView.findViewById(R.id.r2_1stoptext); 
     holder.dep_duration=(TextView) convertView.findViewById(R.id.r2_dep_flightduration); 
     holder.price=(TextView) convertView.findViewById(R.id.result2_price); 
     holder.Dep_dep_citycode=(TextView) convertView.findViewById(R.id.r2_dep_citycode); 


      holder.Ret_Airlinelogo = (ImageView) convertView.findViewById(R.id.r2_return_airlineimage); 
      holder.Ret_Airline_code = (TextView) convertView.findViewById(R.id.result2_returnflightcode); 
      holder.Ret_time = (TextView) convertView.findViewById(R.id.r2_return_deptime); 
      holder.Ret_arr_time = (TextView) convertView.findViewById(R.id.r2_ret_arrivetime); 
      holder.Ret_arrivecitycode = (TextView) convertView.findViewById(R.id.r2_ret_arrivecitycode); 
      holder.Ret_dep_citycode = (TextView) convertView.findViewById(R.id.r2_return_depcitycode); 
      holder.Ret_stops = (TextView) convertView.findViewById(R.id.r2_return_nonstoptext); 
      holder.Ret_duration = (TextView) convertView.findViewById(R.id.r2_ret_flightduration); 

     convertView.setTag(holder); 
    }else { 
     holder = (ViewHolder) convertView.getTag(); 

    } 
    String retimagename=list_row.get(position).getDep_Airline_code().toLowerCase(); 
    String retpath="drawable/"+retimagename; 
    int retimageresource=context.getResources().getIdentifier(retpath, null, context.getPackageName()); 


    String imagename=list_row.get(position).getRet_Airline_code().toLowerCase(); 
    String path="drawable/"+imagename; 
    int imageresource=context.getResources().getIdentifier(path, null, context.getPackageName()); 

    if(imageresource==0) 
    { 
     imageresource=R.drawable.flight_icon; 
    } 
    if(retimageresource==0) 
    { 
     imageresource=R.drawable.flight_icon; 
    } 
    Drawable image=context.getResources().getDrawable(imageresource); 
    Drawable retimage=context.getResources().getDrawable(imageresource); 

    holder.DepAirline_logo.setImageDrawable(image); 
    holder.Ret_Airlinelogo.setImageDrawable(retimage); 
    holder.Dep_Airline_code.setText(list_row.get(position).getDep_Airline_name()); 
    holder.Ret_Airline_code.setText(list_row.get(position).getRet_Airline_name()); 
    holder.dep_time.setText(list_row.get(position).getDepart_time()); 
    holder.Ret_time.setText(list_row.get(position).getRet_dep_time()); 
    holder.dep_arr_time.setText(list_row.get(position).getArrive_time()); 
    holder.Ret_arr_time.setText(list_row.get(position).getRet_arr_time()); 
    holder.dep_stops.setText(list_row.get(position).getDep_stops()); 
    holder.Ret_stops.setText(list_row.get(position).getRet_stops()); 
    holder.dep_duration.setText(list_row.get(position).getDep_duration()); 
    holder.Ret_duration.setText(list_row.get(position).getRet_duration()); 
    holder.Dep_arrivecitycode.setText(list_row.get(position).getDep_CityCode()); 
    holder.Ret_arrivecitycode.setText(list_row.get(position).getArr_citycode()); 
    holder.Ret_dep_citycode.setText(list_row.get(position).getRet_dep_citycode()); 
    holder.Ret_arrivecitycode.setText(list_row.get(position).getRet_arr_citycoode()); 
    holder.price.setText(list_row.get(position).getPrice()); 


    return convertView; 
} 
+0

Можем ли мы увидеть код макета в вопросе и адаптера пожалуйста ? –

+0

Можете ли вы опубликовать снимок экрана и то, что вы пробовали до сих пор. –

+0

Извините, я попытался опубликовать снимок экрана, но у меня низкая репутация на SO. Поэтому его не позволяя мне это сделать –

ответ

0

Вы можете сделать это в методе getView адаптера класса. Для определенной строки, раздувая представление и получая относительную компоновку с помощью findViewById().

например,

RelativeLayout relativeLayout1 = (RelativeLayout) view.findViewById(R.id.relativeLayoutId); 

Вы можете скрыть макет с помощью

relativeLayout1.setVisibility(View.GONE); 

и Unhide использованием

relativeLayout1.setVisibility(View.VISIBLE); 
+0

Хорошо, как будто я хочу скрыть свой макет 1, если (ring == 2) {скрыть макет 1} else {показать его}. Является ли это утверждением –

+0

Да, вы можете это сделать. – aiRbornE

+0

Спасибо, что помогло мне и спаси мой день –

Смежные вопросы