Listview very long

Asked

Viewed 922 times

5

I created a ListView that features the songs of my sdcard, but as you can see in the image below it is very long regardless of the amount of items, I want its height to be smaller to display more names, so that the list is not too long, and also is very ugly that way.

inserir a descrição da imagem aqui

Screen xml code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/wallpaper"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Musica" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/btnVoltar"
    android:layout_toRightOf="@+id/btnVoltar"
    android:text="Voltar"
    android:textColor="#ffffffff"
    android:textSize="20sp"
    android:textStyle="bold"
    android:typeface="serif" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:src="@drawable/logo" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_toLeftOf="@+id/imageView1"
    android:text="Takion"
    android:textColor="#32CD32"
    android:textSize="20sp"
    android:textStyle="bold"
    android:typeface="serif" />

<ListView
    android:id="@+id/lvPlaylist"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true" >

</ListView>

<ImageButton
    android:id="@+id/btnVoltar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView2"
    android:layout_alignLeft="@+id/lvPlaylist"
    android:background="#00000000"
    android:src="@drawable/back" />

Player class (screen with play buttons, next, ect, seekBar...):

public class Player extends Activity implements View.OnClickListener{

SeekBar music=null;   
AudioManager mgr=null;

static MediaPlayer mp;
ArrayList<File> mySongs;
int position;
Uri u;

Thread updateSeekBar;

ImageButton btnVoltar, btnBackward, btnPrevious, btnNext, btnForward;

private ImageButton btnPlayPause;
private boolean isbtnPlayer = false;
SeekBar progressMusic;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_player);

    mgr=(AudioManager)getSystemService(Context.AUDIO_SERVICE);

    music=(SeekBar)findViewById(R.id.sbMusic);

    initBar(music, AudioManager.STREAM_MUSIC);

    btnVoltar = (ImageButton) findViewById(R.id.btnVoltar);
    btnBackward = (ImageButton) findViewById(R.id.btnBackward);
    btnPrevious = (ImageButton) findViewById(R.id.btnPrevious);
    btnPlayPause = (ImageButton) findViewById(R.id.btnPlayPause);
    btnNext = (ImageButton) findViewById(R.id.btnNext);
    btnForward = (ImageButton) findViewById(R.id.btnForward);       

    btnBackward.setOnClickListener(this);
    btnPrevious.setOnClickListener(this);
    btnPlayPause.setOnClickListener(this);
    btnNext.setOnClickListener(this);
    btnForward.setOnClickListener(this);

    progressMusic = (SeekBar) findViewById(R.id.progressMusic);
    updateSeekBar = new Thread(){
        @Override
        public void run() {
            int totalDuration = mp.getDuration();
            int currentPosition = 0;
            while (currentPosition < totalDuration){
                try {
                    sleep(500);
                    currentPosition = mp.getCurrentPosition();
                    progressMusic.setProgress(currentPosition);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    };

    if(mp!=null){
        mp.stop();
        mp.release();

    }

    Intent i = getIntent();
    Bundle b = i.getExtras();
    mySongs = (ArrayList) b.getParcelableArrayList("songlist");
    position = b.getInt("pos", 0);

    u = Uri.parse(mySongs.get(position).toString());
    mp = MediaPlayer.create(getApplicationContext(), u);
    mp.start();

    progressMusic.setMax(mp.getDuration());

    updateSeekBar.start();

    progressMusic.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            mp.seekTo(seekBar.getProgress());
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        }
    });

    btnVoltar.setOnClickListener(new View.OnClickListener() {       

        @Override
        public void onClick(View v) {

            Intent home = new Intent(Player.this, Musica.class);
            Player.this.startActivity(home);
            Player.this.finish();

        }
    });
}

 private void initBar(SeekBar bar, final int stream) {
        bar.setMax(mgr.getStreamMaxVolume(stream));
        bar.setProgress(mgr.getStreamVolume(stream));

        bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
          public void onProgressChanged(SeekBar bar, int progress,
                                        boolean fromUser) {
            mgr.setStreamVolume(stream, progress,
                                AudioManager.FLAG_PLAY_SOUND);
          }

          public void onStartTrackingTouch(SeekBar bar) {

          }

          public void onStopTrackingTouch(SeekBar bar) {

          }
        });
      }

