2

我在我的應用中出現了一個奇怪的問題。在我的一個應用程序活動中,我使用製表符將三個片段放入其中。一切正常。 在我遇到問題的片段中,我有使用適配器和來自Web服務的數據充氣的listview。這也很好。現在的問題是這個在適配器中膨脹的行,有一個隱藏的視圖,它在xml中有visibility=gone。點擊該行的imageview,我可以通過Java代碼使該佈局可見。問題是佈局不能在點擊時顯示。 我甚至在imageview的onClickListener上設置了斷點,它確實執行了將可見性從變爲可見的行。我無法理解是什麼導致了這個問題,因爲我在其他屏幕上使用相同的行xml和相同的數據,並在那裏工作完美。當listview row item有隱藏的視圖時,片段不尊重匹配父級高度

UPDATE

我知道是什麼導致了這個問題,但不知道如何解決這個問題。在我的活動中,我有三個片段。我提供給片段的視圖(在哪個片段中將被誇大)正在引發主要問題。我已設置高度寬度以匹配父項,但未採用匹配父項高度。如果片段只包含像textview這樣的普通視圖,那麼imageview也會正確顯示片段。但問題是片段是否包含listview,那麼它只取得提供給listview的自定義行的高度。我可以在該空間中滾動完整的listview。 我不明白是什麼導致了這種行爲。 我更新的代碼。

主要佈局XML

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <include layout="@layout/header_1" /> 



<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:background="#000" 
    android:weightSum="3" 
    android:orientation="horizontal"> 
    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:weightSum="1" 
     android:orientation="horizontal" 
     android:id="@+id/lin_birds"> 
     <TextView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:text="@string/aves" 
      android:textColor="#ffffff" 
      android:gravity="center" 
      android:layout_gravity="center" 
      android:layout_weight="0.99" 
      android:id="@+id/fragment_aves"/> 
     <View 
      android:layout_width="0dp" 
      android:layout_height="30dp" 
      android:layout_weight="0.01" 
      android:background="#ffffff" 
      android:layout_marginTop="10dp"/> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:weightSum="1" 
     android:orientation="horizontal"> 
     <TextView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:text="@string/routes" 
      android:textColor="#ffffff" 
      android:gravity="center" 
      android:layout_gravity="center" 
      android:layout_weight="0.99" 
      android:id="@+id/fragment_routes"/> 
     <View 
      android:layout_width="0dp" 
      android:layout_height="30dp" 
      android:layout_weight="0.01" 
      android:background="#ffffff" 
      android:layout_marginTop="10dp"/> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="horizontal"> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="@string/information" 
      android:textColor="#ffffff" 
      android:gravity="center" 
      android:layout_gravity="center" 
      android:id="@+id/fragment_information"/> 

    </LinearLayout> 
</LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 
    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/frame_details" /> 

    </LinearLayout> 
</LinearLayout> 

主要活動的Java代碼

public class ActivityRoutesDetails extends AppCompatActivity { 
    RelativeLayout rel_back; 
    TextView tv_title,tv_information,tv_routes,fragment_aves; 
    RoutesDataBean routesDataBean; 
    LinearLayout frame; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.routes_detail); 
     tv_title= (TextView)findViewById(R.id.tv_title); 
     tv_information= (TextView) findViewById(R.id.fragment_information); 
     tv_routes= (TextView) findViewById(R.id.fragment_routes); 
     fragment_aves= (TextView) findViewById(R.id.fragment_aves); 
//  frame= (LinearLayout) findViewById(R.id.frame_details); 
     routesDataBean= (RoutesDataBean)getIntent().getSerializableExtra("data"); 
     tv_title.setText(routesDataBean.getDescrip1()); 

     Fragment fragment=new FragmentRouteInside(); 
     FragmentManager fragmentManager = getSupportFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     fragmentTransaction.add(R.id.frame_details, fragment); 
     fragmentTransaction.commit(); 
     fragment_aves.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Fragment fragmentBirds=new FragmentRouteBirds(); 
       FragmentManager fragmentManager = getSupportFragmentManager(); 
       FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
       fragmentTransaction.replace(R.id.frame_details, fragmentBirds); 
       fragmentTransaction.commit(); 
      } 
     }); 

     tv_information.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Fragment fragmentRouteInformation = new FragmentRouteInformation(); 
       FragmentManager fragmentManager = getSupportFragmentManager(); 
       FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
       fragmentTransaction.replace(R.id.frame_details, fragmentRouteInformation); 
       fragmentTransaction.commit(); 
      } 
     }); 
     tv_routes.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Fragment fragment=new FragmentRouteInside(); 
       FragmentManager fragmentManager = getSupportFragmentManager(); 
       FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
       fragmentTransaction.replace(R.id.frame_details, fragment); 
       fragmentTransaction.commit(); 
      } 
     }); 
     rel_back= (RelativeLayout) findViewById(R.id.rel_back); 

     rel_back.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       finish(); 
      } 
     }); 
    } 
} 

