ListView,减少 ListView 中一个元素与另一个元素之间的空间

Posted

技术标签:

【中文标题】ListView,减少 ListView 中一个元素与另一个元素之间的空间【英文标题】:ListView, Reduce the space between one element and the other in a ListView 【发布时间】:2015-12-02 10:51:16 【问题描述】:

我有一个从 XML 文件中选择数据的应用程序,通过 ListView 显示它们。但是列表中的一个 elemrny 和另一个之间有很多空间。这是我的课

package com.example.giacomob.myapplication;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Giacomo B on 05/08/2015.
 */
public class Page1 extends Activity 
   // private ArrayList<String> data = new ArrayList<String>();
    final int n_campi = 8; //definisco la costante del numero di campi per fermata
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page1);

        String dato1 = getIntent().getExtras().getString("NomeDati1"); //preleva la stringa
        Toast.makeText(this, dato1, Toast.LENGTH_SHORT).show();  //PROVA: questo mi fa comparire una specie di label notifica trasparente con il valore di "data1"
        Log.d("TAG", "data1:" + dato1); //credo sia una specie di debug

        // seconda stringa
        String destination_nf =  getIntent().getExtras().getString("Destinazione_scelta");
        String destination_f = destination_nf.trim(); //tolgo gli spazi a inzio e fine
        String destination = destination_f.substring(0, 1).toUpperCase() + destination_f.substring(1,destination_f.length()).toLowerCase(); //Faccio in modo che l'utente mi passi una stringa con prima maiuscola e le altre minuscole
        Toast.makeText(this, destination, Toast.LENGTH_SHORT).show();



        //    String salve =  getIntent().getExtras().getString("ciao");

        int c=0, j=0, n=0;
        boolean verify = false;

        //FARE CONTROLLO IN CASO IL FILE XML E' VUOTO E QUINDI LA STRINGA E' VUOTA
        String[] arr = dato1.split("\\|");
        for (int i = 0; i < arr.length; i++) 
            System.out.println(i + " => " + arr[i]);
        
        final ArrayList<String> listp = new ArrayList<String>();
        for (int i=3; i<arr.length-1; i=i+n_campi) 

            if(arr[i].equals(destination)) 
                System.out.println(i + " des " + arr[i]);
                for(; j<n_campi+c; j++)
                    if(j==0+c) 
                        n++;
                        listp.add("Informazioni fermata numero " +n);
                        listp.add("Coordinata X: " + arr[j]);
                    
                    else if(j==1+c)
                        listp.add("Coordinata Y: " + arr[j]);
                    else if (j==2+c)
                        listp.add("Nazione di destinazione: " + arr[j]);
                    else if (j==3+c)
                        listp.add("Paese di destinazione: " + arr[j]);
                    else if (j==4+c)
                        listp.add("Via di destinazione: " + arr[j]);
                    else if (j==5+c)
                        listp.add("Id Corriera: " + arr[j]);
                    else if (j==6+c)
                        listp.add("Paese Fermata: " + arr[j]);
                    else if (j==7+c) 
                        listp.add("Via fermata: " + arr[j]);
                        listp.add("   ");
                    

                    verify = true;

            
            else
            
                j=j+n_campi;
            

            c = j;
        
        if(verify == false)
            Toast.makeText(this,"Non sono stati trovati mezzi per " + destination, Toast.LENGTH_SHORT).show();



        final ListView mylist = (ListView) findViewById(R.id.listView1);



        mylist.setAdapter(new MyListAdaper(this, R.layout.activity_page1, listp));


        Button b_load = (Button) findViewById(R.id.button_send2);
        b_load.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View arg0) 

                Intent openTrack = new Intent(Page1.this, Track.class);

                //String ciao = "ahahah";
                //String data1 = ReadXMLFile.readXMLFile(Page1.this);
                openTrack.putExtra("Destination", getIntent().getExtras().getString("NomeDati1"));
                startActivity(openTrack);

            
        );
        System.out.println("ciao"+listp.size());
    

    private class MyListAdaper extends ArrayAdapter<String> 
        private int layout;



        public MyListAdaper(Context context, int resource, List<String> objects) 
            super(context, resource, objects);
            layout = resource;
        
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) 

                ViewHolder mainViewHolder = null;
                System.out.println("position " + position); //IS THE REAL POSITION
                //   if(convertView == null )
                LayoutInflater inflater = LayoutInflater.from(getContext());
                convertView = inflater.inflate(layout, parent, false);
                ViewHolder viewHolder = new ViewHolder();
                // viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.list_item_thumbnail)
                viewHolder.title = (TextView) convertView.findViewById(R.id.textView2);


                viewHolder.button = (Button) convertView.findViewById(R.id.button_send2);



                if (position != 0 && position % 9 == 0) 
                    viewHolder.button.setVisibility(View.VISIBLE);
                    //  viewHolder.title.setVisibility(View.VISIBLE);
                 else 
                    viewHolder.button.setVisibility(View.GONE);
                    //viewHolder.title.setVisibility(View.GONE);
                
                System.out.println("positionMiddle " + position); //APPEARS ONLY 2 TIMES WITH 0 AND 1
                viewHolder.button.setOnClickListener(new View.OnClickListener() 
                    @Override
                    public void onClick(View v) 
                        System.out.println("position2 " + position); // IS ONLY 0 OR 1
                        Toast.makeText(getContext(), "Bottone cliccato" + position, Toast.LENGTH_SHORT).show();

                    

                );
                convertView.setTag(viewHolder);
                //   
                // else 
                mainViewHolder = (ViewHolder) convertView.getTag();
                mainViewHolder.title.setText(getItem(position));
                //
          //  
            return convertView;

        


    
    public class ViewHolder 
        //ImageView  thumbnail; //forse non serve
        TextView title;
        Button button;
    
    

这是我的活动:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_
    android:layout_
    android:weightSum="1">


    <TextView
    android:layout_
    android:layout_
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/string_destination"
    android:id="@+id/textView2"
    android:layout_gravity="center_horizontal"
    />




    <ListView
        android:id="@+id/listView1"
        android:layout_
        android:layout_
        android:layout_below="@+id/textList"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp" />


    <Button
        android:layout_
        android:layout_
        android:text="@string/string_loadfermata"
        android:id="@+id/button_send2"
        android:layout_gravity="center_horizontal"
        android:onClick="sendMessage"/>
</LinearLayout>

这是我的问题的一个例证: http://i.imgur.com/32sGcxx.jpg

【问题讨论】:

【参考方案1】:

在您的活动布局文件中,您已将 ListView 的“layout_height”属性设置为“359dp”,这就是图 1 和 3 中出现问题的原因。要解决此问题,请将“layout_height”更改为“wrap_content” '。

对于图 2,我猜你可能犯了同样的错误。可以显示列表项的布局文件吗?

【讨论】:

【参考方案2】:

您应该使用RelativeLayout 来替换LinearLayout,然后为ListView 设置minHeight 和maxHeight 以适应您的屏幕。使用按钮,您添加 id 为 ListView 的 id 的 display_below 字段。

【讨论】:

【参考方案3】:

您忘记附加行布局。您能否附上列表视图行布局,可能是该文件中的问题。

【讨论】:

以上是关于ListView,减少 ListView 中一个元素与另一个元素之间的空间的主要内容,如果未能解决你的问题,请参考以下文章

Android ListView多布局讲解

Android ListView多布局讲解

ListView 的优化

如何在 ListView 中设置 Counter 应用

listview加载性能优化ViewHolder

Django将自定义查询集添加到ListView中