@Override
public void onClick(View v) {
    int id = v.getId();
    switch (id){
        case R.id.btnPlayPause:
            if(mp.isPlaying()){                 
                isbtnPlayer = true;
                btnPlayPause.setImageResource(R.drawable.player);
                mp.pause();
            }else{
                isbtnPlayer = true;
                btnPlayPause.setImageResource(R.drawable.pause);
                mp.start();
            }break;

        case R.id.btnForward:
            mp.seekTo(mp.getCurrentPosition()+5000);
            break;

        case R.id.btnBackward:
            mp.seekTo(mp.getCurrentPosition()-5000);
            break;

        case R.id.btnNext:
            mp.stop();
            mp.release();
            position = (position+1) %mySongs.size();
            u = Uri.parse(mySongs.get(position).toString());
            mp = MediaPlayer.create(getApplicationContext(), u);
            mp.start();
            progressMusic.setMax(mp.getDuration());
            break;

        case R.id.btnPrevious:
            mp.stop();
            mp.release();
            position = (position-1 < 0)? mySongs.size()-1: position-1;
            u = Uri.parse(mySongs.get(position).toString());
            mp = MediaPlayer.create(getApplicationContext(), u);
            mp.start();
            progressMusic.setMax(mp.getDuration());
            break;              
    }

}

Music Class (Presents the listview with the music):

public class Musica extends Activity {

ListView lv;

String[] items;

ImageButton btnVoltar;

ImageButton btnBack;    

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.musica);        

    btnVoltar = (ImageButton) findViewById(R.id.btnVoltar);
    btnVoltar.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent home = new Intent(Musica.this, Home.class);
            Musica.this.startActivity(home);
            Musica.this.finish();

        }
    });

    lv = (ListView) findViewById(R.id.lvPlaylist);

    final ArrayList<File> mySongs = findSongs(Environment.getExternalStorageDirectory());

    items = new String[ mySongs.size() ];

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

        //toast(mySongs.get(i).getName().toString());
        items[i] = mySongs.get(i).getName().toString().replace(".mp3", "").replace(".wav", "");

    }

    ArrayAdapter<String> adp = new ArrayAdapter<String>(getApplicationContext(), R.layout.player, R.id.tvPlayer, items);
    lv.setAdapter(adp);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id){
            startActivity(new Intent(getApplicationContext(), Player.class).putExtra("pos", position).putExtra("songlist", mySongs));
        }

    });

}

public ArrayList<File> findSongs(File root){
    ArrayList<File> al = new ArrayList<File>();
    File[] files = root.listFiles();
    for(File singleFile : files){
        if(singleFile.isDirectory() && !singleFile.isHidden()){
            al.addAll(findSongs(singleFile));               
        }else{
            if(singleFile.getName().endsWith(".mp3") || singleFile.getName().endsWith(".wav")){
                al.add(singleFile);
            }               
        }           
    }       
    return al;
}   

public void toast(String text){
    Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();

}

Relativelayout:

<TextView
    android:id="@+id/tvPlayer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#ffffffff"
    android:textStyle="bold"
    android:typeface="sans" />

  • Could you enter the code of your list items? The XML you made to use in getView() of the Adapter of this listview.

  • @Gustavobitencourt ready posted all the codes.

  • Sorry, your code is confused. Try using android:layout_width="wrap_content" in Listview.

  • More do you know any method to decrease listview? Do not need to understand the code just want an example of how to do this.

  • unfortunately does not work only by changing the width, I tried not to.

6 answers

1

Search for the use of Custom Lists on Android using Adapters. Try to work with the implementation this way because it allows you to define the Layout of each line that will be presented.