片段鳥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="match_parent"> 
<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/lv_bird"/> 
    </LinearLayout> 

片段java代碼

public class FragmentRouteBirds extends Fragment { 
AppSharedPreferences appSharedPreferences; 
String REGISTER_URL=""; 
ListView lv_birds; 
private ArrayList<BirdsDataBean> birdsUrlList; 
boolean flag=false; 
@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragments_birds, null); 
    appSharedPreferences=AppSharedPreferences.getsharedprefInstance(getActivity()); 
    REGISTER_URL = "http://192.241.162.63/appvist/v1/routebird/"+appSharedPreferences.getRouteId(); 
    birdsUrlList = new ArrayList<>(); 
    lv_birds = (ListView) root.findViewById(R.id.lv_bird); 
    LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) lv_birds.getLayoutParams(); 
    lp.height = LinearLayout.LayoutParams.MATCH_PARENT; 
    lv_birds.setLayoutParams(lp); 
    hitBirdsService(); 
    return root; 
} 
private void hitBirdsService() { 
    class RegisterUser extends AsyncTask<String, Void, String> { 
     private ProgressDialog mDialog; 
     RequestClass ruc = new RequestClass(); 
     String response = ""; 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      mDialog = new ProgressDialog(getActivity()); 
      mDialog.setMessage("Please Wait ..."); 
      mDialog.setCancelable(false); 
      mDialog.show(); 
     } 

     @Override 
     protected void onPostExecute(String s) { 
      super.onPostExecute(s); 
      mDialog.dismiss(); 
      parseBirdResponse(response); 
      //Toast.makeText(getActivity(), s, Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     protected String doInBackground(String[] params) { 

      response = RequestClass.GET(REGISTER_URL); 

      return response; 
     } 
    } 

    RegisterUser ru = new RegisterUser(); 
    ru.execute(); 
} 
public void parseBirdResponse(String response) { 
    //String descrip, String observaciones, String descrip_larga, String url_video, String url 
    try { 
     JSONObject jsonObject = new JSONObject(response); 
     Boolean error = jsonObject.getBoolean("error"); 
     if (!error) { 
      JSONArray jsonArray = jsonObject.getJSONArray("birds"); 
      for (int i = 0; i < jsonArray.length(); i++) { 
       JSONObject jsonBirds = jsonArray.getJSONObject(i); 
       int idave=jsonBirds.getInt("idave"); 
       String descrip = jsonBirds.getString("descrip"); 
       String observaciones = jsonBirds.getString("observaciones"); 
       String descrip_larga = jsonBirds.getString("descrip_larga"); 
       String url_video = jsonBirds.getString("url_video"); 
       String url = jsonBirds.getString("url"); 
       String nombre_cientifico = jsonBirds.getString("nombre_cientifico"); 
       int flag=jsonBirds.getInt("flag"); 
       birdsUrlList.add(new BirdsDataBean(flag,idave,descrip, observaciones, descrip_larga, url_video, url, nombre_cientifico)); 

      } 
      ScheduleTaskAdapter scheduleTaskAdapter = new ScheduleTaskAdapter(getActivity(), birdsUrlList); 
      lv_birds.setAdapter(scheduleTaskAdapter); 
      LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) lv_birds.getLayoutParams(); 
      lp.height = 800; 
      lv_birds.setLayoutParams(lp); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 
public class ScheduleTaskAdapter extends BaseAdapter { 
    Context context; 
    LayoutInflater layoutInflater; 
    // List<InterestAndLanguageBean> interestAndLanguageBeans=new ArrayList<>(); 
    List<BirdsDataBean> imageList = new ArrayList<>(); 

    public ScheduleTaskAdapter(Context context, List<BirdsDataBean> imagesList) { 
     this.context = context; 
     this.imageList = imagesList; 
     layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

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

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 
     if (convertView == null) { 
      holder = new ViewHolder(); 
      convertView = layoutInflater.inflate(R.layout.routes_bird_row, parent, false); 
      holder.iv_birds = (ImageView) convertView.findViewById(R.id.iv_route_bird); 
      holder.frameLayout = (FrameLayout) convertView.findViewById(R.id.frame_route_bird); 
      holder.linearLayout = (LinearLayout) convertView.findViewById(R.id.route_bird_detail_view); 
      holder.imageView = (ImageView) convertView.findViewById(R.id.iv_hide); 
      holder.iv_video = (ImageView) convertView.findViewById(R.id.iv_seen); 
      holder.iv_sound = (ImageView) convertView.findViewById(R.id.iv_video); 
      holder.tv_short_descript = (TextView) convertView.findViewById(R.id.tv_bird_name); 
      holder.tv_category = (TextView) convertView.findViewById(R.id.tv_scientific_name); 
      holder.tv_long_description = (TextView) convertView.findViewById(R.id.tv_description); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     Uri myUri = Uri.parse(birdsUrlList.get(position).getUrl()); 
     Glide.with(getActivity()).load(myUri).placeholder(R.drawable.birds).into(holder.iv_birds); 
     holder.tv_short_descript.setText(birdsUrlList.get(position).getDescrip()); 
     holder.tv_long_description.setText(birdsUrlList.get(position).getDescrip_larga()); 
     holder.tv_category.setText(birdsUrlList.get(position).getNombre_cientifico()); 
     final ViewHolder finalHolder = holder; 
     holder.frameLayout.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       finalHolder.linearLayout.setVisibility(View.VISIBLE); 
       finalHolder.iv_sound.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         try { 
          startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(birdsUrlList.get(position).getUrl_video()))); 
         } catch (ActivityNotFoundException e) { 
          e.printStackTrace(); 
         } 
        } 
       }); 
       finalHolder.iv_video.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         //hitBirdSeenService(birdsUrlList.get(position).getIdave()); 
         // finalHolder.iv_video.setImageResource(R.drawable.eye_selected); 
        } 
       }); 
      } 
     }); 
     holder.imageView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       finalHolder.linearLayout.setVisibility(View.GONE); 
      } 
     }); 
     //Picasso.with(context).load(myUri).placeholder(R.drawable.image).into(holder.pic); 
     //malevich.load(helperTaskBeanList.get(position).getImage()).into(holder.pic); 
     return convertView; 
    } 

} 
static class ViewHolder { 
    ImageView iv_birds,imageView,iv_video,iv_sound; 
    FrameLayout frameLayout; 
    LinearLayout linearLayout; 
    TextView tv_short_descript,tv_category,tv_long_description; 
} 

}

