滚动listView Items View混合后的元素

时间:2013-09-07 08:29:46

标签: java android

我在android项目中有简单的Լis.View.OfListView项目是两种类型。 1.歌曲类型 2.文件夹类型 ListView项目模型'SongModel',有TYPE字段 SongModel

public class SongModel {


public String title;
public int type;

public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}

public int getType() {
    return type;
}
public void setType(int type) {
    this.type = type;
}

}

ListView项目视图我有3个元素:歌曲名称为TextView,歌曲或文件夹照片为ImageView,歌曲播放为ImageButton。

<?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" >

<ImageView
    android:id="@+id/song_photo"
    android:layout_width="119dp"
    android:layout_height="match_parent"
    android:src="@drawable/ic_launcher" />



    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />


</LinearLayout>

<ImageButton
    android:id="@+id/add_queue"
    android:layout_width="wrap_content"
    android:layout_height="542dp"
    android:src="@drawable/ic_launcher" />

在Adapter类我检查,如果TYPE = 1我在项目中查看绘画歌曲照片。如果TYPE = 2我绘制文件夹照片,我ImageButton可见性设置View.GONE。

package com.example.adapter;
import java.util.ArrayList;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

  public class SongAdapter extends ArrayAdapter<SongModel> {
private ArrayList<SongModel> entries;
private Activity activity;

 static class ViewHolder {

  public ImageButton song_play;
  public TextView song_name;
  public ImageView song_photo;


 }
  public SongAdapter(Activity a, int textViewResourceId,ArrayList<SongModel> entries) {

  super(a, textViewResourceId, entries);
    this.entries = entries;
    this.activity = a;
}


 @Override
  public View getView(int position, View convertView, ViewGroup parent) {
  View rowView = convertView;
  if (rowView == null) {
  LayoutInflater inflater = activity.getLayoutInflater();
  rowView = inflater.inflate(R.layout.list_item, null);
  ViewHolder viewHolder = new ViewHolder();
  viewHolder.song_name = (TextView) rowView.findViewById(R.id.title);
  viewHolder.song_photo = (TextView) rowView.findViewById(R.id.song_photo);
  viewHolder.song_play = (ImageView) rowView .findViewById(R.id.song_play);
  rowView.setTag(viewHolder);
}

ViewHolder holder = (ViewHolder) rowView.getTag();
SongModel song = entries.get(position);

 holder.song_name.setText(song. getTitle());

if (song.getType()==2)
{ 

  holder.song_play.setVisibility(View.Gone);
  holder.song_photo.setImageResource(R.drawable.folder);

}
else
{
     holder.song_photo.setImageResource(R.drawable.song);
}
 rowView.setTag(getItemViewType(position));
 return rowView;
}
  }

在活动代码

 package com.example.adapter;

 import java.util.ArrayList;

 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;

 import android.os.Bundle;
  import android.app.Activity;
   import android.view.Menu;
   import android.widget.ListView;

 public class MainActivity extends Activity {
private ArrayList<SongModel> fetch;
private ListView MusicItemlistView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MusicItemlistView=(ListView)findViewById(R.id.listview);
     fetch = new ArrayList<SongModel>();

    try {
        JSONObject jsonData=new JSONObject(datajson);
    JSONArray dataArray=jsonData.getJSONArray("responseBody");
    SongModel sg;
    JSONObject js;
    for(int i=0;i<dataArray.length();i++)
    {
        sg=new SongModel();
        js=(JSONObject) dataArray.get(i);
        sg.setAlbum(js.getString("artist"));
        sg.setFileName(js.getString("fileName"));
        sg.setType(Integer.parseInt(js.getString("type")));
        sg.setDuration(Integer.parseInt(js.getString("duration")));
        fetch.add(sg);
    }
    MyPerformanceArrayAdapter  adapter = new MyPerformanceArrayAdapter        (this,R.id.listview, fetch);
    MusicItemlistView.setAdapter(adapter);
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

 }

启动应用程序时,工作正常,所有项目绘制为true,如果文件夹没有ImageButton,如果歌曲有ImageButton。

滚动列表视图项目混合的视图元素,添加ImageButton的文件夹项目和歌曲项目没有ImageButton。

请帮助我,以获得正确的错误。

2 个答案:

答案 0 :(得分:2)

尝试这个:

if (song.getType()==2)
{ 

  holder.song_play.setVisibility(View.Gone);
  holder.song_photo.setImageResource(R.drawable.folder);

}
else
{
 holder.song_play.setVisibility(View.VISISBLE);
 holder.song_photo.setImageResource(R.drawable.song);
}

答案 1 :(得分:0)

当歌曲类型不是2时,请确保将song_play设置为View.Visible的可见性。您的单元格会被重复使用,而某些重复使用的视图会使song_play元素不可见。

if (song.getType()==2)
{ 

  holder.song_play.setVisibility(View.Gone);
  holder.song_photo.setImageResource(R.drawable.folder);

}
else
{
     holder.song_play.setVisibility(View.Visible); //Add this here.
     holder.song_photo.setImageResource(R.drawable.song);
}