There is a lot of material on the Internet showing how to implement it. I suggest that you conduct a search for a better understanding.

Another tip I can offer you is the implementation of Recyclerview instead of the traditional Listview. It is an "evolution" with performance improvement for your application.

Here is an example showing how the Listview application is: Customizing a Listview on Android

The content is the staff of Alura which is an online teaching platform that I really liked and recommend their material: Site Alura

In this Link there is an example to implement Recyclerview and it was written by Rafael de Araújo (very well written by the way): Lists with Recyclerview

Here is my contribution to its implementation. I believe that by following them and observing the other answers of colleagues you will achieve a good implementation with an excellent performance in your App.

0

You can create an xml layout for the Listview line.

row_layout.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"
    android:orientation="horizontal"
    android:padding="10dp" >

    <TextView
        android:id="@+id/txtName"
        android:layout_width="match_parent"
        android:layout_height="46dp"/>

</LinearLayout>

And create your Arrayadapter extending from ArrayAdapter

public class myArrayAdapter extends ArrayAdapter<SUA_CLASSE>{

    Context context;


    public myArrayAdapter (Context ctx) {
        super(ctx, R.layout.row_layout);
        this.context = ctx;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.row_layout, parent, false);

        SUA_CLASSE p = this.getItem(position);

        txtName = TextView rowView.findViewById(R.id.txtName);
        txtName.setText(SUA_CLASSE.toString());

        return rowView;
    }


    @Override
    public void add(SUA_CLASSE object) {
        super.add(object);
    }

    @Override
    public SUA_CLASSE getItem(int position) {
        // TODO Auto-generated method stub
        return super.getItem(position);
    }

}

0

The problem is probably in your "player" layout file, I could not fully understand its explanation but I believe that your file has a Relativelayout with a Textview, you should put the property "layout_height" of the two as "wrap_content" this way will solve the problem.

0

The factor is not only decrease the listview, but put the size you want. We know that match_parent, causes it to extend from the size of the parent component, and wrap_content, makes the component to the size that satisfies the data contained in it. So, by analyzing your code, I would try to tamper with two three things:

  1. Swap match_parent for android:layout_width="wrap_content" on listview component.

  2. Analyze the operation of the section: getWindow(). setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

  3. Analyze the Listview parent element, which in this case is the Relativelayout.

I hope I’ve helped!

Good luck!

  • I already switched the layout_width="wrap_content", removed the getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); and it didn’t work and the Father element I added in the question, if I can take a look more I don’t see anything that can extend the listview like this.

0

The problem could be here

android:layout_height="wrap_content" listview

This snippet of code can cause your getView from Adapter to be called endlessly and also occur the problem that happens to you, the layout of Listview must have height set in match_parent if you don’t want to hide the entire screen with match_parent, think about using weighsum and layout_weight to work with "percentages". Remembering that for this should be within a Linearlayout.

example:

<LinearLayout
android:wheigthsum=1 <!-- as somas dos layout_weight deve ser 1 == 100% -->
 ...>
    <LinearLayout
        android:layout_height=0dp
        android:layout_weight=0.3 <!-- 30% -->
        ...>
        <!-- cabeçalho, alguns textviews ou outros componentes aqui -->
    </LinearLayout>

    <LinearLayout
        android:layout_height=0dp
        android:wheigthsum=0.5 <!-- 50% -->
        ...>
        <!-- implementa seu listview aqui -->
        <ListView
            android:id="@+id/lvPlaylist"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>

    <LinearLayout
        android:layout_height=0dp
        android:layout_weight=0.2 <!-- 20% -->
        ...>
        <!-- rodapé, alguns textviews ou outros componentes aqui -->
    </LinearLayout>
</LinearLayout>

0

You should use margins or some component to limit the size. If you want to leave only the list on the screen, but not filling everything, add margins, if you want to fill this space with another component, use belowof or aboveof of relative layout. Remember, that if your application supports different screens, implement other layouts and using rightof or leftof instead of belowof.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.