和行佈局適配器

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:id="@+id/frame_route_bird" 
     xmlns:android="http://schemas.android.com/apk/res/android"> 
     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scaleType="fitXY" 
      android:id="@+id/iv_route_bird"/> 
     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:layout_gravity="bottom" 
      android:layout_marginBottom="5dp" 
      android:layout_marginLeft="10dp" > 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textColor="#ffffff" 
       android:text="@string/name" 
       android:textSize="24sp" 
       android:textStyle="bold" 
       android:id="@+id/tv_bird_name"/> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="15sp" 
       android:textColor="#ffffff" 
       android:text="@string/bird_sub_category" 
       android:id="@+id/tv_scientific_name"/> 

     </LinearLayout> 
    </FrameLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:visibility="gone" 
     android:background="#ffffff" 
     android:id="@+id/route_bird_detail_view" 
     android:paddingBottom="120dp"> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/bird_hidden_text" 
      android:textSize="20sp" 
      android:padding="20dp" 
      android:id="@+id/tv_description"/> 
     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_gravity="center"> 
      <ImageView 
       android:layout_width="50dp" 
       android:layout_height="50dp" 
       android:src="@drawable/eye110" 
       android:id="@+id/iv_seen"/> 
      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/right" 
       android:layout_marginTop="25dp" 
       android:layout_marginLeft="15dp" 
       android:id="@+id/iv_arrow"/> 
      <ImageView 
       android:layout_width="50dp" 
       android:layout_height="50dp" 
       android:src="@drawable/ear" 
       android:layout_marginLeft="15dp" 
       android:id="@+id/iv_video"/> 
     </LinearLayout> 
     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="20dp" 
      android:background="#D6D6D6" 
      android:layout_marginTop="20dp" 
      android:src="@drawable/up" 
      android:layout_gravity="center" 
      android:id="@+id/iv_hide"/> 
    </LinearLayout> 
</LinearLayout> 

這裏有問題This is the screen when fragment opens

This is when I make hidden view visible

兩個圖像進行比較的截圖,你可以看到隱藏的視圖出現在單行項目的空間並且可以在該空間中完全滾動。

回答

1

更好的結果進行了針對加爲Android開發者(克里斯托Beils):

「你的ListView必須有它的高度設置爲match_parent或者如果您需要添加其他固定的大小,而不是WRAP_CONTENT

。視圖固定在ListView的底部作爲頁腳,您需要將ListView和頁腳視圖都放置在一個垂直的LinearLayout中,並將List_layout的layout_weight設置爲0dp,然後在ListView上將layout_weight設置爲1,這樣就可以獲得剩餘的垂直空間。

+0

謝謝你,你救了我。你的答案的下一部分做到了。我在線性佈局中添加了listview,並給它權重= 1,然後給listview權重= 1,它現在正在工作。非常感謝。 –

0

在列表視圖中嘗試fillViewPort = true ..但不知道爲什麼yoi在列表視圖中有滾動視圖..也可能是問題。

+0

我的一切測試,可以工作 –

+0

fillView端口的工作,但何時展開的列表項的身高是大於屏幕尺寸時,剩下的部分被切斷,未顯示 –

+0

滾動視圖沒有問題,我已經有兩個測試添加/刪除它沒有區別 –

0

嘗試設置match_parent作爲列表視圖的高度 我懷疑這會強制列表視圖具有定義的高度並修復您的問題。

+0

它沒有任何區別 –

相關問題